首页 > 解决方案 > 我想使用过滤器进行搜索,该过滤器排除与条件或其他条件不匹配的结果:

问题描述

如果我像这样给出它,它的给出结果是期望 { "track_total_hits": true, "query": { "bool": { "must": [ { "bool": { "should": [ { "simple_query_string": { “字段”:[“GECORRIGEERDETOEVOEGING”],“查询”:“ {uer} ”,“analyze_wildcard”:“true”,“default_operator”:“AND”}}],“minimum_should_match”:1}}]}}, “_source”:[“GECORRIGEERDETOEVOEGING”,“INLEGDATUM”],“大小”:30,“来自”:0 }

但是如果我这样给我就会出错

{
  "track_total_hits": true,
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "simple_query_string": {
                  "fields": [
                    "GECORRIGEERDETOEVOEGING"
                  ],
                  "query": "*{uer}*",
                  "analyze_wildcard": "true",
                  "default_operator": "AND"
                }
              },
              {
                "query_string": {
                  "default_field": "GECORRIGEERDETOEVOEGING.keyword",
                  "query": "*{uer}*",
                  "analyze_wildcard": "true",
                  "default_operator": "AND"
                }
              }
            ],
            "minimum_should_match": 1
          }
        }
      ]
    }
  },
  "_source": [
    "GECORRIGEERDETOEVOEGING",
    "INLEGDATUM"
  ],
  "size": 30,
  "from": 0
}

错误:

"failed_shards" : [
      {
        "shard" : 0,
        "index" : "momsarchival2",
        "node" : "BxZPpbudTl-zjT1a2OAGZA",
        "reason" : {
          "type" : "query_shard_exception",
          "reason" : "Failed to parse query [*{uer}*]",
          "index_uuid" : "e40ac98FRYKKun3C4BXaNA",
          "index" : "momsarchival2",
          "caused_by" : {
            "type" : "parse_exception",
            "reason" : "Cannot parse '*{uer}*': Encountered \" \"}\" \"} \"\" at line 1, column 5.\r\nWas expecting:\r\n    \"TO\" ...\r\n    ",
            "caused_by" : {
              "type" : "parse_exception",
              "reason" : "Encountered \" \"}\" \"} \"\" at line 1, column 5.\r\nWas expecting:\r\n    \"TO\" ...\r\n    "
            }
          }
        }
      }
    ]
  },
  "status" : 400
}

<

标签: elasticsearch

解决方案


当存在查询字符串的一部分并且需要被视为普通字符时,您必须转义 lucene 保留字符。所以在字符串中"*{uer}*"你必须逃避{}

{
  "track_total_hits": true,
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "simple_query_string": {
                  "fields": [
                    "GECORRIGEERDETOEVOEGING"
                  ],
                  "query": "*\\{uer\\}*",
                  "analyze_wildcard": "true",
                  "default_operator": "AND"
                }
              },
              {
                "query_string": {
                  "default_field": "GECORRIGEERDETOEVOEGING.keyword",
                  "query": "*{uer}*",
                  "analyze_wildcard": "true",
                  "default_operator": "AND"
                }
              }
            ],
            "minimum_should_match": 1
          }
        }
      ]
    }
  },
  "_source": [
    "GECORRIGEERDETOEVOEGING",
    "INLEGDATUM"
  ],
  "size": 30,
  "from": 0
}

推荐阅读