用于从文档中检索对象数组的特定对象的弹性搜索查询

问题描述 投票:0回答:1

我下面的文件具有弹性,只需要从聊天数组中获取sessionId:3dddf4ab的对话,但是我正在获取整个文档

Elastic文档:

{
  "_index" : "myindex",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "chatid" : "1",
    "chat" : [
      {
        "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
          }
        }
      }
    ]
  }
}   

在Kibana中搜索查询DSL:

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

预期如下,但获取了整个文档:

这是我期望的输出,相反,我正在获取包括其他对话ID的整个文档

{
    "_index": "myindex",
    "_type": "_doc",
    "_id": "1",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "chatid": "1",
        "chat": [{
                "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"
                                            }
                                         }
                                       }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

非常感谢您的帮助

elasticsearch
1个回答
0
投票

相信,您需要从嵌套查询中检索内部匹配,如下所示:

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

      }
    }
  }
}

Response

{
  "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
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.