【问题标题】:ElasticSearch 2 bucket level sortingElasticSearch 2 桶级排序
【发布时间】:2015-08-31 17:24:21
【问题描述】:

数据库的映射是这样的:

{
   "users": {
      "mappings": {
         "user": {
            "properties": {
              credentials": {
                  "type": "nested",
                  "properties": {
                     "achievement_id": {
                        "type": "string"
                     },
                     "percentage_completion": {
                        "type": "integer"
                     }
                  }
               },
               "current_location": {
                  "type": "geo_point"
               },
             "locations": {
               "type": "geo_point"
         }
            }
         }
      }
   }

现在在映射中,您可以看到有两个地理距离字段,一个是 current_location,另一个是位置。现在我想根据 credentials.percentage_completion 对用户进行排序,这是一个嵌套字段。这很好,例如这个查询, 示例查询:

GET /users/user/_search?size=23
{
  "sort": [
    {
      "credentials.percentage_completion": {
        "order": "desc",
        "missing": "_last"
      }
    },
 "_score"
  ],
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "geo_distance": {
          "distance": "100000000km",
          "user.locations": {
            "lat": 19.77,
            "lon": 73
          }
        }
      }
    }
  }
}

我想更改桶中的排序顺序,希望的顺序是首先显示所有在 user.current_location 半径 100KM 范围内的人,并根据 credentials.percentage_completion 对他们进行排序,然后再按 credentials.percentage_completion 对其余用户进行排序.

我尝试在排序中放置条件并使其成为多级,但这不起作用,因为只有嵌套可以有过滤器,而嵌套字段只能有子字段。

我认为我可以使用 _score 进行排序并为 1000 公里以下的人提供更多相关性,但地理距离是一个过滤器,我似乎没有找到任何方法在过滤器中提供相关性。

这里有什么我遗漏的吗,任何帮助都会很棒。

谢谢

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    终于解决了,把它贴在这里,这样其他人到了这里也可以带头。解决此问题的方法是为特定查询提供恒定的相关性分数,但由于这里是地理距离,因此无法在查询中使用它,然后我发现 Constant Score query:它允许在查询中包装过滤器。

    这是查询的样子:

    GET /users/user/_search?size=23
    {
      "sort": [
        "_score",
        {
          "credentials.udacity_percentage_completion": {
            "order": "desc",
            "missing": "_last"
          }
        }
      ],
      "explain": true,
      "query": {
        "filtered": {
          "query": {
            "bool": {
              "should": [
                {
                  "constant_score": {
                    "filter": {
                      "geo_distance": {
                        "distance": "100km",
                        "user.current_location": {
                          "lat": 19.77,
                          "lon": 73
                        }
                      }
                    },
                    "boost": 50
                  }
                },
                {
                  "constant_score": {
                    "filter": {
                      "geo_distance": {
                        "distance": "1000000km",
                        "user.locations": {
                          "lat": 19.77,
                          "lon": 73
                        }
                      }
                    },
                    "boost": 1
                  }
                }
              ]
            }
          },
          "filter": {
            "geo_distance": {
              "distance": "10000km",
              "user.locations": {
                "lat": 19.77,
                "lon": 73
              }
            }
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-09-19
      • 1970-01-01
      • 2021-03-28
      • 2018-09-14
      • 2018-05-03
      • 2021-01-30
      • 2016-09-08
      • 1970-01-01
      • 2016-03-15
      相关资源
      最近更新 更多