elasticsearch系列(七)java定義score

來源:互聯網
上載者:User

標籤:如何   檔案格式   tor   嵌套   modules   建立   使用   計算   pac   

概述

ES支援groovy 和 java兩種語言自訂score的計算方法,groovy甚至可以嵌套在請求的參數中,有點厲害,不過不在本篇討論範圍。

如何用自訂的java代碼來定義score如何產生,這用到了ES的外掛程式功能

關於es外掛程式的相關操作,可以通過以下命令獲得

./bin elasticsearch-plugn –h

 

操作步驟1.增加配置

首先需要在ES的配置中增加以下,

舊版本是

script.disable_dynamic: false

 

目前用的ES是5.2.2,根據日誌提示,改成如下,改完後需要重啟ES

script.inline: truescript.stored: true

 

不得不說這個日誌提供的資訊真不錯

 

 

如果不加這兩條配置,則會這樣這種錯誤

 

2.寫java代碼

//java代碼格式參考

https://www.elastic.co/guide/en/elasticsearch/reference/5.2/modules-scripting-native.html

 

3.寫設定檔

設定檔格式參考

https://github.com/elastic/elasticsearch/blob/master/buildSrc/src/main/resources/plugin-descriptor.properties

 

4.上傳到ES叢集

在$ELASTICSEARCH_HOME/plugins建立一個檔案夾,這個檔案夾的名稱就是這個外掛程式的名稱,

例如有 三個檔案夾

可以通過如下命令找到他們

 

在建立的檔案夾中,匯入jarplugin-descriptor.properties檔案(必須有)

 

5.重啟ES服務

kill -9 pid

nohup ./elasticsearch & 

 

5.使用
{  "query": {    "function_score": {      "query": {        "match": {          "your_fields": "xxxx"        }      },      "functions": [        {          "script_score": {            "script": {              "inline": "key",              "lang": "native",              "params": {                "your_fields": "xxxxx"              }            }          }        }      ]    }  }}

 

 

key解釋:

Key

解釋

備忘

inline

調用外掛程式的key,和外掛程式名稱是兩個東西

在NativeScriptFactory介面中的
getName()方法設定

lang

自訂方式

native意思是用java原生實現

 

 

//關於自訂function-script-score

https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-function-score-query.html#function-script-score

 

6.更新外掛程式

由於ES叢集會快取區外掛程式代碼,就算把外掛程式刪了依然可用,目前還是需要重啟來更新外掛程式

 

參考資料

//自訂java製作外掛程式必要的步驟

https://www.elastic.co/guide/en/elasticsearch/plugins/5.2/plugin-authors.html#_plugin_structure

//github完整外掛程式

https://github.com/momoxixi/elasticsearch-feature-vector-scoring

 

elasticsearch系列(七)java定義score

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.