如何使用 lucene API (a AND (b OR c)) 创建嵌套布尔查询? [英] How to create nested boolean query with lucene API (a AND (b OR c))?

查看:31
本文介绍了如何使用 lucene API (a AND (b OR c)) 创建嵌套布尔查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含三个字段(userId、title、description)的索引对象.我想查找特定用户的所有对象,其中标题或描述包含给定的关键字.

I have an indexed object with three fields (userId, title, description). I want to find all objects of a specific user where the title OR the description contains a given keyword.

我有这样的事情(但这显然是错误的):

I have something like this (but that's obviously wrong):

WildcardQuery nameQuery = new WildcardQuery(new Term("name", filter.getSearch()));
WildcardQuery descQuery = new WildcardQuery(new Term("description", filter.getSearch()));

TermQuery userQuery = new TermQuery(new Term("user_id", u.getId()+""));

BooleanQuery booleanQuery = new BooleanQuery();
booleanQuery.add(new BooleanClause(name_query, Occur.SHOULD));
booleanQuery.add(new BooleanClause(desc_query, Occur.SHOULD));
booleanQuery.add(new BooleanClause(user_query, Occur.MUST));

如何修改代码以获取所有具有正确ID和标题或描述中的搜索词组的对象?

How wo modify the code to get all objects with the correct ID and the search phrase in title or description?

推荐答案

我认为应该是这样的:

TermQuery userQuery = new TermQuery(new Term("user_id", u.getId()+""));

BooleanQuery orQuery = new BooleanQuery();
orQuery.add(new BooleanClause(name_query, Occur.SHOULD));
orQuery.add(new BooleanClause(desc_query, Occur.SHOULD));

BooleanQuery andQuery = new BooleanQuery();
andQuery.add(new BooleanClause(userQuery , Occur.MUST));
andQuery.add(new BooleanClause(orQuery, Occur.MUST));

这篇关于如何使用 lucene API (a AND (b OR c)) 创建嵌套布尔查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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