嵌套 json 对象的弹性搜索映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30308817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:54:15  来源:igfitidea点击:

Elastic search mapping for nested json objects

jsonelasticsearch

提问by Jayadratha Mondal

I need help regarding elastic search mapping of nested json document. I search in web a lot but didnt find any good to the point info. Suppose I have this type of data..

我需要有关嵌套 json 文档的弹性搜索映射的帮助。我在网上搜索了很多,但没有找到任何有用的信息。假设我有这种类型的数据..

{
  "name" : "Zach",
  "car" : [
    {
      "make" : "Saturn",
      "model" : "SL"
    },
    {
      "make" : "Subaru",
      "model" : "Imprezza"
    }
  ]
}
{
  "name" : "Bob",
  "car" : [
    {
      "make" : "Saturn",
      "model" : "Imprezza"
    }
  ]
}

where car can have any number of data objects inside. According to the elastic searchdoc I came to know that for nested json I have to specify the type as nested. But there has no information regarding how I will specify the mapping info of variables under that nested type.

其中 car 可以包含任意数量的数据对象。根据弹性搜索文档,我知道对于嵌套 json,我必须将类型指定为嵌套。但是没有关于我将如何指定该嵌套类型下变量的映射信息的信息。

Like in the example above I can write the mapping like this.

就像上面的例子一样,我可以像这样编写映射。

{
  "person":{
    "properties":{
      "name" : {
        "type" : "string"
      },
      "car":{
        "type" : "nested"
      }
    }
  }
}

but how to provide mapping info for car.make& car.model??

但是如何为car.make&提供映射信息car.model??

Will this work fine without any future problem?

这会在没有任何未来问题的情况下正常工作吗?

{
  "person": {
    "properties": {
      "name" : {
        "type" : "string"
      },
      "car": {
        "type" : "nested"
        "properties": {
          "make": {....},
          "model": {....}
        }
      }
    }
  }
}

回答by Andrei Stefan

You can do it like this:

你可以这样做:

PUT /my_index
{
  "mappings": {
    "blogpost": {
      "properties": {
        "comments": {
          "type": "nested", 
          "properties": {
            "name":    { "type": "string"  },
            "comment": { "type": "string"  },
            "age":     { "type": "short"   },
            "stars":   { "type": "short"   },
            "date":    { "type": "date"    }
          }
        }
      }
    }
  }
}

Quote from this section of the ES definitive guide.

引用自这个部分的ES明确的指导

回答by Rajnish

put /my_index/_mapping/my_type 
{
 "person": {
   "properties": {
     "name" : {
       "type": "text",
       "analyzer": "string_lowercase",
       "fields": {
         "keyword": {
           "type": "keyword"
         }
       }
      },
      "car": {
        "type" : "nested",
        "properties": {
          "make": { 
            "type": "text",
            "analyzer": "string_lowercase",
            "fields": {
            "keyword": {
              "type": "keyword"
            }
           }
          },
          "model": { 
            "type": "text",
            "analyzer": "string_lowercase",
            "fields": {
            "keyword": {
              "type": "keyword"
             }
            }
           }
         }
       }
     }
    }
   }