简体   繁体   English

如何在java中的弹性搜索Querybuilder中处理多个“和”“或”运算符

[英]How to handle multiple 'and' 'or' operators in Elastic search Querybuilder in java

I have a POST body JSON which has one of the input as below我有一个 POST 正文 JSON,它具有如下输入之一

    q = (a=1 and b=2 or c=3 and d=8)

I need to form a querybuilder elastic query using java我需要使用java形成一个querybuilder弹性查询

BoolQueryBuilder qb = QueryBuilders.boolQuery();
qb1= QueryBuilders.matchQuery("a", "1");
qb2= QueryBuilders.matchQuery("b", "2");
qb3= QueryBuilders.matchQuery("c", "1");
qb4= QueryBuilders.matchQuery("d", "1");

qb.must(qb1);
qb.must(qb2);
qb.should(qb3);
qb.must(qb4);

Will elastic search take care of the and operation to be done first and then or to be considered.弹性搜索是否会先处理和操作,然后再考虑。

How to I handle multiple and and or combinations (user is free to type anything ).如何处理多个和和或组合(用户可以自由输入任何内容)。 I am new to Elastic search would appreciate some help我是 Elastic 搜索的新手,希望得到一些帮助

I think bool query might be helpful to you我认为bool查询可能对您有帮助

{
    "bool" : {
        "should" : [
            { "must" : { "term" : { "a" : "1" } } },
            { "must" : { "term" : { "b" : "1" } } }
        ]
        "should" : [
            { "must" : { "term" : { "c" : "1" } } },
            { "must" : { "term" : { "d" : "1" } } }
        ]
    }
}

sharing a pseudo code, hope it helps分享一个伪代码,希望有帮助

elasticbuilder.boolQuery().should([
    elasticbuilder.boolQuery().must([
        elasticbuilder.matchQuery(1)
        elasticbuilder.matchQuery(2)    
    ])
    elasticbuilder.boolQuery().must([
        elasticbuilder.matchQuery(3)
        elasticbuilder.matchQuery(4)
    ])
])

Usually I just create an example query in Kibana and then 'translate' it to the Java client api.通常我只是在 Kibana 中创建一个示例查询,然后将它“翻译”到 Java 客户端 api。 Not sure if this applies to you but if you want to create a full text search you could provide hints to the user as per how it is in ES docs .不确定这是否适用于您,但如果您想创建全文搜索,您可以根据ES 文档中的内容向用户提供提示。

The simple_query_string query supports the following operators: + signifies AND operation | signifies OR operation - negates a single token " wraps a number of tokens to signify a phrase for searching * at the end of a term signifies a prefix query ( and ) signify precedence ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Java 库 (QueryBuilder) 组合多个过滤器查询和 Elastic Search 应该 - How do I combine multiple filter query and should of Elastic Search using Java Library (QueryBuilder) 如何在CQ5 Querybuilder中搜索多个Xpath - How search multiple Xpath in CQ5 Querybuilder 如何在弹性搜索中基于多字段值进行过滤 - How to filter based on multiple field value in elastic search java 如何使用弹性搜索Java API编写多个得分函数? - How to use elastic search java API to write multiple score functions? 如何通过 Java 高级 rest 客户端在 Elastic Search 中使用多个字段进行搜索 - How to search using multiple fields in Elastic Search through Java high level rest client 我可以使用Elastic Search Java API按多个字段进行搜索吗? - Can I search by multiple fields using the Elastic Search Java API? 弹性搜索中多匹配查询中的比较运算符 - Comparison operators in multimatch query in elastic search 如何在Java中将Elastic Search 5.4连接到TCP? - How to connect Elastic search 5.4 to tcp in java? 如何在 java 中生成弹性搜索嵌套聚合? - How to generate elastic search nested aggregations in java? 如何批量更新多个id的弹性搜索? - How to update elastic search in batches for multiple id?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM