OR and AND Operators in Elasticsearch query

前端 未结 2 2066
醉梦人生
醉梦人生 2021-02-13 01:59

I have few json document with the following format :-

    _source: {
            userId: \"A1A1\",
            customerId: \"C1\",
            component: \"comp_         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-13 02:52

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

    Something like :

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

    Which gives in Java:

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

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

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

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

提交回复
热议问题