Query in Lucene

自闭症网瘾萝莉.ら 提交于 2019-12-22 01:29:07

问题


The structure of the table "testtable" is

  1. id int primary key

  2. productid int

  3. attributeid int

  4. value varchar(250)

where productid is the unique id of a product, attributeid is the unique id of attribute of a product e.g. size, quality,height, color and 'value' is the value for the attribute

i have to filter a result. I achieve the requirement by this query. But i am not able to make it in a query.

select a.* from dbo.testtable a
where a.attributeId=10 and a.[Value]='Romance'
and productId in
(
    select productId
    from
    dbo.testtable where attributeId =7 and [Value]='Hindi'
)

Need help to build this query..


回答1:


I think you have to do this in two steps:

Step 1: extract product ids

BooleanQuery query = new BooleanQuery();

query.add(new TermQuery("attributeId", 7), BooleanClause.Occur.MUST); 
query.add(new TermQuery("value", "hindi"), BooleanClause.Occur.MUST); 
TopDocs docs = searcher.search(query, null, searchLimit);

You then need to extract the productId from the docs

Step 2: run query

BooleanQuery query = new BooleanQuery();

query.add(new TermQuery("attributeId", 10), BooleanClause.Occur.MUST); 
query.add(new TermQuery("value", "Romance"), BooleanClause.Occur.MUST); 

// build "IN" clause
BooleanQuery pidQuery = new BooleanQuery();
for( long productId : productIds ){
    pidQuery.add(new TermQuery("productId", productId), BooleanClause.Occur.SHOULD); 
}
query.add(pidQuery, BooleanClause.Occur.MUST); 
TopDocs docs = searcher.search(query, null, searchLimit);



回答2:


Look at using Hibernate Search which provides you with semantics of lucene based searching on a database. Alternatively look at luke and figure out how lucene has indexed your data. Play around with it and it will help you frame lucene queries as it gives you a deeper look into lucene indexing and searching.



来源:https://stackoverflow.com/questions/999936/query-in-lucene

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!