title: Sort on matching number of days in period with Solr
body: <p>We are asked to implement a new feature in one of our sites and after a certain amount of research we think that we can't solve this within Solr. But we briefly want to double check this;</p>
  
  <p>Let's say we store Companies in our index. Every company can have multiple opening periods. A period is formatted as "start day (date)" to "end day (date)". At the website we allow our visitor to search on a period and we show them the companies which match at least one day of the company's periods. So far so good, we already implemented this with Spatial Search;
  (<a href="https://people.apache.org/~hossman/spatial-for-non-spatial-meetup-20130117/" rel="nofollow">https://people.apache.org/~hossman/spatial-for-non-spatial-meetup-20130117/</a>).</p>
  
  <p>Our field(type) definition:</p>
  
  <pre><code>&lt;fieldType name="dayrange" class="solr.SpatialRecursivePrefixTreeFieldType" geo="false" worldBounds="ENVELOPE(16071, 20088, 20088, 16071)" distErrPct="0" maxDistErr="1" units="degrees"/&gt;
  
  &lt;field name="periods" type="dayrange" indexed="true" stored="true" multiValued="true" required="false"/&gt;
  </code></pre>
  
  <p><em>Note: the worldBounds are based on the days since epoch. Start day is 1 jan 2014 (16071 days since epoch) and the end day is 31 dec 2024 (20088 days since epoch).</em></p>
  
  <p>Whenever we want to search we use the contains method: <code>fq=periods:"INTERSECTS(16071 16733 16739 20088)"</code></p>
  
  <p>Like i said, so far so good. This is already working. Now they asked us to implement two new features;</p>
  
  <ol>
  <li>Allow sorting based on the number of matching days </li>
  <li>Display the number of matching days (This company is opened on 10 of the 14 days you searched for)</li>
  </ol>
  
  <p>The second feature <em>could</em> also be implemented outside Solr (e.g. in our application). But if its possible to sort on the number of matching days, it will probably also be possible to return that number?! For performance reasons we prefer to implement the sorting thing on Solr. But right now we couldn't find a good solution to implement this. 
  Anyone who can help us with the right solution or can confirm this is not possible with native Solr 4.x?!</p>
  
  <p>I'd be very grateful for any help.</p>