Search code examples
elasticsearchnest

Elasticsearch more than one should, must and combined


I learning Elasticsearch, but i don't know how combine should and must. I need one field with some value and other field with several values, this condition can repeat several times. For example,

(Field1 == 1 and (Field2 == "a" or Field2 == "b" )) or
(Field1 == 2 and (Field2 == "c" or Field2 == "d" )) or
(Field1 == 3 and (Field2 == "a" or Field2 == "c" )) .... etc

I try many ways, but i can't use bool, must o should at same level. For example,

{
    "query": {
        "bool": {
            "should": [
                {
                    "bool": {
                        "must":  [
                                { "term": { "Field1": 1 } },
                                {
                                    "bool": {
                                        "should": [
                                            { "terms": { "Field2" : [ "a", "b" ] } }
                                        ]
                                    }
                                }
                        ]
                    },
                    "bool": {
                        "must": [
                                { "term": { "Field1": 2 } },
                                {
                                    "bool": {
                                        "should": [
                                            { "terms": { "Field2" : [ "c", "d" ] } }
                                        ]
                                    }
                                }
                        ]
                    },
                    "bool": {
                        "must": [
                                { "term": { "Field1": 2 } },
                                {
                                    "bool": {
                                        "should": [
                                            { "terms": { "Field2" : [ "a", "c" ] } }
                                        ]
                                    }
                                }
                        ]
                    }
                }
            ]
        }
    }
}

I really need for NEST, but i wanna start with this.


Solution

  • This is how you can do it:

    {
      "query": {
        "bool": {
          "filter": [
            {
              "bool": {
                "should": [
                  {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "field1": 1
                          }
                        },
                        {
                          "terms": {
                            "field2": [
                              "a",
                              "b"
                            ]
                          }
                        }
                      ]
                    }
                  },
                  {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "field1": 2
                          }
                        },
                        {
                          "terms": {
                            "field2": [
                              "c",
                              "d"
                            ]
                          }
                        }
                      ]
                    }
                  },
                  {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "field1": 3
                          }
                        },
                        {
                          "terms": {
                            "field2": [
                              "a",
                              "c"
                            ]
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
    

    If you want a OR b OR c where a, b, c represent any type of query then this is expressed in elastic query as:

    {
      "bool": {
        "should": [
          {
            a
          },
          {
            b
          },
          {
            c
          }
        ]
      }
    }
    

    Replace a, b, c with exact queries.