Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use MultiFieldQueryParser from Lucene?

I am using Version.Lucene_29. Using the normal string query method i could do the following:

Directory directory = new FSDirectory(...);
//Start Lucene retrieval.
IndexSearcher iSearch = new IndexSearcher(directory, true);
Analyzer analyzer = new WhitespaceAnalyzer();
QueryParser parser = new QueryParser(Version.LUCENE_29, "content", analyzer);
String str = 'filename:testfile.txt AND filetext:"Singapore food"'
Query query = parser.parse(str);
ScoreDoc[] hits = iSearch.search(query, 1000).scoreDocs;

How do i fire a query using MultiFieldQueryParser in Lucene similar to the string query method?

MultiFieldQueryParser multiParser = new MultiFieldQueryParser(
    Version.LUCENE_29, new String[] {"content", "ne"}, analyzer);
str = ???
Query = ????
ScoreDoc[] hits = iSearch.search(query, 1000).scoreDocs;
like image 227
alvas Avatar asked Jan 13 '12 00:01

alvas


2 Answers

MultiFieldQueryParser allows you to search for a "WORD" in more then one Fileds with same Analyzer.

e.g.

 Query query = MultiFieldQueryParser.parse("development",
        new String[]{"title", "subject"},
        new SimpleAnalyzer());

it will look for word development in Field : "title" and Field : "subject"

like image 89
Princesh Avatar answered Sep 28 '22 06:09

Princesh


MultiFieldQueryParser is-a QueryParser, MultiFieldQueryParser creates the two Queries into a BooleanClause in this case. So it also supports filename:testfile.txt AND filetext:"Singapore food".

like image 40
卢声远 Shengyuan Lu Avatar answered Sep 28 '22 05:09

卢声远 Shengyuan Lu