Добавление Рабочий пример с сопоставлением, образцами документов и поисковым запросом.
Сопоставление:
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"status": {
"type": "text"
},
"adsPurchased": {
"type": "boolean"
},
"searchString": {
"type": "text"
},
"productCost": {
"type": "nested",
"properties": {
"product": {
"type": "text"
},
"cost": {
"type": "integer"
}
}
}
}
}
}
Поисковый запрос:
{
"query": {
"bool": {
"must": [
{
"match": {
"adsPurchased": true -->adsPurchased MUST to be true
}
},
{
"match": {
"searchString": "some_tring" -->searchString MUST be "some_tring"
}
},
{
"nested": {
"path": "productCost",
"query": {
"bool": {
"must": [ -->must have "Product 1" under productCost
{
"match": {
"productCost.product": "Product 1"
}
}
]
}
}
}
}
],
"must_not": [
{
"match": {
"status": "INACTIVE" -->status MUST NOT be "INACTIVE"
}
}
]
}
},
"sort": [ -->Sort them based on cost
{
"productCost.cost": {
"order": "asc",
"nested_path": "productCost"
}
}
]
}
Результат поиска:
"hits": [
{
"_index": "foo3",
"_type": "_doc",
"_id": "2",
"_score": null,
"_source": {
"name": "joey",
"status": "ACTIVE",
"adsPurchased": true,
"searchString": "some_tring",
"productCost": [
{
"product": "Product 1",
"cost": "30.0"
},
{
"product": "Product 5",
"cost": "90.0"
}
]
},
"sort": [
30
]
},
{
"_index": "foo3",
"_type": "_doc",
"_id": "1",
"_score": null,
"_source": {
"name": "Ross",
"status": "ACTIVE",
"adsPurchased": true,
"searchString": "some_tring",
"productCost": [
{
"product": "Product 1",
"cost": "50.0"
},
{
"product": "Product 2",
"cost": "80.0"
}
]
},
"sort": [
50
]
}
]
В результате поиска вы получите желаемый результат, например Росс и Джоуи
Чтобы узнать больше о вложенной сортировке, вы можете обратиться к этой официальной документации , а для вложенных запросов обратитесь к this