Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify two Fields in Lucene QueryParser?

I read How to incorporate multiple fields in QueryParser? but i didn't get it.

At the moment i have a very strange construction like:

parser = New QueryParser("bodytext", analyzer) parser2 = New QueryParser("title", analyzer) query = parser.Parse(strSuchbegriff) query2 = parser.Parse(strSuchbegriff) 

What can i do for something like:

parser = New QuerParser ("bodytext" , "title",analyzer) query =parser.Parse(strSuchbegriff)  

so the Parser looks for the searching word in the field "bodytext" an in the field "title".

like image 655
Tyzak Avatar asked Jan 05 '10 09:01

Tyzak


People also ask

How do you use the wildcard in Lucene?

Lucene supports single and multiple character wildcard searches within single terms (not within phrase queries). To perform a single character wildcard search use the "?" symbol. To perform a multiple character wildcard search use the "*" symbol. You can also use the wildcard searches in the middle of a term.

What are Lucene special characters?

You can't search for special characters in Lucene Search. These are + - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ / @.

What is Lucene query syntax?

What is Lucene Query Syntax? Lucene is a query language that can be used to filter messages in your PhishER inbox. A query written in Lucene can be broken down into three parts: Field The ID or name of a specific container of information in a database.

Can Boolean operators like and/or and so on be used in Lucene query syntax?

You can embed Boolean operators in a query string to improve the precision of a match. The full syntax supports text operators in addition to character operators. Always specify text boolean operators (AND, OR, NOT) in all caps.


1 Answers

There are 3 ways to do this.

The first way is to construct a query manually, this is what QueryParser is doing internally. This is the most powerful way to do it, and means that you don't have to parse the user input if you want to prevent access to some of the more exotic features of QueryParser:

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); // Use BooleanClause.Occur.MUST instead of BooleanClause.Occur.SHOULD // for AND queries Hits hits = searcher.Search(booleanQuery); 

The second way is to use MultiFieldQueryParser, this behaves like QueryParser, allowing access to all the power that it has, except that it will search over 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 final way is to use the special syntax of QueryParser see 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 = "bodytext:" + text + " OR title:" + text;  Hits hits = searcher.Search(queryParser.parse(special)); 

Your other option is to create new field when you index your content called bodytextandtitle, into which you can place the contents of both bodytext and title, then you only have to search one field.

like image 144
Sam Doshi Avatar answered Sep 23 '22 09:09

Sam Doshi