Skip to content

Instantly share code, notes, and snippets.

@pallocchi
Last active September 22, 2018 05:20
Show Gist options
  • Save pallocchi/5ed4821ad3f0a7f1d82972380c8c657f to your computer and use it in GitHub Desktop.
Save pallocchi/5ed4821ad3f0a7f1d82972380c8c657f to your computer and use it in GitHub Desktop.
Offline full-text search in Android & iOS
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