首页 文章

在ElasticSearch中有条件地添加模糊性

提问于
浏览
0

我的所有文档中都有十个字段:特别是 product_code ,每个文档和字符串类型都是唯一的 .

我在 _all 上有一个匹配查询,效果很好,但我想执行"fuzzy match",同时保留搜索精确 product_code 的能力

这是我尝试过的:

"query": {
    "bool": {
        "should": [
            {
                "match": {
                    "product_code": {
                        "query": searchString,
                        "operator": "AND"
                    }
                }
            },
            {
                "match": {
                    "_all": {
                        "query": searchString,
                        "operator": "AND"
                        "fuzziness": 2,
                        "prefix_length": 2
                    }
                }
            }
        ]
    }
}

这种方法的问题在于,模糊性也被应用于搜索 product_code ,因为它包含在_1074982中 .

有没有办法首先在 product_code 上执行搜索,如果没有找到结果,请在 _all 上执行搜索,或从 _all 查询中排除 product_code

任何帮助是极大的赞赏 .

1 回答

  • 1

    是的,您可以使用以下映射从_all中排除 product_code .

    PUT index_name
    {
        "settings": {
            "analysis": {
                "analyzer": {},
                "filter": {}
            }
        },
        "mappings": {
            "type_name": {
                "properties": {
                    "product_code": {
                        "type": "string",
                        "include_in_all": false
                    }
                }
            }
        }
    }
    

    或者,您可以使用query_string搜索,它也提供模糊性 .

    使用以下查询将查询字符串与AND运算符和模糊设置一起使用

    {
        "query": {
            "bool": {
                "should": [{
                    "query_string": {
                        "fields": ["product_code", "other_field"],
                        "query": "this is my string",
                        "default_operator": "AND",
                        "fuzziness": 2,
                        "fuzzy_prefix_length": 2
                    }
                }, {
                    "match": {
                        "product_code": {
                            "query": "this is my string",
                            "operator": "AND"
                        }
                    }
                }]
            }
        }
    }
    

    希望这可以帮助

相关问题