我在一個 ES 索引中有不同的 type_id,想給不同的值 type_id 不同的分數,使某些型別的搜索結果排名更高。我的查詢是
{
"query":{
"bool":{
"must":[
{"terms":{"type_id":[9,10]}}
],
"should":[
{"match":{ "display_name":{"query":"keyword","boost":10}}},
{"match":{ "description":{"query":"keyword","boost":2}}}
]
}
}
}
我想讓type_id9 匹配分數高于type_id10 時 display_name和description相同。
請指導我解決這個問題。謝謝。
uj5u.com熱心網友回復:
您可以像下面這樣對查詢進行分組,并使用 boost 為某些 id 賦予更多權重。
{
"query": {
"bool": {
"must": [
{
"bool": {
"minimum_should_match": 1,
"should": [
{
"term": {
"type_id": {
"value": 9,
"boost": 2
}
}
},
{
"term": {
"type_id": {
"value": 10,
"boost": 1
}
}
}
]
}
},
{
"bool": {
"minimum_should_match": 1,
"should": [
{
"match": {
"display_name": {
"query": "keyword",
"boost": 10
}
}
},
{
"match": {
"description": {
"query": "keyword",
"boost": 2
}
}
}
]
}
}
]
}
}
}
編輯:對于評論中的查詢,您可以使用function_score
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"function_score": {
"query": {
"bool": {
"must": [
{
"term": {
"type_id": {
"value": 9
}
}
}
],
"minimum_should_match": 1,
"should": [
{
"match": {
"display_name": {
"query": "keyword"
}
}
},
{
"match": {
"description": {
"query": "keyword"
}
}
}
]
}
},
"boost": "5"
}
},
{
"function_score": {
"query": {
"bool": {
"must": [
{
"term": {
"type_id": {
"value": 10
}
}
}
],
"minimum_should_match": 1,
"should": [
{
"match": {
"display_name": {
"query": "keyword"
}
}
},
{
"match": {
"description": {
"query": "keyword"
}
}
}
]
}
},
"boost": "4"
}
}
]
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/517316.html
標籤:弹性搜索搜索
