Home > OS >  Cannot query nested object in Elasticsearch
Cannot query nested object in Elasticsearch

Time:10-20

I have the following data sample:

{
  "_index" : "index-stats-202110",
  "_type" : "_doc",
  "_id" : "yT7vlHwBLghXjfKlKm7e",
  "_score" : 9.583174,
  "_source" : {
    "client_id" : "111f34db-c88c-4675-8a69-f3028b3dfa18",
    "campaign_id" : "2c3c62f7-9e77-48df-a211-108a2220a063",
    "created_at" : "1634586272",
    "date_start" : "1634580000",
    "date_end" : "1634583600",
    "specs" : [
      "events",
      "general"
    ],
    "data" : {
      "count" : 13
    }
  }
},
{
  "_index" : "index-stats-202110",
  "_type" : "_doc",
  "_id" : "yj7vlHwBLghXjfKlKm7e",
  "_score" : 9.583174,
  "_source" : {
    "client_id" : "111f34db-c88c-4675-8a69-f3028b3dfa18",
    "campaign_id" : "2c3c62f7-9e77-48df-a211-108a2220a063",
    "created_at" : "1634586272",
    "date_start" : "1634580000",
    "date_end" : "1634583600",
    "specs" : [
      "events",
      "visit"
    ],
    "data" : {
      "label" : "visit",
      "count" : 13
    }
  }
},
{
  "_index" : "index-stats-202110",
  "_type" : "_doc",
  "_id" : "yz7vlHwBLghXjfKlKm7e",
  "_score" : 9.583174,
  "_source" : {
    "client_id" : "111f34db-c88c-4675-8a69-f3028b3dfa18",
    "campaign_id" : "2c3c62f7-9e77-48df-a211-108a2220a063",
    "created_at" : "1634586272",
    "date_start" : "1634580000",
    "date_end" : "1634583600",
    "specs" : [
      "events"
    ],
    "data" : {
      "count" : 1
    },
    "geo" : {
      "country" : "q",
      "province" : "q",
      "city" : "t"
    }
  }
},
{
  "_index" : "index-stats-202110",
  "_type" : "_doc",
  "_id" : "zD7vlHwBLghXjfKlKm7e",
  "_score" : 9.583174,
  "_source" : {
    "client_id" : "111f34db-c88c-4675-8a69-f3028b3dfa18",
    "campaign_id" : "2c3c62f7-9e77-48df-a211-108a2220a063",
    "created_at" : "1634586272",
    "date_start" : "1634580000",
    "date_end" : "1634583600",
    "specs" : [
      "events"
    ],
    "data" : {
      "count" : 1
    },
    "geo" : {
      "country" : "j",
      "province" : "q",
      "city" : "d"
    }
  }
}

which I get from the query:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "client_id": "111f34db-c88c-4675-8a69-f3028b3dfa18"
          }
        },
        {
          "term": {
            "campaign_id": "2c3c62f7-9e77-48df-a211-108a2220a063"
          }
        },
        {
          "term": {
            "specs": "events"
          }
        }
      ]
    }
  }
}

and this is my mapping:

{
    "properties":{
        "specs":{
            "type":"keyword"
        },
        "campaign_id":{
            "type":"keyword"
        },
        "client_id":{
            "type":"keyword"
        },
        "created_at":{
            "type":"date"
        },
        "date_start":{
            "type":"date"
        },
        "date_end":{
            "type":"date"
        },
        "geo":{
            "type":"nested",
            "properties":{
                "country":{
                    "type":"keyword"
                },
                "province":{
                    "type":"keyword"
                },
                "city":{
                    "type":"keyword"
                }
            }
        },
        "data":{
            "enabled":false
        }
    }
}

I want to get the geo.country = 'q' which I have in my sample, but when I try to execute the following query I get an empty response:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "client_id": "111f34db-c88c-4675-8a69-f3028b3dfa18"
          }
        },
        {
          "term": {
            "campaign_id": "2c3c62f7-9e77-48df-a211-108a2220a063"
          }
        },
        {
          "term": {
            "specs": "events"
          }
        },
        {
          "term": {
            "geo.country": "q"
          }
        }
      ]
    }
  }
}

Question: How can I separate the geo.country = 'q' from the list?

CodePudding user response:

You can use nested query along with bool/must clause to achieve your required result

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "client_id": "111f34db-c88c-4675-8a69-f3028b3dfa18"
          }
        },
        {
          "term": {
            "campaign_id": "2c3c62f7-9e77-48df-a211-108a2220a063"
          }
        },
        {
          "term": {
            "specs": "events"
          }
        },
        {
          "nested": {
            "path": "geo",
            "query": {
              "term": {
                "geo.country": "q"
              }
            }
          }
        }
      ]
    }
  }
}
  • Related