Search code examples
elasticsearchluceneelasticsearch-plugin

elastic search query crashing with colon in query_string


I added a colon to the end of the query term "query": "Schwarz AND People:" and this seems to break the query. Not sure why. Is this a bug in elastic search? I'm using 1.0.1. The response contains this confusing exception.

: 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]:*

Below is the query that's breaking because of the colon. What can I do about this, I need the colon?

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

Solution

  • The colon in query_string is also used as a delimiter between a field and query term as mentioned here.

    As a result it parses it as an invalid query. In order to specify the colon as a query term you need to explicitly escape it.

    Example :

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