Elasticsearch对多个查询进行排序 [英] Elasticsearch sort on multiple queries

查看:86
本文介绍了Elasticsearch对多个查询进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的查询:

{
    "sort": [
        {
            "_geo_distance": {
                "geo": {
                    "lat": 39.802763999999996,
                    "lon": -105.08748399999999
                },
                "order": "asc",
                "unit": "mi",
                "mode": "min",
                "distance_type": "sloppy_arc"
            }
        }
    ],
    "query": {
        "bool": {
            "minimum_number_should_match": 0,
            "should": [
                {
                    "match": {
                        "name": ""
                    }
                },
                {
                    "match": {
                        "credit": true
                    }
                }
            ]
        }
    }
}

我希望我的搜索始终返回所有结果,只是将那些具有匹配标志的结果排序为最接近顶部的结果进行排序.

I want my search to always return ALL results, just sorted with those which have matching flags closer to the top.

我希望排序优先级类似于:

I would like the sorting priority to go something like:

  1. searchTerm(名称,字符串)
  2. 标志(credit/atm/ada/etc,布尔值)
  3. 距离

如何实现?

到目前为止,您在上面看到的查询就是我所得到的.我一直无法弄清楚如何始终返回所有结果,也无法将其他查询合并到排序中.

So far, the query you see above is all I've gotten. I haven't been able to figure out how to always return all results, nor how to incorporate the additional queries into the sort.

推荐答案

实际上,我不认为"sort"是您要寻找的答案.我相信您需要一个反复试验的方法,从简单的布尔"查询开始,在该查询中放置所有条件(名称,标志,距离).然后,给您的名称标准更多的权重(增强),然后为您的标志减少一些权重,甚至减少距离计算.

I don't believe "sort" is the answer you are looking for, actually. I believe you need a trial-and-error approach starting with a simple "bool" query where you put all your criterias (name, flags, distance). Then you give your name criteria more weight (boost) then a little bit less to your flags and even less to the distance calculation.

布尔"应该"将根据每个_score为您提供排序的文档列表,并且根据您对每个标准的评分方式,该_score或多或少会受到影响.

A "bool" "should" would be able to give you a sorted list of documents based on the _score of each and, depending on how you score each criteria, the _score is being influenced more or less.

此外,返回所有元素并不困难:只需在您的布尔"应该"查询中添加"match_all":{} .

Also, returning ALL the elements is not difficult: just add a "match_all": {} to your "bool" "should" query.

从我的角度来看,这将是一个起点,根据您的文档和您的要求(请参阅我对您的困惑的评论),您需要调整提升"值并测试,调整再次并再次测试,等等:

This would be a starting point, from my point of view, and, depending on your documents and your requirements (see my comment to your post about the confusion) you would need to adjust the "boost" values and test, adjust again and test again etc:

{
  "query": {
    "bool": {
      "should": [
        { "constant_score": {
            "boost": 6,
            "query": {
              "match": { "name": { "query": "something" } }
            }
        }},
        { "constant_score": {
            "boost": 3,
            "query": {
              "match": { "credit": { "query": true } }
            }
        }},
        { "constant_score": {
            "boost": 3,
            "query": {
              "match": { "atm": { "query": false } }
            }
        }},
        { "constant_score": {
            "boost": 3,
            "query": {
              "match": { "ada": {  "query": true } }
            }
        }},
        { "constant_score": {
            "query": {
              "function_score": {
                "functions": [
                  {
                    "gauss": {
                      "geo": {
                        "origin": {
                          "lat": 39.802763999999996,
                          "lon": -105.08748399999999
                        },
                        "offset": "2km",
                        "scale": "3km"
                      }
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "match_all": {}
        }
      ]
    }
  }
}

这篇关于Elasticsearch对多个查询进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆