繼上一篇的內容,今天就來實際操作看看吧
先從調整得分的方式開始
我們用以下搜尋出來的文檔當作範例
{
"_index" : "school_members",
"_type" : "_doc",
"_id" : "_cXz3nQBXV0k7ep9qP8G",
"_score" : 0.2876821,
"_source" : {
"student" : {
"sid" : "s1090103",
"name" : "風間",
"age" : 18,
"class" : "資工一1"
}
}
}
query:
{
"query": {
"function_score": {
"query": {
"bool": {
"must": {
"match": {
"student.name": "風間"
}
}
}
},
"functions": [
{
"filter": {
"term": {
"student.name": "風間"
}
},
"weight": 2
}
]
}
}
}
結果:
{
"_index" : "school_members",
"_type" : "_doc",
"_id" : "_cXz3nQBXV0k7ep9qP8G",
"_score" : 0.5753642,
"_source" : {
"student" : {
"sid" : "s1090103",
"name" : "風間",
"age" : 18,
"class" : "資工一1"
}
}
}
可以觀察到符合student.name=風間的文檔score乘上了weight變成了2倍
可調參數:
1.origin:中心點,當指定字段的值等於基準,score等於原本的score
2.offest:以origin為中心,為他設置一個偏移量offset給定一個範圍,在此範圍內所有的score也都是和origin一樣為原本的score,不指定預設為0
3.scale:衰減率,即是一個文檔指定字段的值從origin下落時,score改變的速度
4.decay:從origin衰減到scale時所得的score,預設為0.5
基準為age=18的query:
{
"query": {
"function_score": {
"query": {
"bool": {
"must": {
"match": {
"student.name": "風間"
}
}
}
},
"functions": [
{
"gauss": {
"student.age": {
""
"origin": "18",
"scale": "5",
"decay": "0.2"
}
}
}
]
}
}
}
結果:
{
"_index" : "school_members",
"_type" : "_doc",
"_id" : "_cXz3nQBXV0k7ep9qP8G",
"_score" : 0.2876821,
"_source" : {
"student" : {
"sid" : "s1090103",
"name" : "風間",
"age" : 18,
"class" : "資工一1"
}
}
}
跟原本的沒什麼差別
接下來把origin調整成20
結果:
{
"_index" : "school_members",
"_type" : "_doc",
"_id" : "_cXz3nQBXV0k7ep9qP8G",
"_score" : 0.22237073,
"_source" : {
"student" : {
"sid" : "s1090103",
"name" : "風間",
"age" : 18,
"class" : "資工一1"
}
}
}
可以看到score衰減了一點點
可調參數:
field:要乘上的字段
query:
{
"query": {
"function_score": {
"query": {
"bool": {
"must": {
"term": {
"student.name": "風間"
}
}
}
},
"functions": [
{
"field_value_factor": {
"field": "student.age"
}
}
]
}
}
}
結果:
{
"_index" : "school_members",
"_type" : "_doc",
"_id" : "_cXz3nQBXV0k7ep9qP8G",
"_score" : 5.1782775,
"_source" : {
"student" : {
"sid" : "s1090103",
"name" : "風間",
"age" : 18,
"class" : "資工一1"
}
}
}
可以看到原本的score乘上了age(*18)變成了5.178...
random_score加強函數除了能隨機得到一個0~1 的分數,也會使用一個seed 值,來保障生成隨機的順序,當seed 值相同時,生成的隨機結果是一致的
另外從7.0開始需要提供一個字段,該字段可以為生成的隨機種子增加唯一性,不指定默認為id
原文檔
[
{
"_index" : "school_members",
"_type" : "_doc",
"_id" : "-8Xz3nQBXV0k7ep9qP8G",
"_score" : 0.6931471,
"_source" : {
"student" : {
"sid" : "s1090101",
"name" : "王小明",
"age" : 18,
"class" : "資工一1"
}
}
},
{
"_index" : "school_members",
"_type" : "_doc",
"_id" : "_cXz3nQBXV0k7ep9qP8G",
"_score" : 0.2876821,
"_source" : {
"student" : {
"sid" : "s1090103",
"name" : "風間",
"age" : 18,
"class" : "資工一1"
}
}
},
{
"_index" : "school_members",
"_type" : "_doc",
"_id" : "_sXz3nQBXV0k7ep9qP8G",
"_score" : 0.2876821,
"_source" : {
"student" : {
"sid" : "s1090104",
"name" : "小新",
"age" : 18,
"class" : "資工一1"
}
}
}
]
query
{
"query": {
"function_score": {
"query": {
"bool": {
"must": {
"term": {
"student.class": "資工一1"
}
}
}
},
"functions": [
{
"random_score": {
"seed": 100,
"field": "student.name"
}
}
]
}
}
}
結果
[
{
"_index" : "school_members",
"_type" : "_doc",
"_id" : "-8Xz3nQBXV0k7ep9qP8G",
"_score" : 0.6600295,
"_source" : {
"student" : {
"sid" : "s1090101",
"name" : "王小明",
"age" : 18,
"class" : "資工一1"
}
}
},
{
"_index" : "school_members",
"_type" : "_doc",
"_id" : "_cXz3nQBXV0k7ep9qP8G",
"_score" : 0.24322112,
"_source" : {
"student" : {
"sid" : "s1090103",
"name" : "風間",
"age" : 18,
"class" : "資工一1"
}
}
},
{
"_index" : "school_members",
"_type" : "_doc",
"_id" : "_sXz3nQBXV0k7ep9qP8G",
"_score" : 0.083241574,
"_source" : {
"student" : {
"sid" : "s1090104",
"name" : "小新",
"age" : 18,
"class" : "資工一1"
}
}
}
]
可以看到順序沒變但score全都變了
今天的文章就到這邊,剩下的範例就留給明天吧