OR和AND运算符在弹性搜索查询 [英] OR and AND Operators in Elasticsearch query

查看:269
本文介绍了OR和AND运算符在弹性搜索查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个json文件,格式如下: -

I have few json document with the following format :-

    _source: {
            userId: "A1A1",
            customerId: "C1",
            component: "comp_1",
            timestamp: 1408986553,
     }

我想根据以下内容查询文档: -

I want to query the document based on the following :-

(( userId == currentUserId) OR ( customerId== currentCustomerId) OR (currentRole ==ADMIN) )  AND component= currentComponent)

我尝试使用SearchSourceBuilder和QueryBuilders.matchQuery,但是我无法使用AND和OR运算符放置多个子查询。

I tried using the SearchSourceBuilder and QueryBuilders.matchQuery, but I wasnt able to put multiple sub queries with AND and OR operators.

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("userId",userId)).sort("timestamp", SortOrder.DESC).size(count);

我们如何使用OR和AND运算符查询弹性搜索?

How we query elasticsearch using OR and AND operators?

推荐答案

我认为在这种情况下, Bool查询是最好的镜头。

I think in this case the Bool query is the best shot.

如下所示:

{
    "bool" : {
        "must" : { "term" : { "component" : "comp_1" } },
        "should" : [
            { "term" : { "userId" : "A1A1" } },
            { "term" : { "customerId" : "C1" } },
            { "term" : { "currentRole" : "ADMIN" } }
        ],
        "minimum_should_match" : 1
    }
}

Java中给出了哪些:

Which gives in Java:

QueryBuilder qb = QueryBuilders
    .boolQuery()
    .must(termQuery("component", currentComponent))
    .should(termQuery("userId", currentUserId))
    .should(termQuery("customerId", currentCustomerId))
    .should(termQuery("currentRole", ADMIN))
    .minimumShouldMatch("1"); // or .minimumNumberShouldMatch(1)

必须零件是 AND s,应该零件或多或少 OR s,除了您可以指定最小数量应该 s来匹配(使用 minimum_should_match ),这个最小值默认为1,我认为(但是你可以将它设置为0,这意味着一个不符合条件的文档也将被返回)

The must parts are ANDs, the should parts are more or less ORs, except that you can specify a minimum number of shoulds to match (using minimum_should_match), this minimum being 1 by default I think (but you could set it to 0, meaning that a document matching no should condition would be returned as well).

如果你想做更复杂的查询,涉及嵌套的 AND s和 OR s,只需在应该部分之间嵌套其他bool查询。

If you want to do more complex queries involving nested ANDs and ORs, simply nest other bool queries inside must or should parts.

另外,您正在寻找准确的值(ids等),也许您可​​以使用术语查询而不是匹配查询,这为您提供了分析阶段(如果这些字段完全被分析,哪些不是对于ids来说,这是非常有意义的)。如果他们被分析,你仍然可以做到这一点,但只有当你知道你的条款是如何被存储的(标准分析器将它们分为较低的套装,例如)。

Also, as you're looking for exact values (ids and so on), maybe you can use term queries instead of match queries, which spare you the analysis phase (if those fields are analyzed at all, which doesn't necessarily make sense for ids). If they are analyzed, you still can do that, but only if you know exactly how your terms are stored (standard analyzer stores them lower cased for instance).

这篇关于OR和AND运算符在弹性搜索查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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