在Lucene中查询 [英] Query in Lucene
问题描述
表testtable的结构是
-
id int主键
-
productid int
-
attributeid int
-
值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
id int primary key
productid int
attributeid int
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屋!