Lucene.Net排序结果与点的距离

时间:2014-10-12 09:02:00

标签: c# sorting lucene lucene.net spatial

我正在尝试搜索特定半径范围内的结果,并根据距离点(靠近第一个,最后一个点)的距离对结果进行排序。 但是,返回的结果出现在相反的排序中(远远超过最后)。

这是我的代码:

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.Spatial 3.0.3

  • Spatial4n.Core 0.3

由于

1 个答案:

答案 0 :(得分:1)

这似乎是 MakeDistanceValueSource 方法中的Lucene.Net.Contrib.Spatial中的一个错误,所以我编写了一个新的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);

现在结果从最近的到最近的排序!