Last active
September 22, 2018 05:20
-
-
Save pallocchi/5ed4821ad3f0a7f1d82972380c8c657f to your computer and use it in GitHub Desktop.
Offline full-text search in Android & iOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun search(text: String): List<Movie> { | |
val movies = mutableListOf<Movie>() | |
val cursor = getDatabase().rawQuery("SELECT title, overview, poster, matchinfo(movies, 'pcnalx') FROM movies WHERE movies MATCH '$text*'", null) | |
if (cursor.moveToFirst()) { | |
do { | |
// Read and prepare matchinfo blob | |
val matchinfo = cursor.getBlob(3).toIntArray() | |
// Calculate score based on matchinfo values | |
// Here I'm only using the first column (title) to calculate the score | |
val score = OkapiBM25.score(matchinfo = matchinfo, column = 0) | |
val movie = Movie( | |
title = cursor.getString(0), | |
overview = cursor.getString(1), | |
poster = cursor.getString(2), | |
score = score) | |
movies.add(movie) | |
} while (cursor.moveToNext()) | |
} | |
cursor.close() | |
return movies.sortedByDescending { it.score } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment