Skip to content
Advertisement

How can I generate Query in elastic search for multiple boolean queries

I want to generate query for multiple boolean operation dynamically in elasticsearch with spring framework.

my data in elasticsearch is like

{
   "masterID" : "<id>"
   "processedData": [
                {
                    "string_value": "22",
                    "key": "a"
                },
                {
                    "string_value": "abc",
                    "key": "s"
                },
                {
                    "string_value": "xyz",
                    "key": "n"
                }
            ]
}

I want to execute query for this boolean operation ((n=xyz) && (s=abc))

Advertisement

Answer

To be able to query Array inner object you should map your processedData as a Nested type, arrays of objects do not work as you would expect because you cannot query each object independently of the other objects in the array. If you need to be able to do this then you should use the nested data type instead of the object data type. After changing your mapping you can make a nested query with a bool query like this:

{
  "query": {
    "nested": {
      "path": "processedData",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "key": "n"
              }
            },
            {
              "match": {
                "string_value": "xyz"
              }
            }
          ]
        }
      }
    }
  }
}

For more information you can check here: https://www.elastic.co/guide/en/elasticsearch/reference/master/array.html

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement