博客
关于我
【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. 点赞数更平滑

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

          • 通过function_score中的functions数组,结合linearexpgauss函数,对具体字段进行评分衰减。
        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/

    你可能感兴趣的文章
    PIL.Image进行图像融合显示(Image.blend)
    查看>>
    pilicat-dfs 霹雳猫-分布式文件系统
    查看>>
    Pillow lacks the JPEG 2000 plugin
    查看>>
    SpringBoot之ElasticsearchRestTemplate常用示例
    查看>>
    ping 全网段CMD命令
    查看>>
    ping 命令的七种用法,看完瞬间成大神
    查看>>
    Pinia入门(快速上手)
    查看>>
    Pinia:$patch的使用场景
    查看>>
    Pinia:$subscribe()的使用场景
    查看>>
    Pinpoint对Kubernetes关键业务模块进行全链路监控
    查看>>
    Pinterest 大规模缓存集群的架构剖析
    查看>>
    pintos project (2) Project 1 Thread -Mission 1 Code
    查看>>
    PinYin4j库的使用
    查看>>
    PIP
    查看>>
    pip install goose-extractor // SyntaxError: Missing parentheses in call to 'print'
    查看>>
    pip install mysqlclient报错
    查看>>
    pip install 出现报asciii码错误的解决
    查看>>
    pip throws TypeError: parse() got an unexpected keyword argument ‘transport_encoding‘ 在尝试安装新软件包时
    查看>>
    pip 下载慢
    查看>>
    pip 升级报错AttributeError: ‘NoneType’ object has no attribute ‘bytes’
    查看>>