繁体   English   中英

Elasticsearch:_id基于文档字段?

[英]Elasticsearch: _id based on document field?

我是Elasticsearch的新手。 我很难为_id使用文档的_id 这是我的映射:

{
  "product": {
    "_id": {
      "path": "id"
    },
    "properties": {
      "id": {
        "type": "long",
        "index": "not_analyzed",
        "store": "yes"
      },
      "title": {
        "type": "string",
        "analyzer": "snowball",
        "store": "no",
        "index": "not_analyzed"
      }
    }
  }
}

这是一个示例文档:

{
  "id": 1,
  "title": "All Quiet on the Western Front"
}

索引此文档时,我得到的结果如下:

{
  "_index": "myindex",
  "_type": "book",
  "_id": "PZQu4rocRy60hO2seUEziQ",
  "_version": 1,
  "created": true
}

我做错了什么吗? 这怎么办?

编辑:_id.path在v1.5中已弃用,在v2.0中已删除。

编辑2:在支持此版本的版本上,存在性能损失,因为协调节点被强制解析所有请求(包括批量)以便为每个文档确定正确的主分片。

在映射中提供_id.path,如下所述: http ://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-id-field.html

这是一个完整的工作演示:

#!/bin/sh
echo "--- delete index"
curl -X DELETE 'http://localhost:9200/so_id_from_field/'
echo "--- create index and put mapping into place"
curl -XPUT http://localhost:9200/so_id_from_field/?pretty=true -d '{
    "mappings": {
        "tweet" : {
            "_id" : {
                "path" : "post_id"
            },
            "properties": {
                "post_id": {
                    "type": "string"
                },
                "nickname": {
                    "type": "string"
                }
            }
        }
    },
    "settings" : {
        "number_of_shards" : 1,
        "number_of_replicas" : 0
    }
}'
echo "--- index some tweets by POSTing"
curl -XPOST http://localhost:9200/so_id_from_field/tweet -d '{
    "post_id": "1305668",
    "nickname": "Uncle of the month club"
}'
curl -XPOST http://localhost:9200/so_id_from_field/tweet -d '{
    "post_id": "blarger",
    "nickname": "Hurry up and spend my money"
}'
curl -XPOST http://localhost:9200/so_id_from_field/tweet -d '{
    "post_id": "9",
    "nickname": "Who is the guy with the shoe hat?"
}'
echo "--- get the tweets"
curl -XGET http://localhost:9200/so_id_from_field/tweet/1305668?pretty=true
curl -XGET http://localhost:9200/so_id_from_field/tweet/blarger?pretty=true
curl -XGET http://localhost:9200/so_id_from_field/tweet/9?pretty=true

暂无
暂无

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

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