gpt4 book ai didi

c# - Lucene.Net 根据与点的距离排序结果

转载 作者:太空宇宙 更新时间:2023-11-03 13:15:42 26 4
gpt4 key购买 nike

我正在尝试搜索特定半径内的结果,并根据与某个点的距离(最接近第一个,最远最后一个)对结果进行排序。然而,返回的结果以相反的排序出现(最靠前,最接近最后)。

这是我的代码:

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

谢谢

最佳答案

这似乎是 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);

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

关于c# - Lucene.Net 根据与点的距离排序结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26323475/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com