【问题标题】:In Elasticsearch how to give more priority to certain words in a phrase在 Elasticsearch 中,如何为短语中的某些单词赋予更多优先级
【发布时间】:2020-06-24 21:58:45
【问题描述】:

我在 Elastic Search 7.4 中有一个名为 product 的索引

产品有一个名为 title 的字段。

如果我想搜索 blue watch 的所有标题,我想查看所有 blue watch 或 watch 的结果。我不想看到像蓝色 T 恤这样的结果。我想优先考虑watch这个词。

即如何确定搜索短语中的名词并赋予其比相应形容词更高的优先级。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    在elasticsearch中使用boost参数
    Boost 参数赋予单词“watch”比“blue”更高的优先级

    {
        "query": {
            "bool": {
                "should": [{
                        "match": {
                            "title": "blue",
                            "boost": 1
    
                        }
                    },
                    {
                        "match": {
                            "title": "watch",
                            "boost": 10
                        }
                    }
                ]
            }
        }
    }
    

    如果你想让“watch”这个词成为一个强制匹配,只需将它放在 must 块中,然后将“blue”放在 should 块中,使其成为可选

    {
        "query": {
            "bool": {
                "must": {
                    "match": {
                        "title": "watch",
                        "boost": 10
                    }
                },
                "should": {
                    "match": {
                        "title": "blue",
                        "boost": 1
    
                    }
                }
    
    
            }
        }
    }
    

    【讨论】:

    • 如果你想在你的查询中找到一个名词并添加更高的提升,它应该在你的应用程序级别完成。您的服务器代码(例如 python-spacy)可用于识别名词,您可以显式为其分配 boost
    猜你喜欢
    • 1970-01-01
    • 2018-09-12
    • 2012-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 2023-02-13
    相关资源
    最近更新 更多