ElasticSearch BM-25自定义权重

ElasticSearch BM-25

https://www.cnblogs.com/novwind/p/15177871.html

通过kibana中配置explain属性后,可以看出来每个文档的得分由:
score = boost * idf *tf
组成。

 "_explanation" : {
          "value" : 1.3767608,
          "description" : "sum of:",
          "details" : [
            {
              "value" : 1.0121177,
              "description" : "max of:",
              "details" : [
                {
                  "value" : 1.0121177,
                  "description" : "weight(title:student in 0) [PerFieldSimilarity], result of:",
                  "details" : [
                    {
                      "value" : 1.0121177,
                      "description" : "score(freq=1.0), computed as boost * idf * tf from:",
                      "details" : [
                        {
                          "value" : 3.3000002,
                          "description" : "boost",
                          "details" : [ ]
                        },
                        {
                          "value" : 0.6931472,
                          "description" : "idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:",
                          "details" : [
                            {
                              "value" : 2,
                              "description" : "n, number of documents containing term",
                              "details" : [ ]
                            },
                            {
                              "value" : 4,
                              "description" : "N, total number of documents with field",
                              "details" : [ ]
                            }
                          ]
                        },
                        {
                          "value" : 0.44247788,
                          "description" : "tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:",
                          "details" : [
                            {
                              "value" : 1.0,
                              "description" : "freq, occurrences of term within document",
                              "details" : [ ]
                            },
                            {
                              "value" : 1.2,
                              "description" : "k1, term saturation parameter",
                              "details" : [ ]
                            },
                            {
                              "value" : 0.75,
                              "description" : "b, length normalization parameter",
                              "details" : [ ]
                            },
                            {
                              "value" : 4.0,
                              "description" : "dl, length of field",
                              "details" : [ ]
                            },
                            {
                              "value" : 3.75,
                              "description" : "avgdl, average length of field",
                              "details" : [ ]
                            }
                          ]
                        }
                      ]
                    }
                  ]
                },
                {
                  "value" : 0.18794037,
                  "description" : "weight(content:student in 0) [PerFieldSimilarity], result of:",
                  "details" : [
                    {
                      "value" : 0.18794037,
                      "description" : "score(freq=5.0), computed as boost * idf * tf from:",
                      "details" : [
                        {
                          "value" : 2.2,
                          "description" : "boost",
                          "details" : [ ]
                        },
                        {
                          "value" : 0.105360515,
                          "description" : "idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:",
                          "details" : [
                            {
                              "value" : 4,
                              "description" : "n, number of documents containing term",
                              "details" : [ ]
                            },
                            {
                              "value" : 4,
                              "description" : "N, total number of documents with field",
                              "details" : [ ]
                            }
                          ]
                        },
                        {
                          "value" : 0.8108108,
                          "description" : "tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:",
                          "details" : [
                            {
                              "value" : 5.0,
                              "description" : "freq, occurrences of term within document",
                              "details" : [ ]
                            },
                            {
                              "value" : 1.2,
                              "description" : "k1, term saturation parameter",
                              "details" : [ ]
                            },
                            {
                              "value" : 0.75,
                              "description" : "b, length normalization parameter",
                              "details" : [ ]
                            },
                            {
                              "value" : 13.0,
                              "description" : "dl, length of field",
                              "details" : [ ]
                            },
                            {
                              "value" : 13.5,
                              "description" : "avgdl, average length of field",
                              "details" : [ ]
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            },
            {
              "value" : 0.36464313,
              "description" : "weight(EntityList:student in 0) [PerFieldSimilarity], result of:",
              "details" : [
                {
                  "value" : 0.36464313,
                  "description" : "score(freq=1.0), computed as boost * idf * tf from:",
                  "details" : [
                    {
                      "value" : 4.4,
                      "description" : "boost",
                      "details" : [ ]
                    },
                    {
                      "value" : 0.18232156,
                      "description" : "idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:",
                      "details" : [
                        {
                          "value" : 2,
                          "description" : "n, number of documents containing term",
                          "details" : [ ]
                        },
                        {
                          "value" : 2,
                          "description" : "N, total number of documents with field",
                          "details" : [ ]
                        }
                      ]
                    },
                    {
                      "value" : 0.45454544,
                      "description" : "tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:",
                      "details" : [
                        {
                          "value" : 1.0,
                          "description" : "freq, occurrences of term within document",
                          "details" : [ ]
                        },
                        {
                          "value" : 1.2,
                          "description" : "k1, term saturation parameter",
                          "details" : [ ]
                        },
                        {
                          "value" : 0.75,
                          "description" : "b, length normalization parameter",
                          "details" : [ ]
                        },
                        {
                          "value" : 1.0,
                          "description" : "dl, length of field",
                          "details" : [ ]
                        },
                        {
                          "value" : 1.0,
                          "description" : "avgdl, average length of field",
                          "details" : [ ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }

其中的boost = 2.2是下面公式中的k+1 = 1.2 + 1 =2.2组成,当我们在某个字段中加权时乘的是该值,例如我在两个字段,title和content字段中查询,并且title字段加权为1.1,则title字段中的得分会1.1*2.2 = 2.46。elasticsearch中通过multi-match查询多个字段时最后的排序会根据得分值为最高的字段的值进行排序。
Elasticsearch bm25算法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用 Elasticsearch 中的 IK 分词器进行停词和自定义词库的步骤: 1. 安装 Elasticsearch 和 IK 分词器插件。 2. 创建自定义词库文件,格式如下: ``` # 单个词语 词语1 词语2 ... # 带有词性的词语 词语1,词性1 词语2,词性2 ... ``` 注:词性可以不写,用逗号隔开。 3. 将自定义词库文件放置在 Elasticsearch 安装目录下的 `plugins/ik/config/` 目录下。 4. 修改 IK 分词器配置文件,指定停用词文件和自定义词库文件,示例如下: ``` { "index": { "analysis": { "analyzer": { "my_analyzer": { "type": "custom", "tokenizer": "ik_max_word", "filter": [ "my_stopwords", "my_synonyms" ] } }, "filter": { "my_stopwords": { "type": "stop", "stopwords_path": "stopwords.txt" }, "my_synonyms": { "type": "synonym", "synonyms_path": "synonyms.txt" } } } } } ``` 注:上面示例中使用了停用词文件和同义词文件,可以根据需要自行配置。 5. 创建索引并指定使用自定义分词器。 ``` PUT /my_index { "settings": { "analysis": { "analyzer": { "my_analyzer": { "type": "custom", "tokenizer": "ik_max_word", "filter": [ "my_stopwords", "my_synonyms" ] } }, "filter": { "my_stopwords": { "type": "stop", "stopwords_path": "stopwords.txt" }, "my_synonyms": { "type": "synonym", "synonyms_path": "synonyms.txt" } } } }, "mappings": { "properties": { "my_field": { "type": "text", "analyzer": "my_analyzer" } } } } ``` 6. 测试分词效果。 可以使用以下命令测试分词效果: ``` GET /my_index/_analyze { "analyzer": "my_analyzer", "text": "自定义分词器测试" } ``` 上述命令会返回分词结果,可以根据需要调整自定义词库和停用词文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值