ElasticSearch query_string无法解析某些字符的查询

diu*_*lde 4 lucene elasticsearch pyelasticsearch

我正在使用ElasticSearch(2.4)和官方的Python客户端来执行简单的查询。我的代码:

from elasticsearch import Elasticsearch

es_client = Elasticsearch("localhost:9200")
index = "indexName"
doc_type = "docType"

def search(query, search_size):
    body = {
        "fields": ["title"],
        "size": search_size,
        "query": {
            "query_string": {
                "fields": ["file.content"],
                "query": query
            }
        }
    }
    response = es_client.search(index=index, doc_type=doc_type, body=body)
    return response["hits"]["hits"]

search("python", 10) # Works fine.
Run Code Online (Sandbox Code Playgroud)

问题是当我的查询包含不平衡的括号或方括号时。例如,search("python {programming", 10)ES抛出:

elasticsearch.exceptions.RequestError: TransportError(400, u'search_phase_execution_exception', u'Failed to parse query [python {programming}]')
Run Code Online (Sandbox Code Playgroud)

这是ES的预期行为吗?它不使用标记器删除所有这些字符吗?

注意:这也发生在我使用Java的时候。

diu*_*lde 8

我正在阅读文档,并且query_string更严格。以下是保留字符:+ - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /

所以,就像 jhilden 说的,我将不得不逃避它们或使用它们simple_query_string

文档:https : //www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html


Jos*_*ush 6

正如前面的答案中提到的,有些字符需要转义

\n\n
+ - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \\ /\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n

"query": "my:name*&&"应该"query": "my\\\\:name\\\\*\\\\&&"

\n
\n\n
\n\n

正则表达式来救援 \xe2\x9c\xa8

\n\n

借助简单的正则表达式,我们可以轻松转义这些字符

\n\n

Python

\n\n
+ - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \\ /\n
Run Code Online (Sandbox Code Playgroud)\n\n

输出:

\n\n
my\\:name\\*\\&&\n
Run Code Online (Sandbox Code Playgroud)\n\n

JavaScript

\n\n
import re\n\ndef escape_elasticsearch_query(query):\n    return re.sub(\'(\\+|\\-|\\=|&&|\\|\\||\\>|\\<|\\!|\\(|\\)|\\{|\\}|\\[|\\]|\\^|"|~|\\*|\\?|\\:|\\\\|\\/)\', \'\\\\\\\\\\\\1\', query)\n\n\nquery = \'my:name*&&\'\nescaped_query = escape_elasticsearch_query(query)\nprint(escaped_query)\n
Run Code Online (Sandbox Code Playgroud)\n\n

输出:

\n\n
my\\:name\\*\\&&\n
Run Code Online (Sandbox Code Playgroud)\n


Alw*_*nny 5

我知道我已经来晚了,但是我要在这里发布,希望对您有所帮助。当我们从Elasticsearch文档知道这里 ES有一定的保留字符。

保留字符为: + - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /

因此,现在您有两种可能的解决方案来修复它。当我遇到特殊字符问题时,这些对我来说是完美的

解决方案1:将特殊字符包裹在\\

"query": {
    "bool": {
      "must": [
        {
          "match": {
            "country_code.keyword": "IT"
          }
        },
        {
          "query_string": {
            "default_field": "display",
            "query": "Magomadas \\(OR\\), Italy"
          }
        }
      ]
    }
  }
Run Code Online (Sandbox Code Playgroud)

解决方案2:使用simple_query_string时无需改动,query但不支持default_field,因此可以fields改用。

  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "country_code.keyword": "IT"
          }
        },
        {
          "simple_query_string": {
            "fields": ["display"], 
            "query": "Magomadas (OR), Italy"
          }
        }
      ]
    }
  }
Run Code Online (Sandbox Code Playgroud)