2009-06-16 76 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索引和搜索。