【问题标题】:elasticsearch: copying meta-field _id to other field while creating documentelasticsearch:创建文档时将元字段_id复制到其他字段
【发布时间】:2017-08-30 04:27:11
【问题描述】:

我正在使用elasticsearch。我看到每个文档都有元字段_id。我想使用此元字段搜索文档,因为我没有任何其他字段作为文档中的唯一字段。但是_id 是一个字符串,并且可以包含无法搜索的破折号,除非我们将字段映射添加为type :keyword。但正如here 所述,这是可能的。所以现在我正在考虑在文档中添加另一个字段newField 并使其与_id 相同。一种方法是:首先创建文档并将_id 分配给该字段并再次保存文档。但这将有 2 个不太好的连接。所以我想在创建文档本身时找到一些解决方案来设置newField。甚至可能吗?

【问题讨论】:

    标签: elasticsearch elasticsearch-mapping


    【解决方案1】:

    您可以搜索包含破折号的文档:

    PUT my_index/tweet/testwith-
    {
      "fullname" : "Jane Doe",
      "text" : "The twitter test!"
    }
    

    我们刚刚创建了一个 id 中带有破折号的文档

    GET my_index/tweet/_search
    {
      "query": {
        "terms": {
          "_id": [
            "testwith-"
          ]
        }
      }
    } 
    

    我们搜索具有以下 id 的文档:testwith-

    {
      "took": 9,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
          {
            "_index": "my_index",
            "_type": "tweet",
            "_id": "testwith-",
            "_score": 1,
            "_source": {
              "fullname": "Jane Doe",
              "text": "The twitter test!"
            }
          }
        ]
      }
    }
    

    我们找到了。我们可以搜索包含 - 的文档。

    【讨论】:

      【解决方案2】:

      【讨论】:

      猜你喜欢
      • 2020-05-26
      • 2018-03-24
      • 1970-01-01
      • 2012-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多