0

I'm not quite sure why the term filter "term": {"language": "Austrian"} is causing an elastic search parse exception.

The surprising thing is it works if I remove the query_string query. Where would I put "term": {"language": "Austrian"} filter if it doesn't go there?

{
  "query": {
    "filtered": {
      "query": {
        "bool": {
          "must": [
            {
              "terms": {
                "status_type": [
                  "1",
                  "2",
                  "7"
                ]
              }
            }
          ]
        }
      },
      "filter": {
        "query": {
          "query_string": {
            "fields": [
              [
                "name",
                "message"
              ]
            ],
            "query": "Arnold AND Schwarz"
          }
        },
        "term": {                <-- Causes parse exception
          "language": "Austrian"
        }
      }
    }
  },
  "sort": [
    {
      "total": {
        "order": "desc"
      }
    }
  ]
}

1 Answer 1

1

Inside your filter, you need a bool filter if you have more than one constraints, which is your case, since you have a query filter and a term filter. So the correct way of doing it is like this:

{
  "query": {
    "filtered": {
      "query": {
        "bool": {
          "must": [
            {
              "terms": {
                "status_type": [
                  "1",
                  "2",
                  "7"
                ]
              }
            }
          ]
        }
      },
      "filter": {
        "bool": {               <---- add this
          "must": [             <---- and this
            {
              "query": {
                "query_string": {
                  "fields": [
                    [
                      "name",
                      "message"
                    ]
                  ],
                  "query": "Arnold AND Schwarz"
                }
              }
            },
            {
              "term": {
                "language": "Austrian"
              }
            }
          ]
        }
      }
    }
  },
  "sort": [
    {
      "total": {
        "order": "desc"
      }
    }
  ]
}

However, if I may add something, I would rewrite your query a bit differently and move the query_string over to the query part and the status_type term over to the filter part, it would feel more "natural". Also, in your query part you don't need a bool/must if you have only one constraint.

{
  "query": {
    "filtered": {
      "query": {
        "query_string": {
          "fields": [
            [
              "name",
              "message"
            ]
          ],
          "query": "Arnold AND Schwarz"
        }
      },
      "filter": {
        "bool": {
          "must": [
            {
              "terms": {
                "status_type": [
                  "1",
                  "2",
                  "7"
                ]
              }
            },
            {
              "term": {
                "language": "Austrian"
              }
            }
          ]
        }
      }
    }
  },
  "sort": [
    {
      "total": {
        "order": "desc"
      }
    }
  ]
}
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent. Thank you Val!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.