具有模式的ElasticSearch匹配查询

js5cn81o  于 2022-11-02  发布在  ElasticSearch
关注(0)|答案(1)|浏览(169)

我有一个列,其中所有的值都以“ARK”开头。

number
ARK101223
ARK123422
ARK234002
ARK234177

我需要使用ElasticSearch获取与ARK匹配的列编号的所有记录。只要我将编号作为列并与ARK匹配,我就只需检索这些记录。某些记录将不将编号作为列,因此我希望忽略这些记录。
下面是我尝试但无效的查询

{
  "query": {
    "bool": {
      "must": [
        {
          "prefix": {
            "number.keyword": "ARK"
          }
        },
        {
          "range": {
            "date_1": {
              "gte": "2022-01-01 01:00:00",
              "lte": "2022-03-10 01:00:00"
            }
          },
          "sort": [
            {
              "date_1": {
                "order": "asc"
              },
              "date_2": {
                "order": "asc"
              },
              "ts": {
                "order": "asc"
              }
            }
          ]
        }
      ]
    }
  }
}

以下是错误:

{
    "error": {
        "root_cause": [
            {
                "type": "parsing_exception",
                "reason": "[range] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
                "line": 1,
                "col": 155
            }
        ],
        "type": "x_content_parse_exception",
        "reason": "[1:155] [bool] failed to parse field [must]",
        "caused_by": {
            "type": "parsing_exception",
            "reason": "[range] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
            "line": 1,
            "col": 155
        }
    },
    "status": 400
}
vybvopom

vybvopom1#

如果Elasticsearch为您的索引生成了Map,那么您将有.keyword字段作为您的number文本字段,并且在此基础上您可以使prefix query得到预期的结果。

{
    "query": {
        "prefix": {
            "number.keyword": "ARK"
        }
    }
}

更新:

{
    "query": {
        "bool": {
            "must": [
                {
                    "prefix": {
                        "number.keyword": "ARK"
                    }
                },
                {
                    "range": {
                        "date_1": {
                            "gte": "2022-01-01 01:00:00",
                            "lte": "2022-03-10 01:00:00"
                        }
                    }
                }
            ]
        }
    },
    "sort": [
        {
            "date_1": {
                "order": "asc"
            },
            "date_2": {
                "order": "asc"
            },
            "ts": {
                "order": "asc"
            }
        }
    ]
}

相关问题