我有一个包含多个字段的索引,其中一个是一个字符串字段,我在其中存储产品的类别名称......例如电子产品"、家庭"、花园"等
I have an index with multiple fields, one of which is a string field in which I store category names for a product... such as "Electronics", "Home", "Garden", etc
new StringField("category_name", categoryName, Field.Store.YES)); //categoryName is a value such as "Electronics"
我正在执行布尔查询以按名称、价格和类别查找产品,但我不确定如何进行 OR 搜索以便同时查询两个类别.
I am performing a Boolean query to find products by name, price, and category but I'm not sure how to do an OR search so that I can query for two categories at the same time.
我当前的查询如下所示:
My current query looks like this:
String cat = "Electronics"
TermQuery catQuery = new TermQuery(new Term("category_name", cat));
bq.add(new BooleanClause(catQuery, BooleanClause.Occur.MUST)); // where "bq" is the boolean query I am adding to, I tried .SHOULD but that didn't help either
这适用于一个类别搜索,但我不确定如何搜索电子产品或家庭"这两个类别.
this works fine for a one category search, but I am not sure how to search "Electronics OR Home" which would be two categories.
你可以这样写:
BooleanQuery categoryQuery = new BooleanQuery();
TermQuery catQuery1 = new TermQuery(new Term("category_name", "Electronics"));
TermQuery catQuery2 = new TermQuery(new Term("category_name", "Home"));
categoryQuery.add(new BooleanClause(catQuery1, BooleanClause.Occur.SHOULD));
categoryQuery.add(new BooleanClause(catQuery2, BooleanClause.Occur.SHOULD));
bq.add(new BooleanClause(categoryQuery, BooleanClause.Occur.MUST));
这篇关于Lucene OR 使用布尔查询进行搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!