Solr Spatial search - How do I sort documents first by an explicit match and then by distance?

StackOverflow https://stackoverflow.com/questions/14676191

  •  06-03-2022
  •  | 
  •  

문제

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.

도움이 되었습니까?

해결책

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'.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top