Elasticsearch:_id基于文档字段?

时间:2014-07-18 16:57:52

标签: elasticsearch

我是Elasticsearch的新手。我在使用_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
}

我做错了什么吗?这应该怎么做?

1 个答案:

答案 0 :(得分:13)

编辑:_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