0

The document has a field like this.

"device_versions": [
            {
              "name": "android",
              "min_major_ver": 3,
              "min_minor_ver": 54,
              "min_patch_ver": 0
            },
            {
              "name": "ios",
              "min_major_ver": 2,
              "min_minor_ver": 59,
              "min_patch_ver": 0
            }
          ]

I want to write a query like this

(device_versions.name = 'android' AND device_versions.min_major_ver = 3 AND device_versions.min_minor_ver = 55)

I have written the following query-

"must": [
              {
                "term": {
                  "device_versions.name": "android"
                }
              },
              {
                "range": {
                  "device_versions.min_major_ver": {
                    "from": 3,
                    "include_lower": true,
                    "include_upper": true,
                    "to": null
                  }
                }
              },
              {
                "range": {
                  "device_versions.min_minor_ver": {
                    "from": 55,
                    "include_lower": true,
                    "include_upper": true,
                    "to": null
                  }
                }
              }
            ]

The problem with above query is: The last range query matches with the array element which has device name = 'ios'

What I need is: Apply 3 condition on same array element. device = "android" AND min_major_ver = 55 AND min_minor_ver = 55

1
  • are u aware of nested datatype in elasticsearch? Does your mapping has nested enabled? Commented Dec 26, 2019 at 14:04

1 Answer 1

2

Your mapping should have nested enabled, otherwise your objects will flatten out

PUT my_index/_doc/1
{
  "group" : "fans",
  "user" : [ 
    {
      "first" : "John",
      "last" :  "Smith"
    },
    {
      "first" : "Alice",
      "last" :  "White"
    }
  ]
}

will be stored as

{
  "group" :        "fans",
  "user.first" : [ "alice", "john" ],
  "user.last" :  [ "smith", "white" ]
}

https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html

after nesting enabled your query should be

{
  "query": {
    "nested": {
      "path": "device_versions",
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "device_versions.name": "android"
              }
            },
            {
              "range": {
                "device_versions.min_major_ver": {
                  "from": 3,
                  "include_lower": true,
                  "include_upper": true,
                  "to": null
                }
              }
            },
            {
              "range": {
                "device_versions.min_minor_ver": {
                  "from": 55,
                  "include_lower": true,
                  "include_upper": true,
                  "to": null
                }
              }
            }
          ]
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.