在elasticsearch中创建或更新映射

Axe*_*ran 48 indexing geocoding elasticsearch

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

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

         }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

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

"location": [
               71,
               60
            ],
Run Code Online (Sandbox Code Playgroud)

即使用geoJSON格式[lon, lat].

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

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

非常感谢!

Tho*_*asC 63

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

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

          //your new mapping properties

        }
    }
}
'
Run Code Online (Sandbox Code Playgroud)

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

您可以使用put mapping api 包含该位置的另一个属性添加为lat/lon数组,但是您将无法更新以前的位置字段本身.

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

最好的解决方案是创建一个新索引.

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


小智 8

在后来的 Elasticsearch 版本 (7.x) 中,类型被删除。更新映射可以变成:

curl -XPUT "http://localhost:9200/test/_mapping" -H 'Content-Type: application/json' -d'{
  "properties": {
    "new_geo_field": {
      "type": "geo_point"
    }
  }
}'
Run Code Online (Sandbox Code Playgroud)

正如其他人指出的那样,如果该字段存在,您通常必须重新索引。也有例外,例如添加新子字段或更改分析设置。

您无法“创建映射”,因为映射是使用索引创建的。通常,您可以在创建索引时定义映射(或通过索引模板):

curl -XPUT "http://localhost:9200/test" -H 'Content-Type: application/json' -d'{
  "mappings": {
    "properties": {
      "foo_field": {
        "type": "text"
      }
    }
  }
}'
Run Code Online (Sandbox Code Playgroud)

这是因为,至少在生产中,您希望避免让 Elasticsearch“猜测”新字段。这就是产生这个问题的原因:地理数据被读取为值数组long


use*_*066 6

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

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

HTTP://本地主机:9200/name_of_index/_mappings/DOCUMENT_TYPE

并不是

HTTP://本地主机:9200/name_of_index/DOCUMENT_TYPE/_mappings

  • 这取决于您使用的Elasticsearch版本.例如,版本0.90使用了您认为无效的符号:https://www.elastic.co/guide/en/elasticsearch/reference/0.90/indices-put-mapping.html (3认同)