<p>I've had this long term issue in not quite understanding how to implement a decent Lucene sort or ranking.  Say I have a list of cities and their populations. If someone searches "new" or "london" I want the list of prefix matches ordered by population, and I have that working with a prefix search and an sort by field reversed, where there is a population field, IE New Mexico, New York; or London, Londonderry.</p>

<p>However I also always want the exact matching name to be at the top.  So in the case of "London" the list should show "London, London, Londonderry" where the first London is in the UK and the second London is in Connecticut, even if Londonderry has a higher population than London CT.</p>

<p>Does anyone have a single query solution?</p>

[{'answer_id': 8522, 'author': 'dlamblin', 'author_id': 459, 'author_profile': 'https://Stackoverflow.com/users/459', 'pm_score': 0, 'selected': False, 'text': '<p>My current solution is to create an exact searcher and a prefix searcher, both sorted by reverse population, and then copy out all my hits starting from the exact hits, moving to the prefix hits.  It makes paging my results slightly more annoying than I think it should be.</p>\n\n<p>Also I used a hash to eliminate duplicates but later changed the prefix searcher into a boolean query of a prefix search (MUST) with an exact search (MUST NOT), to have Lucene remove the duplicates. Though this seemed even more wasteful.</p>\n\n<p><em>Edit</em>: Moved to a comment (since the feature now exists): <a href="https://stackoverflow.com/users/1702/yuval-f">Yuval F</a> Thank you for your blog post ... How would the sort comparator know that the name field "london" exactly matches the search term "london" if it cannot access the search term?</p>\n'}, {'answer_id': 36791, 'author': 'Yuval F', 'author_id': 1702, 'author_profile': 'https://Stackoverflow.com/users/1702', 'pm_score': 3, 'selected': True, 'text': '<p>dlamblin,let me see if I get this correctly: You want to make a prefix-based query, and then sort the results by population, and maybe combine the sort order with preference for exact matches.\nI suggest you separate the search from the sort and use a CustomSorter for the sorting:\nHere\'s <a href="http://blog.tremend.ro/2007/05/17/a-z-0-9-custom-sorting-in-lucene/" rel="nofollow noreferrer">a blog entry describing a custom sorter</a>.\n<a href="https://rads.stackoverflow.com/amzn/click/com/1932394281" rel="nofollow noreferrer" rel="nofollow noreferrer">The classic Lucene book</a> describes this well.</p>\n'}, {'answer_id': 1378085, 'author': 'Narayan', 'author_id': 81367, 'author_profile': 'https://Stackoverflow.com/users/81367', 'pm_score': 1, 'selected': False, 'text': '<p>API for </p>\n\n<blockquote>\n  <p><strong><a href="http://lucene.apache.org/java/2_4_0/api/org/apache/lucene/search/SortComparator.html" rel="nofollow noreferrer">Sortcomparator</a></strong></p>\n</blockquote>\n\n<p>says</p>\n\n<blockquote>\n  <p>There is a distinct Comparable for each unique term in the field - if\n  some documents have the same term in\n  the field, the cache array will have\n  entries which reference the same\n  Comparable</p>\n</blockquote>\n\n<p>You can apply a </p>\n\n<blockquote>\n  <p><strong><a href="http://lucene.apache.org/java/2_4_0/api/org/apache/lucene/search/FieldSortedHitQueue.html" rel="nofollow noreferrer">FieldSortedHitQueue</a></strong></p>\n</blockquote>\n\n<p>to the sortcomparator which has a Comparator field for which  the  api says ...  </p>\n\n<blockquote>\n  <p>Stores a comparator corresponding to\n  each field being sorted by.</p>\n</blockquote>\n\n<p>Thus the term can be sorted accordingly</p>\n'}]
['https://Stackoverflow.com/questions/8517', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/459/']