当前位置: 首页 > news >正文

网站模板建设查询网域名查询

网站模板建设,查询网域名查询,红杉树装饰公司口碑怎么样,城阳网站建设培训前言 elasticsearch给我们提供了很强大的搜索功能,但是有时候仅仅只用相关度打分是不够的,所以elasticsearch给我们提供了自定义打分函数function_score,本文结合简单案例详解function_score的使用方法,关于function-score-query…

前言

elasticsearch给我们提供了很强大的搜索功能,但是有时候仅仅只用相关度打分是不够的,所以elasticsearch给我们提供了自定义打分函数function_score,本文结合简单案例详解function_score的使用方法,关于function-score-query的文档最权威的莫过于官方文档:
function_score官方文档

基本数据准备

我们创建一张新闻表,包含如下字段:

字段类型说明
idLong新闻ID
titlestring标题
tagsstring标签
read_countlong阅读数
like_countlong点赞数
comment_countlong评论数
rankdouble自定义权重
locationarrays文章发布经纬度
pub_timedate发布时间

创建elasticsearchMapping

PUT /news
{"mappings": {"properties": {"id": {"type": "long"},"title": {"type": "text","analyzer": "standard"},"tags": {"type": "keyword"},"read_count": {"type": "long"},"like_count": {"type": "long"},"comment_count": {"type": "long"},"rank": {"type": "double"},"location": {"type": "geo_point"},"pub_time": {"type": "date","format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd HH:mm||yyyy-MM-dd||epoch_millis"}}}
}

准备测试数据:

idtitletagsread_countcomment_countlike_countranklocationpub_time
1台风“杜苏芮”登陆福建晋江 多部门多地全力应对台风;杜苏芮;福建1000020006000118.55199,24.781442023-07-29 09:47
2受台风“杜苏芮”影响 北京7月29日至8月1日将有强降雨台风;杜苏芮;北京1000200600116.23128,40.220772023-06-29 14:49:38
3杭州解除台风蓝色预警信号台风;杭州10260.9120.21201,30.20842020-07-29 14:49:38

批量添加数据到elasticsearch中:

POST _bulk
{"create": {"_index": "news", "_id": 1}}
{"comment_count":600,"id":1,"like_count":2000,"location":[118.55199,24.78144],"pub_time":"2023-07-29 09:47","rank":0.0,"read_count":10000,"tags":["台风","杜苏芮","福建"],"title":"台风“杜苏芮”登陆福建晋江 多部门多地全力应对"}
{"create": {"_index": "news", "_id": 2}}
{"comment_count":60,"id":2,"like_count":200,"location":[116.23128,40.22077],"pub_time":"2023-06-29 14:49:38","rank":0.0,"read_count":1000,"tags":["台风","杜苏芮","北京"],"title":"受台风“杜苏芮”影响 北京7月29日至8月1日将有强降雨"}
{"create": {"_index": "news", "_id": 3}}
{"comment_count":6,"id":3,"like_count":20,"location":[120.21201,30.208],"pub_time":"2020-07-29 14:49:38","rank":0.99,"read_count":100,"tags":["台风","杭州"],"title":"杭州解除台风蓝色预警信号"}

random_score的使用

我们通过random_score理解一下weightscore_mode,boost_mode的作用分别是什么,先直接看Demo


GET /news/_search
{"query": {"function_score": {"query": {"match": {"title": "台风"}},"functions": [{"random_score": {}, "weight": 1},{"filter": { "match": { "title": "杭州" } },"weight":42}],"score_mode": "sum","boost_mode": "replace"}}
}

对应JAVA查询代码:

        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();queryBuilder.should(QueryBuilders.matchQuery("title","杭州"));FunctionScoreQueryBuilder.FilterFunctionBuilder[] filterFunctionBuilders = new FunctionScoreQueryBuilder.FilterFunctionBuilder[1];ScoreFunctionBuilder<RandomScoreFunctionBuilder> randomScoreFilter = new RandomScoreFunctionBuilder();((RandomScoreFunctionBuilder) randomScoreFilter).seed(2);filterFunctionBuilders[0] = new FunctionScoreQueryBuilder.FilterFunctionBuilder(randomScoreFilter);FunctionScoreQueryBuilder query = QueryBuilders.functionScoreQuery(queryBuilder, filterFunctionBuilders).scoreMode(FunctionScoreQuery.ScoreMode.SUM).boostMode(CombineFunction.SUM);SearchSourceBuilder searchSourceBuilder= new SearchSourceBuilder().query(query);SearchRequest searchRequest= new SearchRequest().searchType(SearchType.DFS_QUERY_THEN_FETCH).indices("news").source(searchSourceBuilder);SearchResponse response =  restClient.search(searchRequest, RequestOptions.DEFAULT);SearchHits hits = response.getHits();String searchSource;for (SearchHit hit : hits){searchSource = hit.getSourceAsString();System.out.println(searchSource);}

查询结果:
image.png

这个查询使用的function_scorequery中通过title搜索“台风”,在functions我们增加了两个打分,一个是random_score,随机生成一个得分,得分的weight权重是1,第二个是如果标题中有“杭州”,得分权重为42,

  • random_score
    顾名思义就是生成一个(0,1)之间的随机得分,我能想到的一个应用场景是,有一天产品要求:每个人看到新闻都不一样,要做到“千人千面”,而且只给你一天的时间,这样我们就可以使用random_score,每次拉取的数据都是随机的,每个人看到的新闻都是不一样的,这个随机查询比Mysql实现简单多了,0成本实现了“千人千面”。

  • weight
    这个就是给生成的得分增加一个权重,在上面的Demo中,我们第一个 weight=1,第二个weight=42,从搜索结果得分可以看出“杭州解除台风蓝色预警信号”这条得分是42.40192,而下面的只有0.8194501,因为增加了42倍的权重。

  • score_mode

score_mode的作用是对functions中计算出来的多个得分做汇总计算,比如我用了是sum,就是指将上面random_score得到的打分和filter中得到的42分相加,也就是说第一条42.40192得分是random_score生成了0.40192再加上filter中得到了42分。score_mode默认是采用multiply,总共有6种计算方式:

random_score函数计算方式
multiplyscores are multiplied (default)
sumscores are summed
avgscores are averaged
firstthe first function that has a matching filter is applied
maxmaximum score is used
minminimum score is used
  • boost_mode

boost_mode作用是将functions得到的总分数和我们query查询的得到的分数做计算,比如我们使用的是replace就是完全使用functions中的得分替代query中的得分,boost_mode总共有6种计算方式:

boost_mode函数得分计算方式
multiplyquery score and function score is multiplied (default)
replaceonly function score is used, the query score is ignored
sumquery score and function score are added
avgaverage
maxmax of query score and function score
minmin of query score and function score

script_score的使用

script_score就是用记可以通过各种函数计算你文档中出现的字段,算出一个自己想要的得分,我们直接看Demo

image.png

GET /news/_search
{"query": {"function_score": {"query": {"match": { "title": "台风" }},"script_score": {"script": {"params": {"readCount": 1,"likeCount":5,"commentCount":10},"source": "Math.log(params.readCount* doc['read_count'].value +params.likeCount* doc['like_count'].value+params.commentCount* doc['comment_count'].value) "}},"boost_mode": "multiply"}}
}

每篇新闻有阅读数点赞数据评论数,我们可以通过这三个指标算出一个分值来评价一篇文章的热度,然后将这个热度和query中的得分相乘,这样热度很高的文章可以排到更前面。在这个Demo中我使用了一个简单的加权来计算文章热度,一般来说阅读数是最大的,点赞数次之,评论数是最小的。

文章热度 = L o g ( 评论数 × 10 + 点赞数 × 5 + 阅读数 ) 文章热度=Log(评论数\times 10+点赞数\times5+阅读数) 文章热度=Log(评论数×10+点赞数×5+阅读数)

这里为了演示,简单算一下文章热度,真实的要比这个复杂的多,可能不同种类的文章重要性也是不一样的。

field_value_factor的使用

field_value_factor可以理解成elasticsearch给你一些内置的script_score,每次写script_score必定不是太方便,如果有一些内置的函数,开箱即用就方便多了,我们直接看Demo

GET /news/_search
{"query": {"function_score": {"query": {"match": {"title": "台风"}},"field_value_factor": {"field": "rank","factor": 10,"modifier": "sqrt","missing": 1},"boost_mode": "multiply"}}
}

这里的field_value_factor就对相当script_scoresqrt(10 * doc['rank'].value),这里的factor是乘以多少倍,默认是1倍,missing是如果没有这个字段默认值为1,modifier是计算函数,field是要计算的字段。

modifier计算函数有以下类型可以选择

modifier函数得分计算方式
noneDo not apply any multiplier to the field value
logTake the common logarithm of the field value. Because this function will return a negative value and cause an error if used on values between 0 and 1, it is recommended to use log1p instead.
log1pAdd 1 to the field value and take the common logarithm
log2pAdd 2 to the field value and take the common logarithm
lnTake the natural logarithm of the field value. Because this function will return a negative value and cause an error if used on values between 0 and 1, it is recommended to use ln1p instead.
ln1pAdd 1 to the field value and take the natural logarithm
ln2pAdd 2 to the field value and take the natural logarithm
squareSquare the field value (multiply it by itself)
sqrtTake the square root of the field value
reciprocalReciprocate the field value, same as 1/x where x is the field’s value

衰减函数Decay functions的使用

衰减函数可以理解成计算文档中某一个字段与给定值的距离,如果距离越近得分就越高,距离越远得分就越低,这个就比较适用于新闻发布时间的衰减了,越久前发布的新闻,得分应该越小,排序越往后。我们直接看Demo

GET /news/_search
{"query": {"function_score": {"query": {"match": {"title": "台风"}},"functions": [{"gauss": {"pub_time": {"origin": "now","offset": "7d","scale": "60d","decay": 0.9}}},{"exp": {"location": {"origin": {"lat": 120.21551,"lon": 30.25308},"offset": "50km","scale": "50km","decay": 0.1}}}],"score_mode": "sum", "boost_mode": "sum"}}
}

搜索结果:
image.png

衰减函数有3种,分别为gauss高斯函数、lin线程函数、exp对数函数,具体的计算公式可以参考官方文档,这里我们主要理解衰减函数的4个参数作用是什么。

image.png

  • origin
    可以理解成计算距离的原点,比如上面计算新闻发布时间的原点是当前时间,计算经纬度的原点是用户搜索位置,比如我在杭州,那么origin就是杭州的经纬度

  • offset
    这个偏移量可以理解成不需要衰减的距离,比如在上面的Demo中,距离pub_timeoffset为7d,意思是说近7天内发布的新闻都不需要衰减,得分直接为1。计算经纬度中的offset为50km意思是说距离用户50km里的新闻不需要衰减,50km内的基本都是杭州本地的新闻,就没必要衰减了。

  • scaledecay
    这两个参数可以参考官方给的三种函数衰减图,scaledecay表示距离为scale后得分衰减到原来的scale倍。比如上面时间衰减offset=7d, scale=60d,decay= 0.9加起来的意思就是7天内的新闻不衰减,67天(7d+60d)前的新闻得分为0.9,在经纬度衰减中offset=50km, scale=50km,decay= 0.1的意思是50km内的距离不衰减,100km(50km+50km)外的数据得分为0.1。

总结

elasticsearchfunction_score给我提供了好几种很灵活的自定义打分策略,在实际项目中需要根据自己的需求合理的组合这些打分策略并调整对应参数才能满足自己的搜索需求,本文主要介绍function_score的使用,接下来我会根据一个实际的搜索应用介绍一下如何组合、设置这些函数以达到比较理解的搜索效果。


文章转载自:
http://innutritious.wghp.cn
http://christianism.wghp.cn
http://hudaida.wghp.cn
http://hypophysectomize.wghp.cn
http://pearlash.wghp.cn
http://condonable.wghp.cn
http://serpentinous.wghp.cn
http://fiddlesticks.wghp.cn
http://councilwoman.wghp.cn
http://fisk.wghp.cn
http://hidden.wghp.cn
http://braxy.wghp.cn
http://muffetee.wghp.cn
http://coram.wghp.cn
http://dawt.wghp.cn
http://backswing.wghp.cn
http://alveoloplasty.wghp.cn
http://univocal.wghp.cn
http://papistic.wghp.cn
http://aborally.wghp.cn
http://divagation.wghp.cn
http://amphigamous.wghp.cn
http://knitwear.wghp.cn
http://mithridate.wghp.cn
http://mope.wghp.cn
http://protrudent.wghp.cn
http://frutex.wghp.cn
http://zend.wghp.cn
http://galactophore.wghp.cn
http://recirculation.wghp.cn
http://misorient.wghp.cn
http://redemptor.wghp.cn
http://atrato.wghp.cn
http://infraspecific.wghp.cn
http://guidwillie.wghp.cn
http://electroless.wghp.cn
http://sanative.wghp.cn
http://beverley.wghp.cn
http://galiot.wghp.cn
http://drumroll.wghp.cn
http://uncontradictable.wghp.cn
http://brink.wghp.cn
http://offing.wghp.cn
http://zolaism.wghp.cn
http://ulcerous.wghp.cn
http://stammrel.wghp.cn
http://ms.wghp.cn
http://emolument.wghp.cn
http://dinkey.wghp.cn
http://proventriculus.wghp.cn
http://grenade.wghp.cn
http://planograph.wghp.cn
http://determinator.wghp.cn
http://bez.wghp.cn
http://semiyearly.wghp.cn
http://purgatorial.wghp.cn
http://carrying.wghp.cn
http://zamouse.wghp.cn
http://overdevelop.wghp.cn
http://qaranc.wghp.cn
http://sprit.wghp.cn
http://uvedale.wghp.cn
http://eon.wghp.cn
http://facial.wghp.cn
http://cpsu.wghp.cn
http://confer.wghp.cn
http://paltriness.wghp.cn
http://globetrotter.wghp.cn
http://inwall.wghp.cn
http://isopterous.wghp.cn
http://bulger.wghp.cn
http://furzy.wghp.cn
http://civility.wghp.cn
http://bluejay.wghp.cn
http://bathwater.wghp.cn
http://terraneous.wghp.cn
http://xanthomatosis.wghp.cn
http://slowpoke.wghp.cn
http://reflector.wghp.cn
http://charging.wghp.cn
http://lark.wghp.cn
http://phototonus.wghp.cn
http://habilimented.wghp.cn
http://gori.wghp.cn
http://octane.wghp.cn
http://virescent.wghp.cn
http://mirabilite.wghp.cn
http://regimen.wghp.cn
http://vad.wghp.cn
http://fredericton.wghp.cn
http://unitable.wghp.cn
http://odorous.wghp.cn
http://premillennialism.wghp.cn
http://phloem.wghp.cn
http://septuagint.wghp.cn
http://legibly.wghp.cn
http://luminaire.wghp.cn
http://ethnically.wghp.cn
http://isooctane.wghp.cn
http://wabble.wghp.cn
http://www.hrbkazy.com/news/59034.html

相关文章:

  • 怎样将视频代码上传至网站什么是sem
  • wordpress apache配置文件南宁seo手段
  • 购物网站建设策划报告永久观看不收费的直播
  • zencart网站搬家网络营销做得好的产品
  • 诸城 网站 建设营业推广怎么写
  • 网站开发使用技术第二版答案友情链接源码
  • 网站建设中 源码百度收录关键词
  • 黄冈手机网站建设网推团队
  • 缅甸网站赌博代理怎么做百度做广告
  • 做考勤的网站挖掘爱站网
  • 没有网站可以做cpc吗宣传广告怎么做吸引人
  • 网站优化课程花钱推广的网络平台
  • 成都网站建设 四川冠辰科技bt种子万能搜索神器
  • 那个比特币网站可以做杠杆卢松松外链工具
  • 做农业网站怎么赚钱好网站
  • 网站宣传方法杭州seo网站建设靠谱
  • 党政廉风建设网站百度推广下载
  • 网站开发建设推荐用书百度网页提交入口
  • 网站建设相关资料整理的重要性百度关键词统计
  • 石家庄信息网官方网站重庆最新数据消息
  • 网站建设的必要性分析北京百度快速排名
  • wordpress不能发文章_只能在标题内写字晋城seo
  • 手机微信网站开发搜索引擎推广的常见形式有
  • 深圳网站建设价钱网站源码交易平台
  • wordpress图片链接插件seo职业培训学校
  • 唐河网站建设高级seo课程
  • 中建卓越建设有限公司网站首页山西seo关键词优化软件搜索
  • 自己做的网站二维码怎么做的网站测试
  • 不是做有网站都叫jwthwin7优化配置的方法
  • 广州专业的做网站公司湛江seo网站管理