elasticsearch无法分析查询“或”

2hh7jdfx  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(2)|浏览(422)

我正在尝试从索引中搜索文档中的“或”字。
这是我输入的查询

GET index_name/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "query_string": {
            "query": "OR word1",
            "fields": [
              "field1"
            ]
          }
        }
      ]
    }
  }
}

我得到以下错误

{
  "error": {
    "root_cause": [
      {
        "type": "query_shard_exception",
        "reason": "Failed to parse query [OR word1]",
        "index_uuid": "Brju2MNeQ5m7sI02Q2Y4gw",
        "index": "index_name"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "index_name",
        "node": "C5Suw-gaR7eehoH0WNGw1A",
        "reason": {
          "type": "query_shard_exception",
          "reason": "Failed to parse query [OR word1]",
          "index_uuid": "Brju2MNeQ5m7sI02Q2Y4gw",
          "index": "index_name",
          "caused_by": {
            "type": "parse_exception",
            "reason": "Cannot parse 'OR word1': Encountered \" <OR> \"OR \"\" at line 1, column 0.\nWas expecting one of:\n    <NOT> ...\n    \"+\" ...\n    \"-\" ...\n    <BAREOPER> ...\n    \"(\" ...\n    \"*\" ...\n    <QUOTED> ...\n    <TERM> ...\n    <PREFIXTERM> ...\n    <WILDTERM> ...\n    <REGEXPTERM> ...\n    \"[\" ...\n    \"{\" ...\n    <NUMBER> ...\n    <TERM> ...\n    ",
            "caused_by": {
              "type": "parse_exception",
              "reason": "Encountered \" <OR> \"OR \"\" at line 1, column 0.\nWas expecting one of:\n    <NOT> ...\n    \"+\" ...\n    \"-\" ...\n    <BAREOPER> ...\n    \"(\" ...\n    \"*\" ...\n    <QUOTED> ...\n    <TERM> ...\n    <PREFIXTERM> ...\n    <WILDTERM> ...\n    <REGEXPTERM> ...\n    \"[\" ...\n    \"{\" ...\n    <NUMBER> ...\n    <TERM> ...\n    "
            }
          }
        }
      }
    ]
  },
  "status": 400
}

为什么这是一个错误?克服这个错误的正确方法是什么?如果我从查询中删除“or”这个词,它可以正常工作,但是当我保留它时,它会给我这个错误

pqwbnv8z

pqwbnv8z1#

不建议使用查询字符串,如es官方文档中所述:
因为对于任何无效的语法,它都会返回一个错误,所以我们不建议对搜索框使用query\u string查询。
如果不需要支持查询语法,请考虑使用匹配查询。如果您需要查询语法的特性,请使用简单的\u查询\u字符串查询,它不太严格。
搜索查询:

{
  "query": {
    "match": {
      "title": {
        "query": "OR"
      }
    }
  }
}

搜索结果:

"hits": [
            {
                "_index": "stof_63952836",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.43445712,
                "_source": {
                    "title": "OR word1"
                }
            }
        ]
lndjwyie

lndjwyie2#

如果我能很好地理解你的问题,我就创建了一个索引,并接收了两个文档。我试着用这个词想出一个建议 escape 字符= \\ 以下是我摄取的数据:
“field1”:“word1”
“field1”:“或word1”
然后,我对您的查询做了一个小修改:

{
    "query": {
        "bool": {
        "filter": [
            {
                "query_string": {
                    "query": "word1 \\OR",
                    "fields": ["field1"]
                }
            }
            ]
        }
    }
}

答复是:

"hits" : [
  {
    "_index" : "or-doc",
    "_type" : "_doc",
    "_id" : "9mCioHQBowpsxTkF3Jdx",
    "_score" : 0.0,
    "_source" : {
      "field1" : "word1"
    }
  },
  {
    "_index" : "or-doc",
    "_type" : "_doc",
    "_id" : "82CjoHQBowpsxTkFCZgE",
    "_score" : 0.0,
    "_source" : {
      "field1" : "OR word1"
    }
  }
]

注意:我逃过了 OR 因为它被认为是 reserved words .
如果你有什么问题,我很乐意帮忙。
链接:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html

相关问题