繼上一篇的範例,今天就把剩下的幾個例子結束掉吧
今天的例子都用以下的文檔當例子
{
"_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": {
"term": {
"student.name": "風間"
}
}
}
},
"functions": [
{
"filter": {"term": {
"student.name": "風間"
}},
"weight": 2
}
]
}
}
}
加上boost_mode
結果:
{
"_index" : "school_members",
"_type" : "_doc",
"_id" : "_cXz3nQBXV0k7ep9qP8G",
"_score" : 0.5753642,
"_source" : {
"student" : {
"sid" : "s1090103",
"name" : "風間",
"age" : 18,
"class" : "資工一1"
}
}
}
query:
{
"query": {
"function_score": {
"query": {
"bool": {
"must": {
"term": {
"student.name": "風間"
}
}
}
},
"functions": [
{
"filter": {"term": {
"student.name": "風間"
}},
"weight": 2
}
],
"boost_mode": "sum"
}
}
}
結果:
{
"_index" : "school_members",
"_type" : "_doc",
"_id" : "_cXz3nQBXV0k7ep9qP8G",
"_score" : 2.287682,
"_source" : {
"student" : {
"sid" : "s1090103",
"name" : "風間",
"age" : 18,
"class" : "資工一1"
}
}
}
score變成了原本的0.28+加權的2=2.28
結果
{
"_index" : "school_members",
"_type" : "_doc",
"_id" : "_cXz3nQBXV0k7ep9qP8G",
"_score" : 0.2876821,
"_source" : {
"student" : {
"sid" : "s1090103",
"name" : "風間",
"age" : 18,
"class" : "資工一1"
}
}
}
0.28 < 2 所以結果變成0.28
結果:
{
"_index" : "school_members",
"_type" : "_doc",
"_id" : "_cXz3nQBXV0k7ep9qP8G",
"_score" : 2.0,
"_source" : {
"student" : {
"sid" : "s1090103",
"name" : "風間",
"age" : 18,
"class" : "資工一1"
}
}
}
0.28 < 2 所以結果是2
結果:
{
"_index" : "school_members",
"_type" : "_doc",
"_id" : "_cXz3nQBXV0k7ep9qP8G",
"_score" : 2.0,
"_source" : {
"student" : {
"sid" : "s1090103",
"name" : "風間",
"age" : 18,
"class" : "資工一1"
}
}
}
直接把2取代掉0.28
原query:
{
"query": {
"function_score": {
"query": {
"bool": {
"must": {
"term": {
"student.name": "風間"
}
}
}
},
"functions": [
{
"filter": {"term": {
"student.name": "風間"
}},
"weight": 2
},
{
"field_value_factor": {
"field": "student.age"
}
}
],
"score_mode": "multiply"
}
}
}
結果:
{
"_index" : "school_members",
"_type" : "_doc",
"_id" : "_cXz3nQBXV0k7ep9qP8G",
"_score" : 10.356555,
"_source" : {
"student" : {
"sid" : "s1090103",
"name" : "風間",
"age" : 18,
"class" : "資工一1"
}
}
}
score:0.2876 * 2 * 18 = 10.35
結果:
{
"_index" : "school_members",
"_type" : "_doc",
"_id" : "_cXz3nQBXV0k7ep9qP8G",
"_score" : 5.7536416,
"_source" : {
"student" : {
"sid" : "s1090103",
"name" : "風間",
"age" : 18,
"class" : "資工一1"
}
}
}
score:(2 + 18) * 0.2876 = 5.752
結果:
{
"_index" : "school_members",
"_type" : "_doc",
"_id" : "_cXz3nQBXV0k7ep9qP8G",
"_score" : 5.1782775,
"_source" : {
"student" : {
"sid" : "s1090103",
"name" : "風間",
"age" : 18,
"class" : "資工一1"
}
}
}
18 > 2
score: 0.2876 * 18 = 5.17
結果:
{
"_index" : "school_members",
"_type" : "_doc",
"_id" : "_cXz3nQBXV0k7ep9qP8G",
"_score" : 0.5753642,
"_source" : {
"student" : {
"sid" : "s1090103",
"name" : "風間",
"age" : 18,
"class" : "資工一1"
}
}
}
18 > 2
score:0.2876 * 2 = 0.57
結果:
{
"_index" : "school_members",
"_type" : "_doc",
"_id" : "_cXz3nQBXV0k7ep9qP8G",
"_score" : 0.5753642,
"_source" : {
"student" : {
"sid" : "s1090103",
"name" : "風間",
"age" : 18,
"class" : "資工一1"
}
}
}
functions裡面的第一個是weight:2
score:0.2876 * 2 = 0.57
結果:
{
"_index" : "school_members",
"_type" : "_doc",
"_id" : "_cXz3nQBXV0k7ep9qP8G",
"_score" : 1.9178805,
"_source" : {
"student" : {
"sid" : "s1090103",
"name" : "風間",
"age" : 18,
"class" : "資工一1"
}
}
}
#這裡我不太清楚ES是怎麼運算的,有興趣的可能要自己上網找了
今天的文章就到這邊告一段落,明天的文章將會帶大家進入聚合的世界