弹性搜索查询在 query_string 中因冒号崩溃

elastic search query crashing with colon in query_string

我在查询词 "query": "Schwarz AND People:" 的末尾添加了一个冒号,这似乎打断了查询。不知道为什么。这是弹性搜索中的错误吗?我正在使用 1.0.1。响应包含这个令人困惑的异常。

: Encountered "<EOF>" at line 1, column 19. Was expecting one of: <BAREOPER> ... "(" ... "" ... <QUOTED> ... <TERM> ... <PREFIXTERM> ... <WILDTERM> ... <REGEXPTERM> ... "[" ... "{" ... <NUMBER> ... ]; nested: ParseException[Encountered "<EOF>" at line 1, column 19. Was expecting one of: <BAREOPER> ... "(" ... "" ... <QUOTED> ... <TERM> ... <PREFIXTERM> ... <WILDTERM> ... <REGEXPTERM> ... "[" ... "{" ... <NUMBER> ... ]; }{[R7VEhVnkSnS2FW980lP6BA][facebook_posts][26]:*

以下是由于冒号而中断的查询。我该怎么办,我需要冒号?

{
  "query": {
    "filtered": {
      "query": {
        "bool": {
          "must": [
            {
              "query_string": {
                "fields": [
                  "name",
                  "message"
                ],
                "query": "Schwarz AND People:"
              }
            },
            {
              "range": {
                "created_time": {
                  "gte": 1363219771,
                  "lte": 1449623371
                }
              }
            }
          ]
        }
      }
    }
  },
  "size": 20,
  "sort": [
    {
      "total_interactions": {
        "order": "desc"
      }
    }
  ],
  "highlight": {
    "fields": {
      "name": {
        "pre_tags": [
          "<strong>"
        ],
        "post_tags": [
          "</strong>"
        ],
        "fragment_size": 500,
        "number_of_fragments": 1
      },
      "message": {
        "pre_tags": [
          "<strong>"
        ],
        "post_tags": [
          "</strong>"
        ],
        "fragment_size": 500,
        "number_of_fragments": 1
      }
    }
  }
}

query_string 中的冒号还用作字段和查询词之间的分隔符,如前所述 here

因此它将其解析为无效查询。为了将冒号指定为查询词,您需要对其进行显式转义。

示例:

{
  "query": {
    "filtered": {
      "query": {
        "bool": {
          "must": [
            {
              "query_string": {
                "fields": [
                  "name",
                  "message"
                ],
                "query": "Schwarz AND People\:"
              }
            },
            {
              "range": {
                "created_time": {
                  "gte": 1363219771,
                  "lte": 1449623371
                }
              }
            }
          ]
        }
      }
    }
  },
  "size": 20,
  "sort": [
    {
      "total_interactions": {
        "order": "desc"
      }
    }
  ],
  "highlight": {
    "fields": {
      "name": {
        "pre_tags": [
          "<strong>"
        ],
        "post_tags": [
          "</strong>"
        ],
        "fragment_size": 500,
        "number_of_fragments": 1
      },
      "message": {
        "pre_tags": [
          "<strong>"
        ],
        "post_tags": [
          "</strong>"
        ],
        "fragment_size": 500,
        "number_of_fragments": 1
      }
    }
  }
}