jo.p.kennedy
jo.p.kennedy

Reputation: 138

SolrNet spatial search - sort on geodist()

I'm using Solr (3.6.0) with SolrNet (0.4) and I'm looking for some help with spatial search in SolrNet - specifically, sorting the results by distance.

Here's my query:

var postcode = new SolrQueryByField("Postcode", fields[13]);
var distance = new SolrQueryByDistance("LatLong", latitude, longitude, 1);
QueryOptions options = new QueryOptions();
options.Rows = 25;
options.AddFilterQueries(distance);

I've tried the obvious things:

options.OrderBy = new List<SortOrder> { new SortOrder("geodist()", Order.ASC)};

and:

options.OrderBy = new List<SortOrder> { SortOrder.Parse("geodist() asc") };

and as a last-ditch attempt, I tried defining the filter query together with the sort as extra params:

options.ExtraParams = new Dictionary<string, string>
{
    {"d", "1"},                        
    {"sField", "LatLong"},
    {"pt", latitudeString + "," + longitudeString},
    {"fq", "{!geofilt}"},
    {"sort", "geodist() asc"}
};

but everything results in:

SEVERE: org.apache.solr.common.SolrException: can not sort on unindexed field: geodist()

I also tried passing in the lat, long and distance parameters to the geodist() function, to no avail.

I'm able to construct this query by hand, just not through SolrNet! It seems that the problem's to do with the way the query string is ordered and bracketed.

This works (constructed by hand):

{d=1&sort=geodist()+asc&sfield=LatLong&version=2.2&rows=25&q=Postcode:"LN1+2EB"&pt=52.1,-1.11&fq={!geofilt}}

This fails (constructed by SolrNet):

{d=1&sort=geodist()+asc&q=(Postcode:"LN1+2EB")&sField=LatLong&pt=53.289186,-0.705095&fq={!geofilt}&rows=25&version=2.2}

I assume I'm doing something silly; there must be a way to make this functionality work! Any pointers would be much appreciated.

Upvotes: 2

Views: 2300

Answers (1)

Adam
Adam

Reputation: 442

This will order your results by distance. Uncomment lines to also filter the by distance from point.

solr.Query(SolrQuery.All,
                              new QueryOptions
                                  {
                                      FilterQueries = filterQueries.ToArray(), // add filter items
                                      OrderBy = new[] { new SolrNet.SortOrder("geodist()", Order.ASC) },
                                      ExtraParams = new Dictionary<string, string>
                                          {
                                              // uncomment for filtering by distance
                                              //{"fq", "{!geofilt}"},
                                              //{"d", distance.ToString(CultureInfo.InvariantCulture)} replace distance with your radius filter
                                              {"sfield", "lat_long"}, // replace lat_long with your field in solr that stores the lat long values
                                              {"pt", "-33.858727,151.213199"}, // this is the point of reference
                                                                                        }
                                  });

Upvotes: 3

Related Questions