2015-08-31 29 views
1

数据库的映射是这样的:ElasticSearch 2桶水平整理

{ 
    "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半径和整理他们的人根据凭证.percentage_completion,然后其余的用户通过credentials.percentage_completion重新排序。

我试图在条件排序,并使其成为多层次,但不会工作,因为只有嵌套可以有过滤器和嵌套字段只有子级。

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

有什么我在这里失踪,任何帮助将是伟大的。

感谢

回答

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 
      } 
     } 
     } 
    } 
    } 
}