博客
关于我
【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/

    你可能感兴趣的文章
    npm install 卡着不动的解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install 报错 no such file or directory 的解决方法
    查看>>
    npm install报错,证书验证失败unable to get local issuer certificate
    查看>>
    npm install无法生成node_modules的解决方法
    查看>>
    npm install的--save和--save-dev使用说明
    查看>>
    npm node pm2相关问题
    查看>>
    npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
    查看>>
    npm run build报Cannot find module错误的解决方法
    查看>>
    npm run build部署到云服务器中的Nginx(图文配置)
    查看>>
    npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
    查看>>
    npm scripts 使用指南
    查看>>
    npm should be run outside of the node repl, in your normal shell
    查看>>
    npm start运行了什么
    查看>>
    npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
    查看>>
    npm 安装依赖过程中报错:Error: Can‘t find Python executable “python“, you can set the PYTHON env variable
    查看>>
    npm.taobao.org 淘宝 npm 镜像证书过期?这样解决!
    查看>>
    npm—小记
    查看>>
    npm介绍以及常用命令
    查看>>
    NPM使用前设置和升级
    查看>>