node.js - Elasticsearch对多个查询进行排序

标签 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
                    }
                }
            ]
        }
    }
}

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

我希望排序优先级如下:
  • searchTerm(名称,字符串)
  • 标志(信用/ atm / ada / etc, bool(boolean) 值)
  • 距离

  • 如何做到这一点?

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

    最佳答案

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

    一个“ bool(boolean) ”“应该”将能够根据每个_score为您提供排序的文档列表,并且根据您对每个标准的评分方式,或多或少会影响_score。

    同样,返回所有元素也不难:只需将"match_all": {}添加到“ bool(boolean) ”“应该”查询中即可。

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

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

    关于node.js - Elasticsearch对多个查询进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26005449/

    相关文章:

    elasticsearch - ElasticSearch映射问题-嵌套到非嵌套

    elasticsearch - 无法远程访问9200端口

    node.js - PayPal 执行付款返回 PAYMENT_STATE_INVALID

    javascript - 使用本地存储来存储AngularJS数据

    Bash:按内容对 'find' 中的文件进行排序

    linux - 在不丢失原始排序顺序的情况下进行子排序?

    javascript - Node/Express 4.0 中可以声明全局变量吗

    Visual Studio 12 中不显示 Node.js 交互窗口

    java - 如何按最接近的值排序列表

    python - 在 Elastic Search 中为批量加载设置请求超时