2

表“testtable”的结构是

  1. id int 主键

  2. 产品编号

  3. 属性 int

  4. 值 varchar(250)

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

我必须过滤结果。我通过这个查询实现了要求。但我无法在查询中完成。

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

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

4

2 回答 2

4

我认为您必须分两步执行此操作:

第 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);
于 2009-06-26T21:53:53.823 回答
0

看看使用 Hibernate Search,它为您提供基于数据库搜索的 lucene 语义。或者查看卢克并找出 lucene 是如何索引您的数据的。使用它,它将帮助您构建 lucene 查询,因为它可以让您更深入地了解 lucene 索引和搜索。

于 2009-06-16T07:40:46.010 回答