2

I'm having some issues on how I control the way Bloodhound and Twitter Typeahead sorts the results that are displayed in the typeahead.

I'm using the typeahead to display suggested addresses and cities, meaning that someone could either search for "New York" or "Manhattan New York" or "55th Avenue Manhattan New York".

When a user searches for "New York" I would prefer that Typeahead gave "New York" as the first suggestion which is the closest match. Currently if the user search for example "New York", the first suggestion is "55h Avenue Manhattan New York", "22 Street Manhattan New York" and so on.

Anyone can point me in the right direction of how I achieve this kind of sorting?

1 Answer 1

0

I probably missunderstood what Bloodhound was for. I thought Bloodhound was used to sort the results in a clever way, and that there were some kind of way you could select sorting algoritm through Bloodhound. I guess I was wrong.

The solution is to sort the whole thing before it fetches it. In this case during the SQL Query of the database. I ordered in the following way to get what I wanted:

SELECT address, city FROM house WHERE city LIKE :query OR address LIKE :query GROUP BY city ORDER BY CASE 
                                    WHEN city LIKE :query1 THEN 0
                                    WHEN city LIKE :query2 THEN 1
                                    WHEN city LIKE :query3 THEN 2
                                    ELSE 3
                                    END, city

Where query1, query2 and query3 were variations of the parameter. For example "cityName%", "% %cityName% %", "% cityName".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.