Search code examples
elasticsearchelasticsearch-6

What is wrong with my Elastisearch query?


I need to make multi_match and bool query at once, but query below doesn't work :( When I use them separatley, they working perfect.

{
  "query": {
    "multi_match": {
      "query": "kotlety*",
      "fields": [
        "name"
      ]
    },
    "bool": {
      "filter": {
        "term": {
          "status": 2
        }
      }
    }
  },
  "size": 24
}

response is:

{
    "error": {
        "root_cause": [
            {
                "type": "parsing_exception",
                "reason": "[multi_match] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
                "line": 9,
                "col": 5
            }
        ],
        "type": "parsing_exception",
        "reason": "[multi_match] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
        "line": 9,
        "col": 5
    },
    "status": 400
}

Elastic 6.6, I think my query can have wrong syntax?


Solution

  • The query can't contain both a bool and a multi_match. You could re-arrange it like this:

    {
      "query": {
        "bool": {
          "filter": {
            "term": {
              "status": 2
            }
          },
          "should": {
            "multi_match": {
            "query": "kotlety*",
            "fields": [
              "name"
            ]
          }
        }
      }
      },
      "size": 24
    }