2

Can somebody explain me please what is wrong with this query? I need to convert this generated query from Elasticsearch 2 to Elasticsearch 6. In ES2 this one works well, but in ES6 it throws me an error: [or] query malformed, no start_object after query name. I am lost in it. OR is necessary cause there could be more conditions than this one.

{
    "query": {
        "bool": {
            "filter": {
                "or": [
                    {
                        "nested": {
                            "path": "zalozcovia",
                            "query": {
                                "bool": {
                                    "filter": [
                                        {
                                            "match": {
                                                "zalozcovia.meno": "\u013dubo\u0161"
                                            }
                                        },
                                        {
                                            "match": {
                                                "zalozcovia.priezvisko": "Majgot"
                                            }
                                        },
                                        {
                                            "match": {
                                                "zalozcovia.mesto": "Trnava"
                                            }
                                        }
                                    ]
                                }
                            }
                        }
                    }
                ]
            }
        }
    },
    "size": 20,
    "sort": [
        {
            "rok": "desc"
        },
        {
            "cislo": "desc"
        }
    ]
}

Thanks.

Čamo
  • 3,863
  • 13
  • 62
  • 114

2 Answers2

3

In ES6 there is afaik no "OR" Query (https://www.elastic.co/guide/en/elasticsearch/reference/6.4/query-dsl-or-query.html). You should use a bool query and use there the "should" Part (https://www.elastic.co/guide/en/elasticsearch/reference/6.4/query-dsl-bool-query.html).

{
    "query": {
        "bool": {
            "filter": [{
                "bool": {
                    "should": [{
                        "nested": {
                            "path": "zalozcovia",
                            "query": {
                                "bool": {
                                    "filter": [{
                                            "match": {
                                                "zalozcovia.meno": "\u013dubo\u0161"
                                            }
                                        },
                                        {
                                            "match": {
                                                "zalozcovia.priezvisko": "Majgot"
                                            }
                                        },
                                        {
                                            "match": {
                                                "zalozcovia.mesto": "Trnava"
                                            }
                                        }
                                    ]
                                }
                            }
                        }
                    }]
                }
            }]
        }
    },
    "size": 20,
    "sort": [{
            "rok": "desc"
        },
        {
            "cislo": "desc"
        }
    ]
}
RichieK
  • 474
  • 6
  • 15
  • 1
    Yes I know but SHOULD throws me the same error only insead of [or] is [should] – Čamo Mar 01 '19 at 16:33
  • Don't add `or` inside the `should` part. – dadoonet Mar 01 '19 at 16:34
  • I dont. I change the OR to SHOULD. – Čamo Mar 01 '19 at 16:36
  • can you show your corrected query? at least the error somehow shows, that your syntax is still not applicable. i think you should get rid of the first filter and switch or to should. – RichieK Mar 01 '19 at 16:41
  • You removed FILTER level. I missed it. Without filter it works. But I am not sure if I can remove it from the query. – Čamo Mar 01 '19 at 16:55
  • bool.filter is like bool.must without affecting score in ES6. i dont know, what ES2 was like. maybe you can describe what you want to get with this query. – RichieK Mar 01 '19 at 16:58
  • Hmm if I add the BOOL level inside the FILTER it works. – Čamo Mar 01 '19 at 16:59
  • well I guess you can also just remove the "or" part of your query and keep the filter, because you just have one statement in there (and "or" just affecting with 2 or more entitys). – RichieK Mar 01 '19 at 17:00
  • I am little confused about the score. I thought filter is exactly the way how to not count the score. – Čamo Mar 01 '19 at 17:02
  • i updated the answer so it uses filter as you wished instead of should – RichieK Mar 01 '19 at 17:02
  • yes filter ignores the score. i think the query now should fit your needs – RichieK Mar 01 '19 at 17:03
  • No the SHOULD is necessary cause the code is generated and there could be more conditions not only one. – Čamo Mar 01 '19 at 17:03
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/189270/discussion-between-camo-and-richiek). – Čamo Mar 01 '19 at 17:11
2

Try changing "filter-or" with should

{
    "query": {
        "bool": {
              "should" : [
                    {
                        "nested": {
                            "path": "zalozcovia",
                            "query": {
                                "bool": {
                                    "filter": [
                                        {
                                            "match": {
                                                "zalozcovia.meno": "\u013dubo\u0161"
                                            }
                                        },
                                        {
                                            "match": {
                                                "zalozcovia.priezvisko": "Majgot"
                                            }
                                        },
                                        {
                                            "match": {
                                                "zalozcovia.mesto": "Trnava"
                                            }
                                        }
                                    ]
                                }
                            }
                        }
                    }
                ]
            }
    },
    "size": 20,
    "sort": [
        {
            "rok": "desc"
        },
        {
            "cislo": "desc"
        }
    ]
}
ibrahimozgon
  • 1,137
  • 1
  • 12
  • 19
  • Yes it works but I am not sure if I can remove Filter level from query. Filter should be little efficient cause it does not count a score. Am I right? – Čamo Mar 01 '19 at 16:54
  • I'm not sure sir, But in your query above, seems, it doesn't need filter with or :) You can check this answer for must and filter difference: https://stackoverflow.com/a/43349478/2057653 – ibrahimozgon Mar 01 '19 at 17:01
  • I know but there could be more conditions than one. – Čamo Mar 01 '19 at 17:06
  • you can add each condition to should array ? I don't get what is wrong? – ibrahimozgon Mar 01 '19 at 17:10
  • Ok I have it. There should be one more BOOL level inside the FILTER. Thanks. – Čamo Mar 01 '19 at 18:00