繁体   English   中英

Elasticsearch对多个查询进行排序

[英]Elasticsearch sort on multiple queries

我有这样的查询:

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

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

我希望排序优先级如下:

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

如何做到这一点?

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

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

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

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

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

{
  "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