Search code examples
androidsqliteandroid-sqliteandroid-search

Sorting search results (from Android SearchView query) by number of matches


i'm trying to find a good way to sort the search results according to relevance after performing a search with a SearchView in Android. For me relevance means the number of matches in two SQLite text columns

I'm using a CursorLoader and there the sort order can be given to the constructor at the end

CursorLoader tLoader = new CursorLoader(
    getActivity(), ContentProviderM.ARTICLE_CONTENT_URI,
    tProj, tSel, tSelArgs, SORT_ORDER);

(or set using the setSortOrder (String sortOrder) method)

But i need more flexibility than this because i'm looking to sort on the number of matches rather than just on one or two columns

The only solution i can see myself is to add another column in my SQLite table, do some processing, and supply that column as the sort column to the CursorLoader

Now for my question: What is the best way to supply the sort order information to the CursorLoader using SQLite syntax, avoiding having to add a new column? (And what could this SQLite code look like?) Also, i'd like to ask more in general: Is there a different solution to this problem that i've missed?

Grateful for any help! And with kind regards, Tord


Solution

  • i found a "workaround" for this problem.

    After investigating different ways to write sqlite code i ended up just adding a new table column just for sorting. This column simply stores an integer and is updated every time that the user performs a search, right before the CursorLoader is created

    Advantages:

    • We can now do all of the relevance calculations in Java code

    Drawbacks:

    • Relevance calculation is done as the search is done so if we have a large number of items it may take some time to process everything