Nested query

I have an index for a country list as:

{
  "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:

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

Result:

{
  "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

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

and

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.

Hi @shrm

Try adding this:

  "_source": {
    "excludes": "*"
  },
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.

1 Like

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

1 Like

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

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