I’m using NHibernate to do a search on item name. I’m getting a paged list of items back from the database, and I’m ordering it by item name ascending.
So, if I search for ‘term’, I get back a page of results that contains ‘term’ anywhere in the result, and the page is ordered alphabetically.
For example, the results might look like this
a term
d term
g term
j term
p term
s term
term 1
term 2
v term
z term
Technically this list is correct, because it’s ordered alphabetically.
However, technically correct is not good enough for the client. They want the list to be ordered first by the relevance of the term being searched for, and then alphabetically.
So the result would become
term 1
term 2
a term
d term
g term
j term
p term
s term
v term
z term
I don’t know how to construct this query, or if its even possible. If this could be done in the application it would be much easier, but because of the paging, this has to be done on the database. This is what complicates things. Could somebody please point me in the right direction here?
If it’s possible to write a function that returns an integer based on the relevancy, you could do
Your function wouldn’t have to be very complicated. It could just look to see if the Field Starts with the SearchTerm and return 2 and if it doesn’t return 1. Then you could expand it if you have more criteria of relevancy.
Or if by relevancy you mean it starts with the search term, you could also split your DB query into two and union them together.
i.e.
It’s not going to be good for your performance though.