user3182237
user3182237

Reputation: 39

Searching for multi keywords in multi fields using LUCENE library

I want to search for keyword1 in field1 and keyword2 in field 2 Actually this code works perfectly but it gives me the results of field1 and field2 containing keyword in one of them:

StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_46, stopWordsSet );

String[] fields = { "field1", "field2"};
Float float10 = new Float(10);
Float float5 = new Float(5);
Map<String, Float> boost = new HashMap<String, Float>();
boost.put("nom", float10);
boost.put("email", float10);
MultiFieldQueryParser mfqp = 
    new MultiFieldQueryParser(Version.LUCENE_46,fields, analyzer, boost);
mfqp.setAllowLeadingWildcard(true); 
Query userQuery = mfqp.parse("*keyword*");

Upvotes: 0

Views: 110

Answers (2)

maksim07
maksim07

Reputation: 390

If you want to find documents which contains keyword1 in field1 and keyword2 in field2 then boolean query may help:

BooleanQuery query = new BooleanQuery();
query.add(new TermQuery(new Term("field1", "keyword1")), BooleanClause.Occur.MUST);
query.add(new TermQuery(new Term("field2", "keyword2")), BooleanClause.Occur.MUST);

Upvotes: 0

groverboy
groverboy

Reputation: 1135

Your question seems to be about finding documents that contain 'keyword' in both field1 and field2. If this is correct the following may help. Note the + signs which are needed in case the default operator is OR.

Query userQuery = mfqp.parse("+field1:*keyword* +field2:*keyword*")

Upvotes: 0

Related Questions