Предполагая, что ваше отображение для вложенного объекта / коллекции правильно настроено, вы можете использовать вложенный запрос
Подготовка: Удалить индекс
DELETE test_nested
Подготовка: установка вложенного отображения
PUT test_nested
{
"mappings": {
"properties": {
"attr1": {
"type": "text"
},
"nestedAttr": {
"type": "nested",
"properties": {
"id": {
"type": "integer"
},
"type": {
"type": "keyword"
},
"output": {
"type": "keyword"
}
}
}
}
}
}
Подготовка: создание do c # 1
POST test_nested/_doc/1
{
"attr1": "attrVal1",
"nestedAttr": [
{
"id": 1,
"type": "Type1",
"output": "PASS"
},
{
"id": 2,
"type": "Type2",
"output": "FAIL"
},
{
"id": 3,
"type": "Type3",
"output": "PASS"
}
]
}
Подготовка: Создать do c # 2
POST test_nested/_doc/2
{
"attr1": "attrVal1",
"nestedAttr": [
{
"id": 1,
"type": "Type2",
"output": "PASS"
},
{
"id": 2,
"type": "Type1",
"output": "FAIL"
},
{
"id": 3,
"type": "Type3",
"output": "FAIL"
}
]
}
Поиск: (nestedAttr.type: Type1 AND nestedAttr.output: PASS)
POST /test_nested/_search
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "nestedAttr",
"query": {
"bool": {
"must": [
{
"match": {
"nestedAttr.type": "Type1"
}
},
{
"match": {
"nestedAttr.output": "PASS"
}
}
]
}
}
}
}
]
}
}
}
Результат: count (nestedAttr.type: Type1 AND nestedAttr.output: PASS) = 1
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.7227666,
"hits" : [
{
"_index" : "test_nested",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.7227666,
"_source" : {
"attr1" : "attrVal1",
"nestedAttr" : [
{
"id" : 1,
"type" : "Type1",
"output" : "PASS"
},
{
"id" : 2,
"type" : "Type2",
"output" : "FAIL"
},
{
"id" : 3,
"type" : "Type3",
"output" : "PASS"
}
]
}
}
]
}
}