在 elasticsearch 中创建或更新映射 [英] Create or update mapping in elasticsearch

查看:37
本文介绍了在 elasticsearch 中创建或更新映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to Elasticsearch and am currently working on implementing a geo_distance filter for searching. As of now my index has the following mapping (I've removed some fields):

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

         }
      }
   }
}

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

The geo_distance field is going to be implemented on the location field, where an example instance looks like this:

"location": [
               71,
               60
            ],

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

I.e. is on geoJSON format [lon, lat].

我知道我将不得不更新我的索引,以便位置字段的类型为 geo_point,如文档 (mapping-geo-point).似乎我必须删除索引并创建一个新索引,但我无法做到这一点.

I understand that I will have to update my index so that the location field is of type geo_point, as described in the documentation (mapping-geo-point). It seems like I have to drop the index and create a new one, but I am not able to do this.

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

Am I on the right track? I would greatly appreciate it if anyone could help me with how I could create a new index or update my existing one with the correct data type.

非常感谢!

推荐答案

一般来说,您可以使用 put mapping api(参考 此处):

Generally speaking, you can update your index mapping using the put mapping api (reference here) :

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

          //your new mapping properties

        }
    }
}
'

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

It's especially useful for adding new fields. However, in your case, you will try to change the location type, which will cause a conflict and prevent the new mapping from being used.

您可以使用 put 映射 api 添加另一个属性,其中包含作为纬度/经度数组的位置,但您将无法更新之前的位置字段本身.

You could use the put mapping api to add another property containing the location as a lat/lon array, but you won't be able to update the previous location field itself.

最后,您必须重新编制数据索引,才能将新映射考虑在内.

Finally, you will have to reindex your data for your new mapping to be taken into account.

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

The best solution would really be to create a new index.

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

If your problem with creating another index is downtime, you should take a look at aliases to make things go smoothly.

这篇关于在 elasticsearch 中创建或更新映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆