在Lucene中查询 [英] Query in Lucene

查看:87
本文介绍了在Lucene中查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表testtable的结构是


  1. id int主键


  2. productid int


  3. attributeid int


  4. 值varchar(250)

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



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

 从dbo.testtable中选择a。* a 
其中a.attributeId = 10和a。[Value] = 'Romance'
和productId in

)从
中选择productId
dbo.testtable其中attributeId = 7和[Value] ='印地语'

需要帮助来构建这个查询..

解决方案

我认为你必须分两步来做:

第1步:提取产品id

  BooleanQuery query = new BooleanQuery(); 

query.add(新的TermQuery(attributeId,7),BooleanClause.Occur.MUST);
query.add(新的TermQuery(value,hindi),BooleanClause.Occur.MUST);
TopDocs docs = searcher.search(query,null,searchLimit);

然后您需要从文档中提取productId

第二步:运行查询

  BooleanQuery query = new BooleanQuery(); 

query.add(新的TermQuery(attributeId,10),BooleanClause.Occur.MUST);
query.add(新的TermQuery(value,Romance),BooleanClause.Occur.MUST);

//建立IN子句
BooleanQuery pidQuery = new BooleanQuery(); (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);


The structure of the table "testtable" is

  1. id int primary key

  2. productid int

  3. attributeid int

  4. value varchar(250)

where productid is the unique id of a product, attributeid is the unique id of attribute of a product e.g. size, quality,height, color and 'value' is the value for the attribute

i have to filter a result. I achieve the requirement by this query. But i am not able to make it in a query.

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

Need help to build this query..

解决方案

I think you have to do this in two steps:

Step 1: extract product ids

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

You then need to extract the productId from the docs

Step 2: run query

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

这篇关于在Lucene中查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆