提问者:小点点

Lucene.Net点的距离排序结果


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

这是我的代码:

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.Net3.0.3

    Lucene.Net. Contrib.Sspace 3.0.3

    空间4n.核心0.3

    谢啦


  • 共1个答案

    匿名用户

    这似乎是Lucene.Net. Contrib.Sspace al中的一个bug,所以我写了一个可以解决问题的新距离值源类,叫做距离值源,你可以在下面找到类的来源:

    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);
    

    现在,结果从最接近最接近的排序!