1

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"
            }
        }
    ]
}
1
  • Can you show the parsing exception you get? Commented Aug 3, 2015 at 4:19

2 Answers 2

2

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"
            }
        }
    ]
}
Sign up to request clarification or add additional context in comments.

6 Comments

How would i include the bool? you didn't provide bool query in the example
@HorseVoice notice I only changed the filter part not the whole filtered, just substitute your filter part of the whole query with what I shown.
Awesome!! is there some blog or guide that simply explains what can go into what? What I mean is, for example, I get confused whether a query can go into a bool, or a filter can go into a query or a bool can go into a filter etc. Really confusing. And the documentation doesn't properly describe these rules of query formulation. Should I open a new question on this topic and you can answer there? I'll select your answer there too. Let me know. Thank you!
@HorseVoice well actually personally I've been always using their documentation and found it pretty reasonable but maybe because I've read it all? I mean they don't have much data redundancy and if you don't read it all carefully you indeed will miss a lot of important information.
I posted the question. You see I have to read through the documentation on a per query basis, but I'm actually trying to write an internal sql like api that will build up the elastic dsl. So I need the rules defined for what goes into what in an elastic query.
|
2

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

1 Comment

I posted the question. You see I have to read through the documentation on a per query basis, but I'm actually trying to write an internal sql like api that will build up the elastic dsl. So I need the rules defined for what goes into what in an elastic query

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.