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"
            }
        }
    ]
}
Horse Voice
  • 8,138
  • 15
  • 69
  • 120

2 Answers2

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"
      }
    }
  ]
}
Val
  • 207,596
  • 13
  • 358
  • 360
  • 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 – Horse Voice Aug 03 '15 at 15:51
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"
            }
        }
    ]
}
Mateusz Dymczyk
  • 14,969
  • 10
  • 59
  • 94
  • How would i include the bool? you didn't provide bool query in the example – Horse Voice Aug 03 '15 at 15:13
  • @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. – Mateusz Dymczyk Aug 03 '15 at 15:14
  • 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! – Horse Voice Aug 03 '15 at 15:25
  • @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. – Mateusz Dymczyk Aug 03 '15 at 15:27
  • Let me post a new question on this particular subject. Since I'm new to it, I'm hoping it will bring me upto speed quickly. thanks – Horse Voice Aug 03 '15 at 15:30
  • 1
    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. – Horse Voice Aug 03 '15 at 15:50