1
votes

I'm trying to determine how to sort or score the results of my query so that documents with an explicit match on a particular field will be returned first, followed by other matching documents in ascending distance.

Following the examples used in Solr's Spatial tutorials, let's say that I have a couple Stores, which looks something like this (pseudo):

<Store>
  <Id>1</Id>
  <Coords>10,-15</Coords>
  <PostalCode>12345</PostalCode>
  <ServiceArea>
    <PostalCode>12345</PostalCode>
    <PostalCode>23456</PostalCode>
  </ServiceArea> 
</Store>

<Store>
  <Id>2</Id>
  <Coords>11,-16</Coords>
  <PostalCode>23456</PostalCode>
  <ServiceArea>
    <PostalCode>12345</PostalCode>
    <PostalCode>23456</PostalCode>
  </ServiceArea> 
</Store>

So, two stores. Each in their own postal codes, and each servicing each other's postal codes. Now I want to query something to the effect of:

Find stores which service PostalCode 12345
Ordered by stores in PostalCode 12345 first
Then ordered by ascending distance from PostalCode 12345

I'm trying to keep this to using the StandardQueryParser, and if boosting is involved it needs to be defined in the query, rather than the index.

1

1 Answers

2
votes

For the sort specification, use:

sort=query({!field f=PostalCode v=12345})+desc,geodist()+asc

You can sort on fields, score, and function queries. Function queries opens up a lot of possibilities, and the "query()" function query opens up even more, as seen above. query() will return the score of the query within it, which in this case is a search for a field having a certain value. I chose to use the 'field' query parser but you just as well could do {!v="PostalCode:12345"}.

By the way, don't forget the other parameters for geodist(), like 'pt' and 'sfield'.