先准备数据和索引,在ES插入三笔数据,其中language是keywork类型,like是integer类型(代表点赞量)
{ "language": "java", "like": 5 }
{ "language": "python", "like": 5 }
{ "language": "go", "like": 10 }
functions是一个数组,裡面放著的是将要被使用的加强函数列表,我们在裡面使用了3个filter去过滤数据,并且每个filter都设置了一个加强函数,并且还使用了一个会应用到所有文档的field_value_factor加强函数
可以为列表裡的每个加强函数都指定一个filter,这样做的话,只有在文档满足此filter的要求,此filter的加强函数才会应用到文挡上,也可以不指定filter,这样的话此加强函数就会应用到全部的文挡上
一个文档可以一次满足多条加强函数和多个filter,如果一次满足多个,那麽就会产生多个加强score
因此ES会先使用score_mode定义的方式来合併这些加强score们,得到一个总加强score,得到总加强score之后,才会再使用boost_mode定义的方式去和old_score做合併
GET 127.0.0.1/mytest/doc/_search
{
"query": {
"function_score": {
"query": {
"match_all": {} //match_all查出来的所有文档的_score都是1
},
"functions": [
//第一个filter(使用weight加强函数),如果language是java,加强score就是2
{
"filter": {
"term": {
"language": "java"
}
},
"weight": 2
},
//第二个filter(使用weight加强函数),如果language是go,加强score就是3
{
"filter": {
"term": {
"language": "go"
}
},
"weight": 3
},
//第三个filter(使用weight加强函数),如果like数大于等于10,加强score就是5
{
"filter": {
"range": {
"like": {
"gte": 10
}
}
},
"weight": 5
},
//field_value_factor加强函数,会应用到所有文档上,加强score就是like值
{
"field_value_factor": {
"field": "like"
}
}
],
"score_mode": "multiply", //设置functions裡面的加强score们怎麽合併成一个总加强score
"boost_mode": "multiply" //设置old_score怎麽和总加强score合併
}
}
}
"hits": [
{
"_score": 150, //go同时满足filter2、filter3,且还有一个加强函数field_value_factor产生的加强,因此加强score为3, 5, 10(like的值是10),总加强score为3*5*10=150
"_source": { "language": "go", "like": 10 }
},
{
"_score": 10, //java只满足filter1,但是因为还有field_value_facotr产生的加强score,因此加强score为2, 5,总加强score为2*5=10
"_source": { "language": "java", "like": 5 }
},
{
"_score": 5, //python不满足任何filter,因此加强score只有field_value_factor的like值,就是5
"_source": { "language": "python", "like": 5 }
}
]
其实weight加强函数也是可以不和filter搭配,自己单独使用的,只是这样做没啥意义,因为只是会给全部的文档都增加一个固定值而已
GET 127.0.0.1/mytest/doc/_search
{
"query": {
"function_score": {
"query": {
"match_all": {}
}
},
functions: [
{
"weight": 3
}
]
}
}
"hits": [
{
"_score": 3,
"_source": { "language": "go", "like": 10 }
},
{
"_score": 3,
"_source": { "language": "python", "like": 5 }
},
{
"_score": 3,
"_source": { "language": "java", "like": 5 }
}
]
weight加强函数也可以用来调整每个语句的贡献度,权重weight的默认值是1.0,当设置了weight,这个weight值会先和自己那个{}裡的每个句子的评分相乘,之后再通过score_mode和其他加强函数合併
GET 127.0.0.1/mytest/doc/_search
{
"query": {
"function_score": {
"query": {
"match_all": {}
}
},
functions: [
{
"field_value_factor": {
"field": "like"
},
"weight": 3 //weight1, 加强field_value_factor,只能使用乘法,无法改变
},
{
"weight": 20 //weight2
}
],
"score_mode": "sum"
}
}
"hits": [
{
"_score": 50,
"_source": { "language": "go", "like": 10 }
},
{
"_score": 35,
"_source": { "language": "python", "like": 5 }
},
{
"_score": 35,
"_source": { "language": "java", "like": 5 }
}
]
现有如下电影数据。查询title包含lord
或者ring
,如果tags字段(数组)包含death of a friend
短语,那么提高权重
PUT movie_data/_bulk
{"index":{}}
{"revenue":0,"release_date":"2008-07-16T06:00:00.000Z","minutes":130,"movie_id":"15067","title":"The Good, The Bad, The Weird","tags":["gunslinger","gun"],"budget":10000000,"avg_score":7.1}
{"index":{}}
{"revenue":871368364,"release_date":"2001-12-18T07:00:00.000Z","minutes":178,"movie_id":"120","title":"The Lord of the Rings: The Fellowship of the Ring","tags":["elves","dwarves","orcs","middle-earth (tolkien)","hobbit","based on novel","mountains","fireworks","castle","volcano","password","death of a friend","uncle","mirror","wizard","sword and sorcery"],"budget":93000000,"avg_score":8}
{"index":{}}
{"revenue":1118888979,"release_date":"2003-12-01T07:00:00.000Z","minutes":201,"movie_id":"122","title":"The Lord of the Rings: The Return of the King","tags":["elves","orcs","middle-earth (tolkien)","based on novel","suspicion","bravery","war","honor","troll","brutality","violence","ghost","end of trilogy","quest","sword and sorcery"],"budget":94000000,"avg_score":8.1}
{"index":{}}
{"revenue":15304890,"release_date":"2006-09-06T06:00:00.000Z","minutes":96,"movie_id":"1381","title":"The Fountain","tags":["brain tumor","operation","queen","love of one's life","surgeon","tree","dying and death","transience","immortality","maya civilization","monkey","conquest"],"budget":35000000,"avg_score":6.8}
{"index":{}}
{"revenue":0,"release_date":"2000-09-08T06:00:00.000Z","minutes":96,"movie_id":"10685","title":"The Watcher","tags":["chicago","fbi","menace","covered investigation","state of emergency","investigation","suspense","serial killer","little girl","psychiatrist","fbi agent","psychotherapy"],"budget":33000000,"avg_score":4.9}
{"index":{}}
{"revenue":54700105,"release_date":"2004-04-15T06:00:00.000Z","minutes":124,"movie_id":"7220","title":"The Punisher","tags":["chain","submachine gun","undercover","smuggling","twin brother","marvel comic","one man army","massacre","extreme violence","family reunion","pier"],"budget":33000000,"avg_score":6.1}
{"index":{}}
{"revenue":78636257,"release_date":"2016-04-07T06:00:00.000Z","minutes":91,"movie_id":"323676","title":"The Boss","tags":["business woman","ex-con","duringcreditsstinger","girl scouts"],"budget":29000000,"avg_score":5.5}
{"index":{}}
{"revenue":115922175,"release_date":"2013-08-16T06:00:00.000Z","minutes":132,"movie_id":"132363","title":"The Butler","tags":["white house","butler","biography","civil rights"],"budget":25000000,"avg_score":7.2}
{"index":{}}
{"revenue":24902723,"release_date":"2015-08-06T06:00:00.000Z","minutes":120,"movie_id":"293646","title":"The 33","tags":["mining","chile","based on true story","survival","woman director","mining accident"],"budget":25000000,"avg_score":6}
{"index":{}}
{"revenue":85446075,"release_date":"2012-08-30T06:00:00.000Z","minutes":92,"movie_id":"77883","title":"The Possession","tags":[],"budget":14000000,"avg_score":5.8}
{"index":{}}
{"revenue":71009334,"release_date":"2013-10-25T06:00:00.000Z","minutes":117,"movie_id":"109091","title":"The Counselor","tags":["poetry","lawyer","drug smuggling","drug trafficking","red dress"],"budget":25000000,"avg_score":5}
{"index":{}}
{"revenue":43928932,"release_date":"2002-10-02T06:00:00.000Z","minutes":92,"movie_id":"4108","title":"The Transporter","tags":["car journey","transportation","auto","human trafficking"],"budget":21000000,"avg_score":6.6}
{"index":{}}
{"revenue":0,"release_date":"2009-10-16T06:00:00.000Z","minutes":101,"movie_id":"19904","title":"The Stepfather","tags":["step father","remake"],"budget":0,"avg_score":5.4}
{"index":{}}
{"revenue":75597042,"release_date":"2002-03-25T07:00:00.000Z","minutes":127,"movie_id":"14635","title":"The Rookie","tags":["father son relationship","baseball","sports team","sport","life's dream","growing up"],"budget":20000000,"avg_score":6.5}
{"index":{}}
{"revenue":0,"release_date":"2003-12-12T07:00:00.000Z","minutes":120,"movie_id":"41488","title":"The Statement","tags":[],"budget":0,"avg_score":5.9}
{"index":{}}
{"revenue":29657751,"release_date":"2012-03-09T07:00:00.000Z","minutes":111,"movie_id":"70436","title":"The Raven","tags":["poison","blackmail","masked ball","historical figure","buried alive","serial killer","deadline","edgar allan poe","newspaper review","baltimore maryland","newspaper office","hard times","life imitates art","pendulum"],"budget":26000000,"avg_score":6.1}
{"index":{}}
{"revenue":124272124,"release_date":"2011-08-09T06:00:00.000Z","minutes":146,"movie_id":"50014","title":"The Help","tags":["mississippi","based on novel","exploitation","racial segregation","racism","writer","maid","moral courage","ressentiment","southern belle","racial issues","1960s","newspaper columnist"],"budget":25000000,"avg_score":7.8}
{"index":{}}
{"revenue":0,"release_date":"2002-09-27T06:00:00.000Z","minutes":98,"movie_id":"10771","title":"The Tuxedo","tags":["bomb","intelligence","chauffeur","wound","secret agent","head injury"],"budget":0,"avg_score":5.3}
{"index":{}}
{"revenue":69700000,"release_date":"2000-07-07T06:00:00.000Z","minutes":104,"movie_id":"4244","title":"The Kid","tags":["age difference","midlife crisis","suppressed past","self-awareness","childhood memory","humor","changing the past or future"],"budget":65000000,"avg_score":6}
{"index":{}}
{"revenue":179379533,"release_date":"2012-05-15T06:00:00.000Z","minutes":83,"movie_id":"76493","title":"The Dictator","tags":["kurdish"],"budget":65000000,"avg_score":5.9}
答案
GET movie_data/_search
{
"query": {
"function_score": {
"query": {
"terms": {
"title": [
"lord",
"ring"
]
}
},
"functions": [
{
"filter": {
"match_phrase": {
"tags": "death of a friend"
}
},
"weight": 2
}
],
"boost_mode": "max"
}
}
}
主要参考:https://blog.csdn.net/weixin_40341116/article/details/80931573