查询Lucene

表“testtable”的结构是

  1. id int主键

  2. productid int

  3. attributeid int

  4. value varchar(250)

其中productid是产品的唯一ID,attributeid是产品属性的唯一ID,例如尺寸,质量,高度,颜色,’value’是属性的值

我必须过滤结果。 我通过此查询达到了要求。 但我无法在查询中进行此操作。

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' ) 

需要帮助来构建此查询..

我认为你必须分两步完成:

第1步:提取产品ID

 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); 

然后,您需要从文档中提取productId

第2步:运行查询

 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); 

看看使用Hibernate Search,它为您提供了基于lucene的数据库搜索语义。 或者看看luke并弄清楚lucene如何索引你的数据。 使用它可以帮助您构建lucene查询,因​​为它可以让您更深入地了解lucene索引和搜索。