Displays the highlighted attributes
of your search results.With InstantSearch Android, the Highlightable interface and HighlightedString objects simplify highlighting the correct words in a search response that match your query.See also: Get started with imperative UI
To display those movies in your interface, you likely have created a data class that looks something like the following:
Kotlin
Report incorrect code
Copy
import com.algolia.instantsearch.core.Indexable@Serializabledata class Movie( val title: String, val year: String, val genre: List<String>, override val objectID: String,) : Indexable
Update it to add some highlighting.
Implementing Highlightable will deserialize the _highlightResult for each movie, and make it available through the getHighlight{s} methods. Create @Transient attributes for each highlight to display, being either single values or lists:
Kotlin
Report incorrect code
Copy
import com.algolia.instantsearch.core.Indexable@Serializabledata class Movie( val title: String, val year: String, val genre: List<String>, override val objectID: String, override val _highlightResult: JsonObject?) : Indexable, Highlightable { @Transient public val highlightedTitle: HighlightedString? get() = getHighlight("title") @Transient public val highlightedGenres: List<HighlightedString>? get() = getHighlights("genre") @Transient public val highlightedActors: List<HighlightedString>? get() = getHighlights("actors")}
Use these highlighted strings in the interface,
for example in a MovieViewHolder.
Any way you want, iterating on HighlightedString#tokens to process it however you like:
Kotlin
Report incorrect code
Copy
// Displays actors with highlighted parts in uppercasehighlightedActors?.joinToString { highlight -> highlight.tokens.joinToString("") { if (it.highlighted) it.content.uppercase() else it.content }}
Any way you want, iterating on HighlightedString#tokens to process it however you like:
Kotlin
Report incorrect code
Copy
// Displays actors with highlighted parts in uppercasehighlightedActors?.joinToString { highlight -> highlight.tokens.joinToString("") { if (it.highlighted) it.content.uppercase() else it.content }}