ES:匹配布尔值和模糊查询

时间:2018-08-15 16:53:24

标签: php database laravel elasticsearch nosql

我有ES查询。现在,我想向此查询添加“模糊”参数。 我正在尝试:

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

$ finalQuery是循环生成的查询,其中包含术语,范围和术语参数。

我收到:

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

感谢帮助。

1 个答案:

答案 0 :(得分:1)

请按照此处所述-https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html将查询重组为查询上下文和过滤上下文。

将模糊查询和类似条件放在查询上下文中。移动范围和任何过滤条件以过滤上下文。这是一个示例查询。

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