首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ElasticSearch 2桶级排序

ElasticSearch 2桶级排序
EN

Stack Overflow用户
提问于 2015-08-31 07:04:50
回答 1查看 215关注 0票数 1

数据库的映射如下:

代码语言:javascript
复制
{
   "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对用户进行排序,这是一个嵌套字段。这个操作很好--例如,这个查询,示例查询:

代码语言:javascript
复制
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半径100公里处的所有用户,然后根据credentials.percentage_completion对其进行排序,然后再按credentials.percentage_completion对其他用户进行排序。

我尝试在排序中添加条件,并将其设置为多级,但这不起作用,因为只有嵌套的筛选器才能有过滤器,而嵌套字段上的过滤器只能是子字段。

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

如果我在这里有什么遗漏的话,任何帮助都会很好。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-31 09:24:21

最后解决了这个问题,把它发到这里,这样其他人也可以在他们到达的时候带头。解决这个问题的方法是为特定的查询提供持续的相关性评分,但是由于这里是Geo距离,所以不能在查询中使用它,所以我找到了常值分数查询:它允许在查询中包装过滤器。

这是查询的外观:

代码语言:javascript
复制
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
          }
        }
      }
    }
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32305639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档