Lucene.Net根据与点的距离进行排序

本文关键字:距离 排序 Net Lucene | 更新日期: 2023-09-27 18:26:18

我正在尝试搜索特定半径内的结果,并根据与点的距离(先近后远)对结果进行排序。然而,返回的结果是相反的排序(远在前,近在后)。

这是我的代码:

string name = "__Location";
var strategy = new PointVectorStrategy(ctx, name);
var indexSearcher = new IndexSearcher(_dir, true);
double radious = Double.Parse(rad);
double lat = 33.8886290;
double lng = 35.4954790;
var distance = DistanceUtils.Dist2Degrees(radious, DistanceUtils.EARTH_MEAN_RADIUS_MI);
var spatialArgs = new SpatialArgs(SpatialOperation.Intersects, ctx.MakeCircle(lng, lat, distance));
var spatialQuery = strategy.MakeQuery(spatialArgs);
Point pt = ctx.MakePoint(lng, lat);
ValueSource valueSource = strategy.MakeDistanceValueSource(pt);

ValueSourceFilter vsf = new ValueSourceFilter(new QueryWrapperFilter(spatialQuery ), valueSource, 0, distance);
var filteredSpatial = new FilteredQuery(new MatchAllDocsQuery(), vsf);
var spatialRankingQuery = new FunctionQuery(valueSource);
BooleanQuery bq = new BooleanQuery();
bq.Add(filteredSpatial,Occur.MUST);
bq.Add(spatialRankingQuery,Occur.MUST);
TopDocs hits = indexSearcher.Search(bq, 10);

如何根据从最近到远的距离对结果进行排序?

我正在使用:

  • Lucene.Net 3.0.3

  • Lucene.Net.Contrib.空间3.0.3

  • Spatial4n.Core 0.3

感谢

Lucene.Net根据与点的距离进行排序

这似乎是Lucene.Net.Contrib.Spatial在MakeDistanceValueSource方法中的一个错误,所以我写了一个新的DistanceValueSource类来解决这个问题,称为DistanceReverseValueSource你可以在中找到类的源

https://gist.github.com/aokour/088cd6484bce5e95ba83

这是我现在更新的代码片段:

string name = "__Location";
var strategy = new PointVectorStrategy(ctx, name);
var indexSearcher = new IndexSearcher(_dir, true);
double radious = Double.Parse(rad);
double lat = 33.8886290;
double lng = 35.4954790;
var distance = DistanceUtils.Dist2Degrees(radious, DistanceUtils.EARTH_MEAN_RADIUS_MI);
var spatialArgs = new SpatialArgs(SpatialOperation.Intersects, ctx.MakeCircle(lng, lat, distance));
var spatialQuery = strategy.MakeQuery(spatialArgs);
Point pt = ctx.MakePoint(lng, lat);
DistanceReverseValueSource valueSource = new DistanceReverseValueSource(strategy, pt, distance);

ValueSourceFilter vsf = new ValueSourceFilter(new QueryWrapperFilter(spatialQuery ), valueSource, 0, distance);
var filteredSpatial = new FilteredQuery(new MatchAllDocsQuery(), vsf);
var spatialRankingQuery = new FunctionQuery(valueSource);
BooleanQuery bq = new BooleanQuery();
bq.Add(filteredSpatial,Occur.MUST);
bq.Add(spatialRankingQuery,Occur.MUST);
TopDocs hits = indexSearcher.Search(bq, 10);

现在,结果从最接近到最远进行排序!