繁体   English   中英

在Lucene中查询

[英]Query in Lucene

表“ testtable”的结构是

  1. id int主键

  2. productid int

  3. attributeid int

  4. 值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索引和搜索。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM