【问题标题】:Elasticsearch wildcard query on numeric fields without using mapping在不使用映射的情况下对数字字段进行 Elasticsearch 通配符查询
【发布时间】:2020-12-02 11:57:42
【问题描述】:

我正在寻找一个有创意的解决方案,因为我无法使用映射,因为解决方案已经在生产中。 我有这个问题:

            {
              "size": 4,
              "query": {
                "bool": {
                  "filter": [
                    {
                      "range": {
                        "time": {
                          "from": 1597249812405,
                          "to": null,
                        }
                      }
                    },
                    {
                      "query_string": {
                        "query": "*181*",
                        "fields": [
                          "deId^1.0",
                          "deTag^1.0",
                        ],
                        "type": "best_fields",
                        "default_operator": "or",
                        "max_determinized_states": 10000,
                        "enable_position_increments": true,
                        "fuzziness": "AUTO",
                        "fuzzy_prefix_length": 0,
                        "fuzzy_max_expansions": 50,
                        "phrase_slop": 0,
                        "escape": false,
                        "auto_generate_synonyms_phrase_query": true,
                        "fuzzy_transpositions": true,
                        "boost": 1
                      }
                    }
                  ],
                  "adjust_pure_negative": true,
                  "boost": 1
                }
              },
              "sort": [
                {
                  "time": {
                    "order": "asc"
                  }
                }
              ]
            }

“deId”字段是弹性搜索中的整数,查询不返回任何内容(尽管应该), 有没有在数字字段中搜索通配符而不使用需要映射的多字段选项的解决方案?

【问题讨论】:

    标签: elasticsearch amazon-elasticsearch


    【解决方案1】:

    一旦你索引了一个整数,ES 不会将单个数字视为位置敏感的标记。换句话说,不能直接对数字数据类型执行通配符。

    有一些次优方法可以解决这个问题(想想脚本和String.substring),但最简单的方法是将这些整数转换为字符串。

    让我们看一个例子deId 123181994:

    POST prod/_doc
    {
      "deId_str": "123181994"
    }
    

    然后

    GET prod/_search
    {
      "query": {
        "bool": {
          "filter": [
            {
              "query_string": {
                "query": "*181*",
                "fields": [
                  "deId_str"
                ]
              }
            }
          ]
        }
      }
    }
    

    像魅力一样工作。

    由于您的索引/映射已经在生产中,请查看_update_by_query 并在一次调用中对所有必要的数字进行字符串化。之后,如果您不想(和/或不能)在索引时传递字符串,请使用 ingest pipelines 为您进行转换。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      • 2020-03-14
      • 1970-01-01
      • 2017-12-17
      • 1970-01-01
      相关资源
      最近更新 更多