博客
关于我
【Elasticsearch】相关度控制
阅读量:483 次
发布时间:2019-03-07

本文共 3504 字,大约阅读时间需要 11 分钟。

一. 数据导入

本文中提到了一些数据集下载链接,包括通过CSN下载测试数据集和通过ClueBenchmark搜索更多数据集。这些链接可能包含丰富的文档资料,可供数据分析和测试使用。


二. 创建索引

创建Elasticsearch索引时,需注意以下设置和映射:

  • 索引设置:

    # GET http://192.168.16.128:9200/es_news/_settings{    "number_of_shards": 1,    "number_of_replicas": 0}
  • 映射定义:

    # POST http://192.168.16.128:9200/es_news/_mapping{    "dynamic": true,    "properties": {        "id": {            "type": "long"        },        "title": {            "type": "text",            "analyzer": "ik_max_word",            "search_analyzer": "ik_smart"        },        "content": {            "type": "text",            "analyzer": "ik_smart"        },        "shortname": {            "type": "text",            "analyzer": "ik_max_word"        },        "location": {            "type": "geo_point"        }    }}
  • 接着,可以通过POST请求添加自定义数据:

    # POST http://192.168.16.128:9200/es_news/_doc{    "title": "我中了一个奖品",    "content": "奖品内容是苹果电脑",    "location": "88.884874,29.263792",    "shortname": "日喀则"}

    三. 相关性评分

    每个文档都有一个相关性评分字段_score,其计算基于TF/IDF算法,具体包括以下部分:

  • 检索词频率(TF):

    • 检索词在文档中出现的频率越高,相关性评分越高。例如,“honeymoon”在tweet字段的频率越高,相关性越高。
  • 反向文档频率(IDF):

    • 检索词在整个索引中出现的频率越低,相关性评分越高。例如,“honeymoon”在索引中出现次数越少,相关性越高。
  • 字段长度准则:

    • 字段长度越短,相关性评分的贡献越高。例如,在tweet字段中,内容越简短,相关性评分越高。
  • 通过设置explain: true可以详细查看评分计算过程:

    # GET http://192.168.16.128:9200/es_news/_search{    "explain": "true",    "query": {        "match": {            "content": "奖品"        }    }}

    四. 打分控制

    为了满足具体需求,可以对评分进行更详细的控制:

  • 字段权重调整:通过title^2等方式,可以在title字段和content字段之间调整权重。
  • {    "query": {        "multi_match": {            "query": "奖品",            "fields": ["content", "title^2"]        }    }}
    1. 点赞数打分:使用function-score进行评分调整:
    2. # PUT http://blogposts/post/1{    "title": "关于热度",    "content": "在这篇文章中我们将讨论……",    "votes": 6}
      # GET http://blogposts/post/_search{    "query": {        "function_score": {            "query": {                "multi_match": {                    "query": "popularity",                    "fields": ["title", "content"]                }            },            "field_value_factor": {                "field": "votes",                "modifier": "log1p"            }        }    }}
      1. 点赞数平滑处理:通过modifier进行平滑:
      2. {    "query": {        "function_score": {            "query": {                "multi_match": {                    "query": "popularity",                    "fields": ["title", "content"]                }            },            "field_value_factor": {                "field": "votes",                "modifier": "log1p",                "factor": 2            }        }    }}
        1. 点赞数更平滑:

          • 使用log1p或square等修饰语,以平滑评分变化。
        2. 更精细的控制:

          • 通过function_score中的functions数组,结合linear、exp或gauss函数,对具体字段进行评分衰减。
        3. # GET http://_search{    "query": {        "function_score": {            "functions": [                {                    "gauss": {                        "location": {                            "origin": {                                "lat": 51.5,                                "lon": 0.12                            },                            "offset": "2km",                            "scale": "3km"                        }                    }                },                {                    "gauss": {                        "price": {                            "origin": "50",                            "offset": "50",                            "scale": "20"                        }                    }                },                {                    "weight": 2                }            ]        }    }}

          通过以上方法,可以根据具体需求灵活调整Elasticsearch的相关性评分,以优化搜索结果。

    转载地址:http://zygdz.baihongyu.com/

    你可能感兴趣的文章
    python3 grpc安装
    查看>>
    python3 mysql 封装多表_python3 mysql-多表查询
    查看>>
    python3 mysql8_Python MySQL8.0 错误:authentication plugin 'calling_sha2_password' is not supported....
    查看>>
    Python关键代码总览!从Zero到Hero必看的考点
    查看>>
    python3 robotframework+pycharm框架搭建
    查看>>
    Python3 shutil模块
    查看>>
    Python关于strftime函数详细解析 附实战代码
    查看>>
    Python3 tutorial day3
    查看>>
    python3 week4
    查看>>
    Python3 创建数组
    查看>>
    python3 套接字异常(a byte-like object is required not str)
    查看>>
    python3 实现用户三次输入错误记录黑名单并对该用户登录锁定-day1
    查看>>
    python3 文本变图片
    查看>>
    Python3 查看 com 组件的属性
    查看>>
    python3 案例分享--发邮件
    查看>>
    python3 模板库_Python3 标准库概览_Python教程_我爱模板网 - 提供下载各种免费建站资源,免费网站模板,免费网页特效,让你爱上建站!...
    查看>>
    Python3 求最大/小值及索引值 Numpy
    查看>>
    python3+django使用celery执行某些任务失败的解决方案
    查看>>
    Python3+Requests+Unittest实战系列【一】
    查看>>
    python3+requests+unittest实战系列【一】
    查看>>