Эластичный поисковый запрос для извлечения определенного объекта из массива объектов из документа

У меня ниже документ в эластичном формате, и мне нужно получить только разговор, где разговор Id: 3dddf4ab из массива чата, но я получаю весь документ

Документ в эластичном:

{
  "_index" : "myindex",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "chatid" : "1",
    "conversations" : [
      {
        "conversation" : {
          "conversationId" : "3dddf4ab",
          "startTime" : 1573555131306,
          "question" : "abc",
          "language" : "English",
          "type" : "123",
          "answer" : "weeqwew",
          "feedback" : {
            "rating" : 1,
            "endTime" : 0,
            "votes" : 1,
            "likes" : 1
          }
        }
      },
      {
        "conversation" : {
          "conversationId" : "29363306",
          "startTime" : 1567756384492,
          "question" : "wer",
          "language" : "English",
          "type" : "456",
          "answer" : "zxsz",
          "feedback" : {
            "rating" : 0,
            "endTime" : 0,
            "votes" : 0,
            "likes" : 0
          }
        }
      },
      {
        "conversation" : {
          "conversationId" : "3dddf4ab",
          "startTime" : 1573555131308,
          "question" : "qwer",
          "language" : "English",
          "type" : "789",
          "answer" : "hjhlh",
          "feedback" : {
            "rating" : 0,
            "endTime" : 0,
            "votes" : 0,
            "likes" : 0
          }
        }
      },
      {
        "conversation" : {
          "conversationId" : "29363306",
          "startTime" : 1567756384499,
          "question" : "klklkl",
          "language" : "English",
          "type" : "674",
          "answer" : "kjjj;",
          "feedback" : {
            "rating" : 2,
            "endTime" : 0,
            "votes" : 4,
            "likes" : 4
          }
        }
      }
    ]
  }
}   

Поисковый запрос DSL в Kibana:

{
  "query": {
    "match": {
      "chat.conversation.conversationId": {
        "query": "3dddf4ab",
        "type": "phrase"
      }
    }
  }
}

Ожидается, как показано ниже, но получение всего документа:

Это результат, который я ожидаю, вместо этого я получаю весь документ, включая другие идентификаторы разговоров.

{
    "_index": "myindex",
    "_type": "_doc",
    "_id": "1",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "chatid": "1",
        "conversations": [{
                "conversation": {
                    "conversationId": "3dddf4ab",
                    "startTime": 1573555131306,
                    "question": "abc",
                    "language": "English",
                    "type": "123",
                    "answer": "weeqwew",
                    "feedback": {
                        "rating": 1,
                        "endTime": 0,
                        "votes": 1,
                        "likes": 1
                    }
                }
            },

            {
                "conversation": {
                    "conversationId": "3dddf4ab",
                    "startTime": 1573555131308,
                    "question": "qwer",
                    "language": "English",
                    "type": "789",
                    "answer": "hjhlh",
                    "feedback": {
                        "rating": 0,
                        "endTime": 0,
                        "votes": 0,
                        "likes": 0
                    }
                }
            }
        ]
    }
}

Отображение индекса

{
    "mappings": {
        "chatdb": {
            "properties": {
                "chatid": {
                    "type": "text"
                },
                "conversations": {
                    "type": "nested",
                    "properties": {
                        "conversation": {
                                    "type": "object",
                                    "properties": {
                                        "conversationId": {
                                            "type": "text"
                                        },
                                        "startTime": {
                                            "type": "long"
                                        },
                                        "question": {
                                            "type": "text"
                                        },
                                        "language": {
                                            "type": "text"
                                        },
                                        "type": {
                                            "type": "text"
                                        },
                                        "answer": {
                                            "type": "text"
                                        },

                                    "feedback": {
                                        "type": "object",
                                        "properties": {

                                            "rating": {
                                                "type": "float"
                                            },
                                            "endTime": {
                                                "type": "long"
                                            },
                                            "votes": {
                                                "type": "integer"
                                            },
                                            "likes": {
                                                "type": "integer"
                                            }
                                         }
                                       }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

Любая помощь высоко ценится


person Suven    schedule 12.12.2019    source источник
comment
Привет Сувен и добро пожаловать в Stack Overflow! Можете ли вы поделиться своим отображением индекса, пожалуйста?   -  person baitmbarek    schedule 12.12.2019
comment
@baitmbarek добавил отображение индекса   -  person Suven    schedule 13.12.2019
comment
Спасибо @Suven. Поле conversations совпадает с полем chat в образцах документов?   -  person baitmbarek    schedule 13.12.2019


Ответы (1)


Suven, вам нужно получить ваши внутренние совпадения из вложенного запроса, как показано ниже:

POST testindex/_search
{
  "_source" : false,
  "query": {
    "nested": {
      "path": "conversations",
      "query": {
        "term": {
          "conversations.conversation.conversationId": {
            "value": "3dddf4ab"
          }
        }
      }
      , "inner_hits": {

      }
    }
  }
}

Ответ :

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.6931472,
    "hits": [
      {
        "_index": "testindex",
        "_type": "chatdb",
        "_id": "L9qM_m4BavUEUOqEAqm-",
        "_score": 0.6931472,
        "inner_hits": {
          "conversations": {
            "hits": {
              "total": 2,
              "max_score": 0.6931472,
              "hits": [
                {
                  "_index": "testindex",
                  "_type": "chatdb",
                  "_id": "L9qM_m4BavUEUOqEAqm-",
                  "_nested": {
                    "field": "conversations",
                    "offset": 2
                  },
                  "_score": 0.6931472,
                  "_source": {
                    "conversation": {
                      "conversationId": "3dddf4ab",
                      "startTime": 1573555131308,
                      "question": "qwer",
                      "language": "English",
                      "type": "789",
                      "answer": "hjhlh",
                      "feedback": {
                        "rating": 0,
                        "endTime": 0,
                        "votes": 0,
                        "likes": 0
                      }
                    }
                  }
                },
                {
                  "_index": "testindex",
                  "_type": "chatdb",
                  "_id": "L9qM_m4BavUEUOqEAqm-",
                  "_nested": {
                    "field": "conversations",
                    "offset": 0
                  },
                  "_score": 0.6931472,
                  "_source": {
                    "conversation": {
                      "conversationId": "3dddf4ab",
                      "startTime": 1573555131306,
                      "question": "abc",
                      "language": "English",
                      "type": "123",
                      "answer": "weeqwew",
                      "feedback": {
                        "rating": 1,
                        "endTime": 0,
                        "votes": 1,
                        "likes": 1
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
person baitmbarek    schedule 13.12.2019