Fzyy
Fzyy

Reputation: 13

ES: Match bool and fuzzy query

I have ES query. Now I want add to this query 'fuzzy' parameter. I'am trying :

            "body"  : {
            "query" : {
                "bool" : {
                    "must" : {
                        $finalQuery,
                        },
                    }
                },
                "match" : {
                    "city" : {
                        "query" : 'Tokkiio',
                        "fuzziness" : "AUTO"
                    },
                }
            }

$finalQuery is query generated in loop, contains terms, range and term parameters.

I am receiving :

"{"error":{"root_cause":[{"type":"parsing_exception","reason":"[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":177}],"type":"parsing_exception","reason":"[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":177},"status":400}"

Thanks for help.

Upvotes: 1

Views: 2517

Answers (1)

Jose
Jose

Reputation: 289

Please restructure the query into Query Context and Filter Context as documented here - https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html.

Place your fuzzy query and similar conditions in query context. Move the range and any filter conditions to filter context. Here is a sample query.

{
    "query":
    {
        "bool":
        {
            "must":
            {
                "fuzzy":
                {
                    "city":
                    {
                        "value": "Tokkiio",
                        "fuzziness": "AUTO"
                    }
                }
            },
            "filter":
            {
                "range":
                {
                    "year":
                    {
                        "gte": 2016
                    }
                }
            }
        }
    }
}

Upvotes: 2

Related Questions