How to include multiple fields in QueryParser?

Dim qp1 As New QueryParser("filename", New StandardAnalyzer()) Dim qp2 As New QueryParser("filetext", New StandardAnalyzer()) . . 

I use the Lucene.Net library and ask the following question.

Instead of creating two separate QueryParser and using them to get two Hits, is it possible to search in both fields using the same QueryParser , so that I only have one Hits that gives me the total score of each document?

+34
Jan 22 '09 at 8:20
source share
4 answers

There are 3 ways to do this.

The first way is to create the query manually, this is what QueryParser does internally. This is the most powerful way to do this, and means you don’t need to analyze user input if you want to prevent access to some of the more exotic QueryParser functions:

 IndexReader reader = IndexReader.Open("<lucene dir>"); Searcher searcher = new IndexSearcher(reader); BooleanQuery booleanQuery = new BooleanQuery(); Query query1 = new TermQuery(new Term("filename", "<text>")); Query query2 = new TermQuery(new Term("filetext", "<text>")); booleanQuery.add(query1, BooleanClause.Occur.SHOULD); booleanQuery.add(query2, BooleanClause.Occur.SHOULD); // Use BooleanClause.Occur.MUST instead of BooleanClause.Occur.SHOULD // for AND queries Hits hits = searcher.Search(booleanQuery); 

The second way is to use the MultiFieldQueryParser , it behaves like a QueryParser , which allows you to access all the power it has, except that it will search across multiple fields.

 IndexReader reader = IndexReader.Open("<lucene dir>"); Searcher searcher = new IndexSearcher(reader); Analyzer analyzer = new StandardAnalyzer(); MultiFieldQueryParser queryParser = new MultiFieldQueryParser( new string[] {"filename", "filetext"}, analyzer); Hits hits = searcher.Search(queryParser.parse("<text>")); 

The last way is to use the special QueryParser syntax here .

 IndexReader reader = IndexReader.Open("<lucene dir>"); Searcher searcher = new IndexSearcher(reader); Analyzer analyzer = new StandardAnalyzer(); QueryParser queryParser = new QueryParser("<default field>", analyzer); // <default field> is the field that QueryParser will search if you don't // prefix it with a field. string special = "filename:" + text + " OR filetext:" + text; Hits hits = searcher.Search(queryParser.parse(special)); 

Another option is to create a new field when you index your content with the name filenameandtext into which you can put the contents of both the file name and the file, then you only need to search for one field.

+77
Jan 10 '10 at
source share

Just create a query string with each term:

 "filename:searchText OR filetext:searchText" 

It doesn't matter what you pass as the start field in the QueryParser constructor. Just make sure you call .Parse () on the query string to return the Query object to execute.

If you want to use the search "and":

 "+filename:searchText +filetext:searchText" 
+4
Feb 10 '09 at 14:03
source share

** You can also use MultiFieldQueryParser to search all available fields. **

eg

 Dim queryParser = New MultiFieldQueryParser(Version.LUCENE_29, indexReader__1.GetFieldNames(IndexReader.FieldOption.ALL).ToArray(), analyzer) 

here is an example.

 //get index directory Dim directory As Directory = FSDirectory.Open(New DirectoryInfo(HostingEnvironment.MapPath(VirtualIndexPath))) //get analyzer Dim analyzer As Analyzer = New StandardAnalyzer(Version.LUCENE_29) //get index reader and searcher Dim indexReader__1 As IndexReader = IndexReader.Open(directory, True) Dim indexSearch As Searcher = New IndexSearcher(indexReader__1) //add all possible fileds in multifieldqueryparser using indexreader getFieldNames method Dim queryParser = New MultiFieldQueryParser(Version.LUCENE_29, indexReader__1.GetFieldNames(IndexReader.FieldOption.ALL).ToArray(), analyzer) Dim query = queryParser.Parse(Criteria) Dim resultDocs As TopDocs = Nothing //perform search resultDocs = indexSearch.Search(query, indexReader__1.MaxDoc()) Dim hits = resultDocs.scoreDocs 

hope help

+2
Jun 06 2018-12-06T00:
source share

for each field, create a query from the above query elements, and then add the query to booleanquery, indicating that it "should".

Alternatively, check out MultiFieldQueryParser, which is a simplified way to execute it.

+1
Jan 23 '09 at 10:36
source share



All Articles