Elasticsearch无法解析查询无法解析“ OR”

时间:2020-09-18 09:15:43

标签: elasticsearch

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

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”,它可以正常工作,但是当我保留它时,会出现此错误

2 个答案:

答案 0 :(得分:1)

如果我很好地理解了您的问题,我创建了一个索引,并提取了两个文档。我尝试使用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

答案 1 :(得分:0)

不建议使用{_3}中提到的query_string:

由于它针对任何无效语法返回错误,因此不建议 使用query_string查询搜索框。

如果您不需要支持查询语法,请考虑使用匹配项 查询。如果您需要查询语法的功能,请使用 simple_query_string查询,不太严格。

搜索查询:

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

搜索结果:

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