开发者

Lucene: queries and docs with multiple fields

I have 开发者_运维百科a collection of documents consisting of several fields, and I need to perform queries with several terms coming from multiple fields.

What do you suggest me to use ? MultiFieldQueryParser or MultiPhraseQuery ?

thanks


How about BooleanQuery?

http://lucene.apache.org/java/3_0_2/api/core/org/apache/lucene/search/BooleanQuery.html


Choice of Analyzer

First of all, watch out which analyzer you are using. I was stumped for a while only to realise that the StandardAnalyzer filters out common words like 'the' and 'a'. This is a problem when your field has the value 'A'. You might want to consider the KeywordAnalyzer:

See this post around the analyzer.

// Create an analyzer:
// NOTE: We want the keyword analyzer so that it doesn't strip or alter any terms:
// In our example, the Standard Analyzer removes the term 'A' because it is a common English word.
// https://stackoverflow.com/a/9071806/231860
KeywordAnalyzer analyzer = new KeywordAnalyzer();

Query Parser

Next, you can either create your query using the QueryParser:

See this post around overriding the default operator.

// Create a query parser without a default field in this example (the first argument):
QueryParser queryParser = new QueryParser("", analyzer);

// Optionally, set the default operator to be AND (we leave it the default OR):
// https://stackoverflow.com/a/9084178/231860
// queryParser.setDefaultOperator(QueryParser.Operator.AND);

// Parse the query:
Query multiTermQuery = queryParser.parse("field_name1:\"field value 1\" AND field_name2:\"field value 2\"");

Query API

Or you can achieve the same by constructing the query yourself using their API:

See this tutorial around creating the BooleanQuery.

BooleanQuery multiTermQuery = new BooleanQuery();
multiTermQuery.add(new TermQuery(new Term("field_name1", "field value 1")), BooleanClause.Occur.MUST);
multiTermQuery.add(new TermQuery(new Term("field_name2", "field value 2")), BooleanClause.Occur.MUST);

Delete the Documents that Match the Query

Then we finally pass the query to the writer to delete documents that match the query:

See my answer here, related to this answer.

See the answer to this question.

// Remove the document by using a multi key query:
// http://www.avajava.com/tutorials/lessons/how-do-i-combine-queries-with-a-boolean-query.html
writer.deleteDocuments(multiTermQuery);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜