我的映射有兩個屬性:
"news_from_date" : {
"type" : "string"
},
"news_to_date" : {
"type" : "string"
},
搜索結果具有屬性 news_from_date、news_to_date
curl -X GET 'http://172.2.0.5:9200/test_idx1/_search?pretty=true' 2>&1
結果:
{
"news_from_date" : "2022-05-30 00:00:00",
"news_to_date" : "2022-06-23 00:00:00"
}
問題是:如何提升當前日期介于“news_from_date”-“news_to_date”區間之間的所有結果,以便將它們顯示為排名最高的結果?
uj5u.com熱心網友回復:
特長;
首先,如果您要使用日期,您可能應該使用由Elasticsearch.
它們是解決問題的多種方法,使用無痛、使用評分功能甚至更經典的查詢型別。
使用Should
使用布爾查詢型別,您有多個子句。
- 必須
- 篩選
- 一定不
- 應該
Should允許在最終分數中考慮可選條款。
所以你去:
GET _search
{
"query": {
"bool": {
"should": [
{
"range": {
"news_from_date": {
"gte": "now"
}
}
},
{
"range": {
"news_to_date": {
"lte": "now"
}
}
}
]
}
}
}
意識到:
您可以使用 minimum_should_match 引數指定回傳的檔案必須匹配的 should 子句的數量或百分比。
如果 bool 查詢至少包含一個 should 子句且沒有 must 或 filter 子句,則默認值為 1。否則,默認值為 0。
使用腳本
正如檔案所提供的,您可以創建自定義函式來根據您自己的業務規則對檔案進行評分。
該腳本使用Painless(Java 的精簡版)
GET /_search
{
"query": {
"function_score": {
"query": {
"match": { "message": "elasticsearch" }
},
"script_score": {
"script": {
"source": "Math.log(2 doc['my-int'].value)"
}
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/485197.html
標籤:弹性搜索
