首页 文章

在elasticsearch中创建或更新映射

提问于
浏览
43

我是Elasticsearch的新手,目前正致力于实现 geo_distance 过滤器以进行搜索 . 截至目前,我的索引具有以下映射(我删除了一些字段):

{
advert_index: {
   mappings: {
      advert_type: {
         properties: {
            __v: {
               type: "long"
            },
            caption: {
               type: "string"
            },
            category: {
               type: "string"
            },
            **location: {
            type: "long"
            },**

         }
      }
   }
}

geo_distance字段将在location字段上实现,其中示例实例如下所示:

"location": [
               71,
               60
            ],

即是geoJSON格式 [lon, lat] .

据我所知,我必须更新索引,以便位置字段的类型为 geo_point ,如文档(mapping-geo-point)中所述 . 好像我必须删除索引并创建一个新索引,但我无法做到这一点 .

我在正确的轨道上吗?如果有人能帮助我如何创建新索引或使用正确的数据类型更新现有索引,我将不胜感激 .

非常感谢!

2 回答

  • 5

    请注意,此答案中提供的网址存在错误:

    对于PUT映射请求:url应如下所示:

    http://localhost:9200/name_of_index/_mappings/document_type

    并不是

    http://localhost:9200/name_of_index/document_type/_mappings

  • 53

    一般来说,您可以使用 put mapping api(参考here)更新索引映射:

    curl -XPUT 'http://localhost:9200/advert_index/_mapping/advert_type' -d '
    {
        "advert_type" : {
            "properties" : {
    
              //your new mapping properties
    
            }
        }
    }
    '
    

    它对添加新字段特别有用 . 但是,在您的情况下,您将尝试更改位置类型,这将导致 conflict 并阻止使用新映射 .

    您可以将包含该位置的put mapping api用于 add another property 作为lat / lon数组,但您将无法更新以前的位置字段本身 .

    最后,您必须重新索引数据,以便将新映射重新考虑在内 .

    最好的解决方案是 create a new index .

    如果您创建另一个索引的问题是停机时间,那么您应该看一下aliases以使事情顺利进行 .

相关问题