【问题标题】:Run query by regex on _id field in Elasticsearch通过正则表达式对 Elasticsearch 中的 _id 字段运行查询
【发布时间】:2020-04-23 19:36:59
【问题描述】:

我正在尝试基于名为 _id 的字段在 Elastic 搜索中运行正则表达式查询,但出现此错误:

只能在关键字和文本字段上使用通配符查询 - 不能在 [_id] 类型为 [_id]

我试过regexp:

{
    "query": {
        "regexp": {
            "_id": {
                "value": "test-product-all-user_.*",
                "flags" : "ALL",
                "max_determinized_states": 10000,
                "rewrite": "constant_score"
            }
        }
    }
}

wildcard:

{
    "query": {
        "wildcard": {
            "_id": {
                "value": "test-product-all-user_.*",
                "boost": 1.0,
                "rewrite": "constant_score"
            }
        }
    }
}

但是两者都抛出了同样的错误。

这是完整的错误以防万一:

{   "error": {
    "root_cause": [
      {
        "type": "query_shard_exception",
        "reason": "Can only use wildcard queries on keyword and text fields - not on [_id] which is of type [_id]",
        "index_uuid": "Cg0zrr6dRZeHJ8Jmvh5HMg",
        "index": "explore_segments_v3"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "explore_segments_v3",
        "node": "-ecTRBmnS2OgjHrrq6GCOw",
        "reason": {
          "type": "query_shard_exception",
          "reason": "Can only use wildcard queries on keyword and text fields - not on [_id] which is of type [_id]",
          "index_uuid": "Cg0zrr6dRZeHJ8Jmvh5HMg",
          "index": "explore_segments_v3"
        }
      }
    ]   },   "status": 400 }

【问题讨论】:

    标签: regex elasticsearch


    【解决方案1】:

    _id 是 Elasticsearch 中的一种特殊字段,它不像其他文本字段那样真正是索引字段,它实际上是根据文档的 UID “生成”的。

    您可以参考此链接了解更多信息https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-id-field.html

    根据文档,它仅支持有限类型的查询(term、terms、match、query_string、simple_query_string),如果您想进行更高级的文本搜索,如通配符或正则表达式,您需要将 ID 索引到实际的文档本身的文本字段。

    【讨论】:

      猜你喜欢
      • 2020-12-24
      • 2013-07-04
      • 2014-10-08
      • 1970-01-01
      • 2011-11-05
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      相关资源
      最近更新 更多