【问题标题】:Create or update mapping in elasticsearch在 elasticsearch 中创建或更新映射
【发布时间】:2014-08-24 12:30:54
【问题描述】:

我是 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) 中所述。似乎我必须删除索引并创建一个新索引,但我无法做到这一点。

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

非常感谢!

【问题讨论】:

    标签: indexing elasticsearch geocoding


    【解决方案1】:

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

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

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

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

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

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

    如果您创建另一个索引的问题是停机,您应该查看aliases 以使事情顺利进行。

    【讨论】:

    • 感谢汤姆的及时答复。我将如何具体创建具有正确位置类型的新索引?我目前处于开发环境中,因此停机等不是问题。到目前为止,我一直在使用此代码创建索引:link(在底部)
    • 我不熟悉 mongoDB 河流,但从我看到的河流是基于动态映射的(ES 自动检测要应用的映射,基于值)。您可以通过creating the index and its mapping manually 控制它,然后用新创建的索引名称/类型名称替换 ARBITRARY INDEX NAME 和 ARBITRARY TYPE NAME。您也许应该为此创建一个新问题,因为它是一个单独的主题。
    • 下面提供的方法很有效。非常感谢!
    • 是否可以创建新索引但保存存储的文档?
    • 看看reindex API introduced in 2.3:它应该可以解决问题:)
    【解决方案2】:

    请注意,此答案中提供的网址有误:

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

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

    不是

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

    【讨论】:

    【解决方案3】:

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

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

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

    您不能“创建映射”,因为映射是使用索引创建的。通常,您会在创建索引时定义映射(或通过index templates):

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-11
      • 1970-01-01
      • 2020-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多