【问题标题】:Query in LuceneLucene 中的查询
【发布时间】:2009-06-16 07:08:07
【问题描述】:

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

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

【问题讨论】:

  • 不能用 lucene 查询表。您是否有此数据的现有 lucene 索引?
  • 是的..数据已经被索引了

标签: java hibernate lucene hibernate-search


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-26
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-13
    • 1970-01-01
    相关资源
    最近更新 更多