简体   繁体   English

在 elasticsearch 中创建或更新映射

[英]Create or update mapping in elasticsearch

I am new to Elasticsearch and am currently working on implementing a geo_distance filter for searching.我是 Elasticsearch 的新手,目前正在为搜索实现geo_distance过滤器。 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"
            },**

         }
      }
   }
}

The geo_distance field is going to be implemented on the location field, where an example instance looks like this: geo_distance 字段将在 location 字段上实现,示例实例如下所示:

"location": [
               71,
               60
            ],

Ie is on geoJSON format [lon, lat] .即采用 geoJSON 格式[lon, lat]

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 ).我知道我将不得不更新我的索引,以便位置字段的类型为geo_point ,如文档 ( 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.如果有人能帮助我创建新索引或使用正确的数据类型更新现有索引,我将不胜感激。

Many thanks!非常感谢!

Generally speaking, you can update your index mapping using the put mapping api (reference here ) :一般来说,您可以使用put mapping api(参考此处)更新索引映射:

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.但是,在您的情况下,您将尝试更改位置类型,这将导致冲突并阻止使用新映射。

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.您可以使用 put mapping api包含位置的另一个属性添加为 lat/lon 数组,但您将无法更新先前的位置字段本身。

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.如果您创建另一个索引的问题是停机时间,您应该查看别名以使事情顺利进行。

Please note that there is a mistake in the url provided in this answer:请注意,此答案中提供的网址有误:

For a PUT mapping request: the url should be as follows:对于 PUT 映射请求:url 应如下所示:

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

and NOT并不是

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

In later Elasticsearch versions (7.x), types were removed.在更高的 Elasticsearch 版本 (7.x) 中,类型被删除。 Updating a mapping can becomes:更新映射可以变成:

curl -XPUT "http://localhost:9200/test/_mapping" -H 'Content-Type: application/json' -d'{
  "properties": {
    "new_geo_field": {
      "type": "geo_point"
    }
  }
}'

As others have pointed out, if the field exists, you typically have to reindex .正如其他人指出的那样,如果该字段存在,您通常必须重新 索引 There are exceptions, such as adding a new sub-field or changing analysis settings.也有例外,例如添加新的子字段或更改分析设置。

You can't "create a mapping", as the mapping is created with the index.您不能“创建映射”,因为映射是使用索引创建的。 Typically, you'd define the mapping when creating the index (or via index templates ):通常,您会在创建索引(或通过索引模板)时定义映射:

curl -XPUT "http://localhost:9200/test" -H 'Content-Type: application/json' -d'{
  "mappings": {
    "properties": {
      "foo_field": {
        "type": "text"
      }
    }
  }
}'

That's because, in production at least, you'd want to avoid letting Elasticsearch "guess" new fields.这是因为,至少在生产中,您希望避免让 Elasticsearch “猜测”新字段。 Which is what generated this question: geo data was read as an array of long values.这就是产生这个问题的原因:地理数据被读取为一个long值数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM