Horse Voice
Horse Voice

Reputation: 8348

using query_string query with bool in elastic search causing parsing exception

Why is this query giving me a parsing exception? If I remove the bool it does seem to work. But I need the bool there with the query_string. How can I make this work?

{
    "query": {
        "filtered": {
            "query": {
                "bool": {
                    "must": [
                        {
                            "terms": {
                                "status_type": [
                                    "5"
                                ]
                            }
                        }
                    ]
                }
            },
            "filter": {
                "query_string": {
                    "fields": [
                        [
                            "name",
                            "message"
                        ]
                    ],
                    "query": "Arnold AND Schwarz"
                }
            }
        }
    },
    "sort": [
        {
            "total_metrics": {
                "order": "desc"
            }
        }
    ]
}

Upvotes: 1

Views: 3390

Answers (2)

Mateusz Dymczyk
Mateusz Dymczyk

Reputation: 15141

You should use the query filter which wraps any query into a filter. Otherwise you will get the parse error you get No filter registered for [query_string].

You need to change your filter part to:

"filter": {
  "query": { // <- wraps a query as a filter
    "query_string": {
      "fields": [
        [
          "name",
          "message"
        ]
      ],
      "query": "Arnold AND Schwarz"
    }
  }
}

@Edit: since I see people might have problems noticing that I only pasted the changed part of the whole query including the filter part (not the whole filtered) here's the whole thing after modification:

{
    "query": {
        "filtered": {
            "query": {
                "bool": {
                    "must": [
                        {
                            "terms": {
                                "status_type": [
                                    "5"
                                ]
                            }
                        }
                    ]
                }
            },
            "filter": {
                "query": { // <- the only difference!
                    "query_string": {
                        "fields": [
                            [
                                "name",
                                "message"
                            ]
                        ],
                        "query": "Arnold AND Schwarz"
                    }
                }
            }
        }
    },
    "sort": [
        {
            "total_metrics": {
                "order": "desc"
            }
        }
    ]
}

Upvotes: 2

Val
Val

Reputation: 217344

The parsing exception you get should tell you something like No filter registered for [query_string]

Actually, there is no query_string filter, there is a query_string query, though. So if you swap the filter and the query it will work:

{
  "query": {
    "filtered": {
      "query": {
        "query_string": {         <--- move query_string in the query part
          "fields": [
            [
              "accountIdentifier",
              "accountName"
            ]
          ],
          "query": "Arnold AND Schwarz"
        }
      },
      "filter": {
        "bool": {                 <--- move the bool in the filter part
          "must": [
            {
              "terms": {
                "quantity": [
                  "5"
                ]
              }
            }
          ]
        }
      }
    }
  },
  "sort": [
    {
      "total_metrics": {
        "order": "desc"
      }
    }
  ]
}

Upvotes: 2

Related Questions