简体   繁体   English

Elasticsearch对多个查询进行排序

[英]Elasticsearch sort on multiple queries

I have a query like so: 我有这样的查询:

{
    "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 (name, a string) searchTerm(名称,字符串)
  2. flags (credit/atm/ada/etc, boolean values) 标志(credit / atm / ada / etc,布尔值)
  3. distance 距离

How can this be achieved? 如何做到这一点?

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. 我一直无法弄清楚如何始终返回所有结果,也无法将其他查询合并到排序中。

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. 然后,给您的名称标准更多的权重(增强),然后给您的标志更少的权重,甚至更少的距离计算。

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. 一个“布尔”“应该”将能够根据每个_score为您提供一系列文档列表,并且根据您对每个标准的评分方式,该_score或多或少会受到影响。

Also, returning ALL the elements is not difficult: just add a "match_all": {} to your "bool" "should" query. 同样,返回所有元素并不困难:只需在您的“布尔”“应该”查询中添加"match_all": {}

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": {}
        }
      ]
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM