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

时间:2015-12-09 01:20:41

标签: elasticsearch lucene elasticsearch-plugin

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

  

:遇到“< EOF>”在第1行,第19栏。期待以下之一:   < BAREOPER> ......“(”......“”......< QUOTED> ...< TERM> ...< PREFIXTERM>   ......< WILDTERM> ......< REGEXPTERM> ...“[”...“{”...< NUMBER> ......];   嵌套:ParseException [遇到“< EOF>”在第1行,第19栏   期待以下之一:< 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
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

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
      }
    }
  }
}