2009-06-16 75 views
2

该表的结构 “TestTable的” 是查询中的Lucene

  1. ID INT主键

  2. 的productid INT

  3. 属性Id INT

  4. 值VARCHAR(250)

其中productid是产品的唯一标识, attributeid是产品属性的唯一标识,例如尺寸,质量,身高,颜色 和'值'是属性的值

我必须过滤结果。我通过这个查询来达到要求。 但我无法在查询中完成。

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

您无法使用lucene查询表格。你有这个数据的现有lucene索引吗? – skaffman 2009-06-16 07:22:13

+0

是的..数据已经编入索引 – Shashi 2009-06-16 07:43:54

回答

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

然后,您需要从文档

步骤2中提取产品ID:运行查询

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

看看使用Hibernate Search的它为您提供了基于Lucene的语义数据库上搜索。或者,查看luke并了解lucene如何为您的数据建立索引。玩它,它会帮助你框架lucene查询,因​​为它让你更深入地了解lucene索引和搜索。