Indexing Tutorial

Let's setup a collection of books to search over:

case class Book(author: String, title: String)

val books: List[Book] = List(
  Book("Beatrix Potter", "The Tale of Peter Rabbit"),
  Book("Beatrix Potter", "The Tale of Two Bad Mice"),
  Book("Dr. Seuss", "One Fish, Two Fish, Red Fish, Blue Fish"),
  Book("Dr. Seuss", "Green Eggs and Ham"),
)

In order to index our domain type Book, we'll need a few things:

We'll pass all these things to an IndexBuilder:

import pink.cozydev.protosearch.{Field, IndexBuilder}
import pink.cozydev.protosearch.analysis.Analyzer

val analyzer = Analyzer.default.withLowerCasing
val indexBldr = IndexBuilder.of[Book](
  (Field("title", analyzer, stored=true, indexed=true, positions=true), _.title),
  (Field("author", analyzer, stored=true, indexed=true, positions=false), _.author),
)

And then we can finally index our books using the builder:

val index = indexBldr.fromList(books)

Finally we'll then need a search function to test out. We use a queryAnalyzer with the same default field here to make sure our queries get the same analysis as our documents did at indexing time.

val qAnalyzer = index.queryAnalyzer

def search(q: String): List[Book] =
  index.search(q)
    .map(hits => hits.map(h => books(h.id)))
    .fold(_ => Nil, identity)

Now we can use our search function to explore some different query types!