如何更改 Elastic Search 上搜索结果的顺序?
How to change the order of search results on Elastic Search?
我正在从以下 Elastic Search 查询中获取结果:
"query": {
"bool": {
"should": [
{"match_phrase_prefix": {"title": keyword}},
{"match_phrase_prefix": {"second_title": keyword}}
]
}
}
结果很好,但我想更改结果的顺序,使匹配 title
的结果排在最前面。
任何帮助将不胜感激!!!
我能够使用示例数据重现该问题,我的解决方案是使用查询时间提升,因为索引时间提升已从 ES 5 的主要版本中弃用。
此外,我以这样的方式创建样本数据,如果没有提升,两个样本数据将具有相同的分数,因此不能保证匹配的数据在搜索结果中排在第一位,这应该帮助您更好地理解它。
1。索引映射
{
"mappings": {
"properties": {
"title": {
"type": "text"
},
"second_title" :{
"type" :"text"
}
}
}
}
2。索引示例文档
一)
{
"title": "opster",
"second_title" : "Dimitry"
}
b)
{
"title": "Dimitry",
"second_title" : "opster"
}
搜索查询
{
"query": {
"bool": {
"should": [
{
"match_phrase_prefix": {
"title": {
"query" : "dimitry",
"boost" : 2.0 <-- Notice the boost in `title` field
}
}
},
{
"match_phrase_prefix": {
"second_title": {
"query" : "dimitry"
}
}
}
]
}
}
}
输出
"hits": [
{
"_index": "60454337",
"_type": "_doc",
"_id": "1",
"_score": 1.3862944,
"_source": {
"title": "Dimitry", <-- Dimitry in title field has doube score
"second_title": "opster"
}
},
{
"_index": "60454337",
"_type": "_doc",
"_id": "2",
"_score": 0.6931472,
"_source": {
"title": "opster",
"second_title": "Dimitry"
}
}
]
如果您对理解有任何疑问,请告诉我。
我正在从以下 Elastic Search 查询中获取结果:
"query": {
"bool": {
"should": [
{"match_phrase_prefix": {"title": keyword}},
{"match_phrase_prefix": {"second_title": keyword}}
]
}
}
结果很好,但我想更改结果的顺序,使匹配 title
的结果排在最前面。
任何帮助将不胜感激!!!
我能够使用示例数据重现该问题,我的解决方案是使用查询时间提升,因为索引时间提升已从 ES 5 的主要版本中弃用。
此外,我以这样的方式创建样本数据,如果没有提升,两个样本数据将具有相同的分数,因此不能保证匹配的数据在搜索结果中排在第一位,这应该帮助您更好地理解它。
1。索引映射
{
"mappings": {
"properties": {
"title": {
"type": "text"
},
"second_title" :{
"type" :"text"
}
}
}
}
2。索引示例文档
一)
{
"title": "opster",
"second_title" : "Dimitry"
}
b)
{
"title": "Dimitry",
"second_title" : "opster"
}
搜索查询
{
"query": {
"bool": {
"should": [
{
"match_phrase_prefix": {
"title": {
"query" : "dimitry",
"boost" : 2.0 <-- Notice the boost in `title` field
}
}
},
{
"match_phrase_prefix": {
"second_title": {
"query" : "dimitry"
}
}
}
]
}
}
}
输出
"hits": [
{
"_index": "60454337",
"_type": "_doc",
"_id": "1",
"_score": 1.3862944,
"_source": {
"title": "Dimitry", <-- Dimitry in title field has doube score
"second_title": "opster"
}
},
{
"_index": "60454337",
"_type": "_doc",
"_id": "2",
"_score": 0.6931472,
"_source": {
"title": "opster",
"second_title": "Dimitry"
}
}
]
如果您对理解有任何疑问,请告诉我。