elasticsearch ElasticSearch如何按文档/字段权重排序?
我正在尝试根据文档的权重对搜索结果进行查询和排序,
elasticsearch ElasticSearch如何按文档/字段权重排序?,
elasticsearch,kibana,
elasticsearch,Kibana,我正在尝试根据文档的权重对搜索结果进行查询和排序, 我使用以下查询来创建和查询数据, ElasticSearch版本:7.6, Kibana版本:7.2 DELETE movies PUT movies { "mappings": { "properties": { "name": { "type": "completion" }, "year": { "type": "keyword"
我使用以下查询来创建和查询数据,
ElasticSearch版本:7.6,
Kibana版本:7.2
DELETE movies
PUT movies
{
"mappings": {
"properties": {
"name": {
"type": "completion"
},
"year": {
"type": "keyword"
}
}
}
}
POST _bulk
{ "index" : { "_index" : "movies", "_id" : "1" } }
{ "name" : "Spider-Man: Far From Home", "year" : "2019"}
{ "index" : { "_index" : "movies", "_id" : "2" } }
{ "name" : "Avengers: Endgame" , "year" : "2019"}
{ "index" : { "_index" : "movies", "_id" : "3" } }
{ "name" : "Captain Marvel" , "year" : "2019"}
{ "index" : { "_index" : "movies", "_id" : "4" } }
{ "name" : "Ant-man and the Wasp" , "year" : "2018" }
{ "index" : { "_index" : "movies", "_id" : "5" } }
{ "name" : "Avengers: Infinity War" , "year" : "2018" }
{ "index" : { "_index" : "movies", "_id" : "6" } }
{ "name" : "Black Panther" , "year" : "2018" }
{ "index" : { "_index" : "movies", "_id" : "7" } }
{ "name" : "Thor: Ragnarok" , "year" : "2017" }
{ "index" : { "_index" : "movies", "_id" : "8" } }
{ "name" : "Spider-Man: Homecoming" , "year" : "2017" }
{ "index" : { "_index" : "movies", "_id" : "9" } }
{ "name" : "Guardians of the Galaxy Vol 2" , "year" : "2017" }
{ "index" : { "_index" : "movies", "_id" : "10" } }
{ "name" : "Doctor Strange" , "year" : "2016" }
{ "index" : { "_index" : "movies", "_id" : "11" } }
{ "name" : "Guardians of the Galaxy Vol 2" , "year" : "2019"}
{ "index" : { "_index" : "movies", "_id" : "12" } }
{ "name" : "Captain America: Civil War" , "year" : "2016"}
{ "index" : { "_index" : "movies", "_id" : "13" } }
{ "name" : "Ant-Man" , "year" : "2015" }
{ "index" : { "_index" : "movies", "_id" : "14" } }
{ "name" : "Avengers: Age of Ultron" , "year" : "2015"}
{ "index" : { "_index" : "movies", "_id" : "15" } }
{ "name" : "Guardians of the Galaxy" , "year" : "2014" }
{ "index" : { "_index" : "movies", "_id" : "16" } }
{ "name" : "Captain America: The Winter Soldier" , "year" : "2014" }
{ "index" : { "_index" : "movies", "_id" : "17" } }
{ "name" : "Thor: The Dark World" , "year" : "2013" }
{ "index" : { "_index" : "movies", "_id" : "18" } }
{ "name" : "Iron Man 3" ,"year" : "2013" }
{ "index" : { "_index" : "movies", "_id" : "19" } }
{ "name" : "Marvel's The Avengers" , "year" : "2012"}
{ "index" : { "_index" : "movies", "_id" : "20" } }
{ "name" : "Captain America: The First Avenger" , "year" : "2011"}
{ "index" : { "_index" : "movies", "_id" : "21" } }
{ "name" : "Thor" , "year" : "2011"}
{ "index" : { "_index" : "movies", "_id" : "22" } }
{ "name" : "Iron Man 2", "year" : "2010" }
{ "index" : { "_index" : "movies", "_id" : "23" } }
{ "name" : "The Incredible Hulk", "year" : "2008" }
{ "index" : { "_index" : "movies", "_id" : "24" } }
{ "name" : "Iron Man" , "year" : "2008"}
PUT movies/_doc/22
{
"name": {
"input": ["Iron Man 2"],
"weight": 2
},
"year": 2010
}
GET movies/_search
{
"query": {
"match": {
"name": "iron man"
}
}
}
我得到以下输出-
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.754019,
"hits" : [
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "18",
"_score" : 1.754019,
"_source" : {
"name" : "Iron Man 3",
"year" : "2013"
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "24",
"_score" : 1.754019,
"_source" : {
"name" : "Iron Man",
"year" : "2008"
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "22",
"_score" : 1.754019,
"_source" : {
"name" : {
"input" : [
"Iron Man 2"
],
"weight" : 2
},
"year" : 2010
}
}
]
}
}
我的预期输出是《钢铁侠2》,因为它的重量更高,如何使用查询实现这一点?因为
name
字段的类型为completion,而不是使用匹配查询,所以您应该使用
你能澄清一下请求的必要性吗?我想给文档增加更高的权重,所以它是优先的
GET movies/_search
{
"suggest": {
"movie-suggest": {
"prefix": "Iron Man",
"completion": {
"field": "name"
}
}
}
}