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("bodytext", "<text>")); Query query2 = new TermQuery(new Term("title", "<text>")); booleanQuery.add(query1, BooleanClause.Occur.SHOULD); booleanQuery.add(query2, BooleanClause.Occur.SHOULD);
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[] {"bodytext", "title"}, 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);
Another option is to create a new field when you index your content with the name bodytextandtitle into which you can put the contents of both bodytext and title, then you only need to search for one field.
Sam Doshi Jan 10 '10 at 11:50 2010-01-10 11:50
source share