Exact order Lucene

I had this long-term problem, not quite understanding how to implement decent Lucene sorting or ranking. Say I have a list of cities and their populations. If someone is looking for “new” or “London”, I want a list of prefix matches sorted by population, and I have a prefix search job and sort by field that has a field to fill in, IE New Mexico, New York; or London, Londonderry.

However, I also always want the exact matching name to be at the top. Therefore, in the case of “London”, “London, London, Londonderry” should appear on the list, where the first London is in the UK and the second London is in Connecticut, even if Londonderry has more population than in London.

Does anyone have one solution for queries?

+4
source share
3 answers

dlamblin, let me see if I do it right: you want to make a query based on a prefix, and then sort the results by totality and possibly combine the sort order with the preference for exact matches. I suggest you separate search from sorting and use CustomSorter to sort: Here 's a blog post describing a custom sorter . The classic book Lucene describes this well.

+3
source

API for

Sortcomparator

He speaks

For each unique member in the field, there is a clear, comparable value - if some documents have the same field, the cache array will have entries that reference the same. Comparable

You can apply

FieldSortedHitQueue

to a sorting sorter that has a Comparator field for which api says ...

Stores a comparator, corresponding to each field sorted by.

Thus, this term can be sorted accordingly.

+1
source

My current solution is to create an exact prefix finder and search engine, sorted by reciprocal population, and then copy all my hits, starting from exact hits, going to prefix hits. This makes paging my results a little more annoying than I think it should be.

In addition, I used the hash to eliminate duplicates, but later changed the prefix finder to a logical prefix search query (MUST) with an exact search (MUST NOT) so Lucene removes the duplicates. Although it seemed even more wasteful.

Edit: went to the comment (since the function now exists): Yuval F Thanks for the blog post ... How would the comparative comparator know that the London name field exactly matches the London search term if it cannot access the search term request?

0
source

Source: https://habr.com/ru/post/1276320/


All Articles