iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 19
0
Elastic Stack on Cloud

Python&Elasticsearch 入門系列 第 19

IT鐵人第19天 Elasticsearch 使用python查詢資料 function_score 範例(2)

  • 分享至 

  • xImage
  •  

繼上一篇的範例,今天就把剩下的幾個例子結束掉吧

今天的例子都用以下的文檔當例子

  {
    "_index" : "school_members",
    "_type" : "_doc",
    "_id" : "_cXz3nQBXV0k7ep9qP8G",
    "_score" : 0.2876821,
    "_source" : {
      "student" : {
        "sid" : "s1090103",
        "name" : "風間",
        "age" : 18,
        "class" : "資工一1"
      }
    }
  }

boost_mode

用以下加權的例子作為示範
原query:

{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": {
            "term": {
              "student.name": "風間"
            }
          }
        }
      },
      "functions": [
        { 
          "filter": {"term": {
            "student.name": "風間"
          }}, 
          "weight": 2
        }
      ]
    }
  }
}

加上boost_mode

multiply(結果)

結果:

{
    "_index" : "school_members",
    "_type" : "_doc",
    "_id" : "_cXz3nQBXV0k7ep9qP8G",
    "_score" : 0.5753642,
    "_source" : {
      "student" : {
        "sid" : "s1090103",
        "name" : "風間",
        "age" : 18,
        "class" : "資工一1"
      }
    }
  }

sum

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

min

結果

  {
    "_index" : "school_members",
    "_type" : "_doc",
    "_id" : "_cXz3nQBXV0k7ep9qP8G",
    "_score" : 0.2876821,
    "_source" : {
      "student" : {
        "sid" : "s1090103",
        "name" : "風間",
        "age" : 18,
        "class" : "資工一1"
      }
    }
  }

0.28 < 2 所以結果變成0.28

max

結果:

  {
    "_index" : "school_members",
    "_type" : "_doc",
    "_id" : "_cXz3nQBXV0k7ep9qP8G",
    "_score" : 2.0,
    "_source" : {
      "student" : {
        "sid" : "s1090103",
        "name" : "風間",
        "age" : 18,
        "class" : "資工一1"
      }
    }
  }

0.28 < 2 所以結果是2

replace

結果:

  {
    "_index" : "school_members",
    "_type" : "_doc",
    "_id" : "_cXz3nQBXV0k7ep9qP8G",
    "_score" : 2.0,
    "_source" : {
      "student" : {
        "sid" : "s1090103",
        "name" : "風間",
        "age" : 18,
        "class" : "資工一1"
      }
    }
  }

直接把2取代掉0.28

score_mode

原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"
    }
  }
}

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

sum

結果:

  {
    "_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

max

結果:

  {
    "_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

min

結果:

  {
    "_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

first

結果:

  {
    "_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

avg

結果:

  {
    "_index" : "school_members",
    "_type" : "_doc",
    "_id" : "_cXz3nQBXV0k7ep9qP8G",
    "_score" : 1.9178805,
    "_source" : {
      "student" : {
        "sid" : "s1090103",
        "name" : "風間",
        "age" : 18,
        "class" : "資工一1"
      }
    }
  }

#這裡我不太清楚ES是怎麼運算的,有興趣的可能要自己上網找了

今天的文章就到這邊告一段落,明天的文章將會帶大家進入聚合的世界


上一篇
IT鐵人第18天 Elasticsearch 使用python查詢資料 function_score 範例(1)
下一篇
IT鐵人第20天 Elasticsearch 使用python查詢資料 Aggregations
系列文
Python&Elasticsearch 入門30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言