elasticsearch Elasticsearch查询嵌套对象,elasticsearch,nested,elasticsearch,Nested" /> elasticsearch Elasticsearch查询嵌套对象,elasticsearch,nested,elasticsearch,Nested" />

elasticsearch Elasticsearch查询嵌套对象

elasticsearch Elasticsearch查询嵌套对象,elasticsearch,nested,elasticsearch,Nested,我有这张唱片: { "FirstName": "Winona", "LastName": "Ryder", "Notes": "<p>she is an actress</p>", "Age": "40-50", "Race": "Caucasi

我有这张唱片:

{
    "FirstName": "Winona",
    "LastName": "Ryder",
    "Notes": "<p>she is an actress</p>",
    "Age": "40-50",
    "Race": "Caucasian",
    "Gender": "Female",
    "HeightApproximation": "No",
    "Armed": false,
    "AgeCategory": "Adult",
    "ContactInfo": [
        {
            "ContactPoint": "stranger@gmail.com",
            "ContactType": "Email",
            "Details": "Details of tv show",
        }
    ]
}
我还尝试:

{
   "query": {
     "term" : { "ContactInfo.ContactType" : "email" } 
   }
}
以下是联系人信息的映射:

"ContactInfo":{
    "type": "object"
}
我想我知道的问题是,字段在映射中没有设置为嵌套的,有没有一种方法可以在不更改映射的情况下仍然查询嵌套的字段,我只是希望尽可能避免重新索引数据。 我是弹性搜索的新手,需要你的帮助


提前感谢。

Elasticsearch没有内部对象的概念。

Elasticsearch官方文档中关于

  • 嵌套类型是对象数据类型的一个专门版本,它允许对象数组以相互独立的方式进行索引
  • 如果需要和保持数组中每个对象的独立性,请使用嵌套数据类型而不是对象数据类型
  • 在内部,嵌套对象将数组中的每个对象作为单独的隐藏文档进行索引,这样每个嵌套对象都可以通过嵌套查询独立于其他对象进行查询
  • 请参阅此,以获取有关此的更多详细信息

    添加带有索引映射、搜索查询和搜索结果的工作示例

    应用嵌套数据类型后,必须重新为数据编制索引

    索引映射:

    {
      "mappings": {
        "properties": {
          "ContactInfo": {
            "type": "nested"
          }
        }
      }
    }
    
    {
        "query": {
            "nested" : {
                "path" : "ContactInfo",
                "query" : {
                    "match" : {"ContactInfo.Details" : "Details of tv show"}
                }
            }
        }
    }
    
    "hits": [
          {
            "_index": "stof_64269180",
            "_type": "_doc",
            "_id": "1",
            "_score": 1.1507283,
            "_source": {
              "FirstName": "Winona",
              "LastName": "Ryder",
              "Notes": "<p>she is an actress</p>",
              "Age": "40-50",
              "Race": "Caucasian",
              "Gender": "Female",
              "HeightApproximation": "No",
              "Armed": false,
              "AgeCategory": "Adult",
              "ContactInfo": [
                {
                  "ContactPoint": "stranger@gmail.com",
                  "ContactType": "Email",
                  "Details": "Details of tv show"
                }
              ]
            }
          }
        ]
    
    搜索查询:

    {
      "mappings": {
        "properties": {
          "ContactInfo": {
            "type": "nested"
          }
        }
      }
    }
    
    {
        "query": {
            "nested" : {
                "path" : "ContactInfo",
                "query" : {
                    "match" : {"ContactInfo.Details" : "Details of tv show"}
                }
            }
        }
    }
    
    "hits": [
          {
            "_index": "stof_64269180",
            "_type": "_doc",
            "_id": "1",
            "_score": 1.1507283,
            "_source": {
              "FirstName": "Winona",
              "LastName": "Ryder",
              "Notes": "<p>she is an actress</p>",
              "Age": "40-50",
              "Race": "Caucasian",
              "Gender": "Female",
              "HeightApproximation": "No",
              "Armed": false,
              "AgeCategory": "Adult",
              "ContactInfo": [
                {
                  "ContactPoint": "stranger@gmail.com",
                  "ContactType": "Email",
                  "Details": "Details of tv show"
                }
              ]
            }
          }
        ]
    
    搜索结果:

    {
      "mappings": {
        "properties": {
          "ContactInfo": {
            "type": "nested"
          }
        }
      }
    }
    
    {
        "query": {
            "nested" : {
                "path" : "ContactInfo",
                "query" : {
                    "match" : {"ContactInfo.Details" : "Details of tv show"}
                }
            }
        }
    }
    
    "hits": [
          {
            "_index": "stof_64269180",
            "_type": "_doc",
            "_id": "1",
            "_score": 1.1507283,
            "_source": {
              "FirstName": "Winona",
              "LastName": "Ryder",
              "Notes": "<p>she is an actress</p>",
              "Age": "40-50",
              "Race": "Caucasian",
              "Gender": "Female",
              "HeightApproximation": "No",
              "Armed": false,
              "AgeCategory": "Adult",
              "ContactInfo": [
                {
                  "ContactPoint": "stranger@gmail.com",
                  "ContactType": "Email",
                  "Details": "Details of tv show"
                }
              ]
            }
          }
        ]
    
    “点击次数”:[
    {
    “_索引”:“stof_64269180”,
    “\u类型”:“\u单据”,
    “_id”:“1”,
    “_分数”:1.1507283,
    “_来源”:{
    “名字”:“威诺娜”,
    “姓氏”:“赖德”,
    “备注”:“她是一名演员”

    , “年龄”:“40-50岁”, “种族”:“白种人”, “性别”:“女性”, “高度近似值”:“否”, “武装”:假, “年龄类别”:“成人”, “联系人信息”:[ { “联系人”:stranger@gmail.com", “联系人类型”:“电子邮件”, “详情”:“电视节目详情” } ] } } ]
    非常感谢您的全面回复。我很高兴能为您提供帮助