开发者

Lucene compound query search

I want to query in lucene index with some intersection on it.

Data :

FIELDS FIELD1 FIELD2 FIELD3

ROW     ID1       VALUE1 VALUE2

ROW     ID2       VALUE3 VALUE4

ROW     ID3       VALUE4 VALUE5

ROW     ID3       VALUE6 VALUE5 开发者_StackOverflow

Query : (FIELD2:VALUE4 AND FIELD3:VALUE5) (INTERSECT ON FIELD1) (FIELD2:VALUE4 AND FIELD3:VALUE5)

So my final result should be ID3.

Any input would be of great help.

Thanks in advance.


Let me add some more data in it.

PRODUCT_ID|HEADER_ID|HEADER_VALUE

PRODUCT_01|HEADER_01|XYZ
PRODUCT_02|HEADER_02|XYZ
PRODUCT_02|HEADER_03|ABC
PRODUCT_03|HEADER_03|ABC

Now, suppose from my UI I select headers as:

HEADER_02 with value XYZ

and

HEADER_03 with value ABC

I should ideally get (expected product to which header_id 02, 03 with value XYZ, ABC are matching) PRODUCT_02 as a result. And I really don't know about product in the query.

If I query with OR I will get PRODUCT_02 as well as PRODUCT_03, which will be wrong. If I query with AND I will get nothing because Lucene operate as One-Doc-At-A-Time.

I hope my problem is descriptive this time.

That's for your reply. :)


I donot understand what you are trying to do, there is no intersection in there

if u mean to ask how do i do a compound search(AND) in lucene index, see below

TermQuery tq1= new TermQuery(new Term("VALUE4", FIELD2));
TermQuery tq2= new TermQuery(new Term("VALUE5", FIELD3));
// BooleanClauses Enum SHOULD says Use this operator for clauses that should appear in the matching documents.
BooleanQuery bq = new BooleanQuery();
bq.add(tq1,BooleanClause.Occur.SHOULD);
bq.add(tq2,BooleanClause.Occur.SHOULD);
IndexSearcher searcher = new IndexSearcher(index, true);
TopScoreDocCollector collector = TopScoreDocCollector.create(10, true);
//Iterate Collector hits
//process HitCollector or whatever terms, results,etc

EDIT:

//Parser meant for  using all the fields used for search
 MultiFieldQueryParser multiQueryParser = new MultiFieldQueryParser(
    searchFields, ANALYZER_NAME);
  multiQueryParser.setDefaultOperator(QueryParser.AND_OPERATOR);

 //You need to build something like this

 Query query1 = new TermQuery(HEADER,"HEADER_01");
 Query query2 = new TermQuery(XYZ_FIELD,"XYZ");

 Query query3 = new TermQuery(HEADER,"HEADER_02");
 Query query4 = new TermQuery(XYZ_FIELD,"ABC");

 BooleanQuery booleanQuery = new BooleanQuery();
booleanQuery.add(query1, BooleanClause.Occur.MUST);
booleanQuery.add(query2, BooleanClause.Occur.MUST);

 booleanQuery.add(query3, BooleanClause.Occur.MUST);
booleanQuery.add(query4, BooleanClause.Occur.MUST);

 //Search the index
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜