Elasticsearch对多个查询进行排序

时间:2014-09-23 22:04:11

标签: node.js sorting elasticsearch search-engine

我有这样的查询:

{
    "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(name,a string)
  2. flags(credit / atm / ada / etc,boolean values)
  3. 距离
  4. 如何实现这一目标?

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

1 个答案:

答案 0 :(得分:1)

我不相信"排序"实际上是你正在寻找的答案。我相信你需要一个试错法,从一个简单的" bool"查询您放置所有标准的位置(名称,标志,距离)。然后你给你的名字标准更多的重量(增强)然后少一点你的旗帜,甚至更少的距离计算。

A" bool" "应该"能够根据每个标准的_score为您提供一个排序的文档列表,并且根据您对每个标准的评分,_score会受到或多或少的影响。

此外,返回所有元素并不困难:只需将"match_all": {}添加到您的" bool" "应该"查询。

从我的观点来看,这将是一个起点,并且,根据您的文件和您的要求(请参阅我对您的帖子有关混淆的评论),您需要调整"提升"值和测试,再次调整并再次测试等:

{
  "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": {}
        }
      ]
    }
  }
}
相关问题