# Nested query

**URL:** <https://discuss.elastic.co/t/nested-query/363549>\
**Category:** Elasticsearch\
**Created:** [July 22, 2024, 11:01am UTC](https://discuss.elastic.co/t/nested-query/363549 "2024-07-22T11:01:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shrm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shrm/32/131044_2.png) [@shrm](https://discuss.elastic.co/u/shrm)\
**Post date:** [July 22, 2024, 11:01am UTC](https://discuss.elastic.co/t/nested-query/363549/1 "2024-07-22T11:01:36Z")

</div>

I have an index for a country list as:

```auto
{
  "countries": {
    "mappings": {
      "properties": {
        "cities": {
          "type": "nested",
          "properties": {
            "id": {
              "type": "integer"
            },
            "latitude": {
              "type": "float"
            },
            "longitude": {
              "type": "float"
            },
            "name": {
              "type": "text"
            }
          }
        },
        "region": {
          "type": "keyword"
        }
      }
    }
  }
}

```

When I query it with a country name it works like this:

Query:

```auto
GET /countries/_search
{
  "query": {
    "term": {
      "_id": "Iran"
    }
  }
}

```

Result:

```auto
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "countries",
        "_id": "Iran",
        "_score": 1,
        "_source": {
          "region": "Asia",
          "cities": [
            {
              "id": 146942,
              "name": "Ab Meshkin",
              "latitude": "35.61305556",
              "longitude": "48.26916667"
            },
            {
              "id": 150327,
              "name": "Ab Pakhsh",
              "latitude": "29.36788550",
              "longitude": "51.05140670"
            },
            {
              "id": 150322,
              "name": "Abad",
              "latitude": "29.02994140",
              "longitude": "51.23435480"
            },...

```

How can I limit the result to a single city? I tried with

```auto
GET /countries/_search
{
  "query": {
    "nested": {
      "path": "cities",
      "query": {
        "bool": {
          "must": [
            { "term": { "cities.id": 146942 } }
          ]
        }
      },
      "inner_hits": {}
    }
  }
}

```

and

```auto
GET /countries/_search
{
  "query": {
    "nested": {
      "path": "cities",
      "query": {
        "bool": {
          "must": [
            { "term": { "cities.id": 146942} }
          ]
        }
      },
      "inner_hits": {
        "name": "city_hits"
      }
    }
  }
}

```

But both show the same result as "GET /countries/\_search" result that I provided before.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [July 22, 2024, 11:17am UTC](https://discuss.elastic.co/t/nested-query/363549/2 "2024-07-22T11:17:39Z")

</div>

Hi @shrm

Try adding this:

```auto
  "_source": {
    "excludes": "*"
  },

```

```auto
GET /countries/_search
{  
  "_source": {
    "excludes": "*"
  },
  "query": {
    "nested": {
      "path": "cities",
      "query": {
        "bool": {
          "must": [
            { "term": { "cities.id": 146942} }
          ]
        }
      },
      "inner_hits": {
        "name": "city_hits"
      }
    }
  }
}

```

Now you will only receive the value of inner\_hits.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 22, 2024, 11:28am UTC](https://discuss.elastic.co/t/nested-query/363549/3 "2024-07-22T11:28:48Z")

</div>

IMO you should index cities and not countries, if this is your use case.

---

<div class="post-metadata">

**Author:** ![shrm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shrm/32/131044_2.png) [@shrm](https://discuss.elastic.co/u/shrm)\
**Post date:** [July 22, 2024, 2:11pm UTC](https://discuss.elastic.co/t/nested-query/363549/4 "2024-07-22T14:11:14Z")

</div>

@dadoonet Thanks for the suggestion. I want to use suggestions on city names, which one is better, indexing the city or country?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 22, 2024, 9:12pm UTC](https://discuss.elastic.co/t/nested-query/363549/5 "2024-07-22T21:12:23Z")

</div>

If you are searching for cities, index cities. Simple as that ☺
