我将ONGR ElasticsearchDSL Bundle和Symfony 3一起使用,以经纬度坐标搜索城市列表,如下所示:
$repository = $this->elasticsearchManager->getRepository(CityDocument::class);
$search = $repository->createSearch();
$boolQuery = new BoolQuery();
$boolQuery->add(new MatchAllQuery());
$geoQuery = new GeoDistanceQuery('geo', '50km', $theSearch['location']);
$boolQuery->add($geoQuery, BoolQuery::FILTER);
$search->addQuery($boolQuery);
$documents = $repository->findDocuments($search);
虽然这给我匹配的城市带来了很好的结果,但是我不清楚如何按照距源点的距离(在$ theSearch ['location']中找到)进行排序,以便使它们从最接近的位置到最远的位置排序?!
答案 0 :(得分:1)
这是使用ElasticsearchDSL的正确方法,包括所需的排序。
$repository = $this->elasticsearchManager->getRepository(CityDocument::class);
$search = $repository->createSearch();
$geoQuery = new GeoDistanceQuery('geo', '50km', $theSearch['location']);
$search->addQuery($geoQuery, BoolQuery::FILTER);
$sort = new FieldSort('_geo_distance', null, [
'geo' => $geoLocation,
'order' => 'asc',
'unit' => 'km',
'mode' => 'min',
'distance_type' => 'arc'
]);
$search->addSort($sort);
$documents = $repository->findDocuments($search);