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

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

问题描述

我有三个字段(用户ID,标题,描述)索引对象。我想找到一个特定用户的所有对象所在的标题或描述包含特定关键字。

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

如何を修改code获得的所有对象以正确的ID,并在标题或说明中搜索短语?

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

推荐答案

我认为,这将是这样的:

I think that it will be something like this:

BooleanQuery booleanQuery = new BooleanQuery();

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

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


booleanQuery.add(new BooleanClause(userQuery , Occur.MUST));
booleanQuery.add(new BooleanClause(innerBooleanQuery, Occur.MUST));

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

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