简体   繁体   中英

Elastic Search, Boolean query on array of objects

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

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
                }
              }
            }
          ]
        }
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM