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.