首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Elasticsearch:基于文档字段的_id?

Elasticsearch:基于文档字段的_id?
EN

Stack Overflow用户
提问于 2014-07-19 00:57:52
回答 1查看 18.4K关注 0票数 13

我是Elasticsearch的新手。我在为_id使用文档的字段时遇到了困难。下面是我的映射:

代码语言:javascript
运行
复制
{
  "product": {
    "_id": {
      "path": "id"
    },
    "properties": {
      "id": {
        "type": "long",
        "index": "not_analyzed",
        "store": "yes"
      },
      "title": {
        "type": "string",
        "analyzer": "snowball",
        "store": "no",
        "index": "not_analyzed"
      }
    }
  }
}

下面是一个示例文档:

代码语言:javascript
运行
复制
{
  "id": 1,
  "title": "All Quiet on the Western Front"
}

在为这个文档建立索引时,我得到了如下内容:

代码语言:javascript
运行
复制
{
  "_index": "myindex",
  "_type": "book",
  "_id": "PZQu4rocRy60hO2seUEziQ",
  "_version": 1,
  "created": true
}

我做错什么了吗?这应该如何工作呢?

EN

Stack Overflow用户

回答已采纳

发布于 2014-07-19 01:33:40

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

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

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

下面是一个完整的、有效的演示:

代码语言:javascript
运行
复制
#!/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
票数 16
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24830248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档