【问题标题】:Adding fuzziness conditionally in ElasticSearch在 ElasticSearch 中有条件地添加模糊性
【发布时间】:2017-06-18 14:04:15
【问题描述】:

我的所有文档中有十个左右的字段:特别是product_code,它在每个文档中都是唯一的,并且是字符串类型。

我在_all 上有一个匹配查询,效果很好,但我想执行“模糊匹配”,同时保留精确搜索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,因为它包含在_all 中。

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

非常感谢任何帮助。

【问题讨论】:

    标签: elasticsearch


    【解决方案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"
                        }
                    }
                }]
            }
        }
    }
    

    希望对你有帮助

    【讨论】:

    • 感谢您的建议。有没有办法在查询本身中完成此操作而不是更改映射?
    • 您使用的是哪个版本的elasticsearch?
    • 那么你也可以使用 query_string,我已经编辑了我的模糊查询字符串查询的答案
    • 这似乎对我不起作用。查询返回所有结果。我将硬着头皮将映射更改为不在 _all 中包含 product_code。谢谢。
    猜你喜欢
    • 2023-03-20
    • 2023-04-06
    • 1970-01-01
    • 2017-03-25
    • 2012-10-09
    • 2021-01-21
    • 2019-04-13
    • 2018-11-09
    • 1970-01-01
    相关资源
    最近更新 更多