Lucilla - Fast, efficient, in-memory Full Text Search for Kotlin

Overview

Lucilla

Build Jitpack

Lucilla is an in-memory Full Text Search library for Kotlin.

It allows to you to build a Full Text Search index for data that does not need to be persisted to a database. You can run search queries against the index to find matching documents quickly.

import com.haroldadmin.lucilla.core.*

data class Book(
    @Id
    val id: Int,
    val title: String,
    val summary: String,
)

val index = useFts(getBooks())

index.search("Martian").map { searchResult ->
    val bookId = searchResult.documentId
    val book = getBook(bookId)
    // Show search result to the user
}

Lucilla is in active development. It's an early stage prototype, and not suitable for production use yet.

Features

  • PATRICIA Trie based space efficient FTS index
  • Advanced text processing pipeline with support for Tokenization, Stemming, Punctuation removal and more.
  • Extensible text processing with custom pipeline steps
  • Search results ranking using TF-IDF scores
  • Customisable document parsing with ability to ignore unwanted fields

While lucilla has you covered on most of the basic features, support for some advanced features is missing (but planned):

  • Fuzzy searching
  • Custom field boosts
  • Async processing

Usage

Modelling Data

To use lucilla's FTS capabilities, you must first model your data as a class.

We recommend using data classes for this purpose, but anything should work as long as it satisfies the following requirements:

  • Must have a @Id marked field that can be parsed as an Int
  • Must have one or more other properties that can be parsed as Strings
import com.haroldadmin.lucilla.core.Id

data class Book(
  @Id
  val id: Int,
  val title: String,
  val summary: String,
)

If you don't want lucilla to index some fields of your document, annotate them with @Ignore.

import com.haroldadmin.lucilla.core.Id
import com.haroldadmin.lucilla.core.Ignore

data class Book(
    @Id
    val id: Int,
    val title: String,
    val summary: String, 
    @Ignore
    val publisher: String,
)

Create the Index

Create an FTS index and add your data to it:

val index = useFts<Book>()
getBooks().forEach { index.add(it) }

// You can also pass your seed data directly
val books = getBooks()
val index = useFts<Book>(books)

Adding documents to the index, or creating the index with seed data is a potentially expensive process depending on how large each document is. It's best to perform this process on a background thread or Coroutine.

Search the Index

Send your queries to the index to get search results ordered by relevance.

val searchResults = index.search(query)
val books = searchResults.map { r -> r.documentId }.map { id -> getBook(id) }
showResults(books)

Lucilla runs every search query through a text processing pipeline to extract searchable tokens from it. The tokens may not reflect the search query exactly. To find which token of your search query matched with a given search result, use the "matchTerm" property on a search result.

Installation

Add the Jitpack repository to your list of repositories:

// Project level build.gradle file
allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

And then add the dependency in your gradle file:

// Module build.gradle file
dependencies {
    implementation "com.github.haroldadmin.lucilla:core:(latest-version)"
}

Jitpack

Contributing

lucilla is in active development and does not promise API stability. Expect the library to undergo significant changes before it reaches stable status.

We encourage the community to contribute features and report bugs.

Meta

The name 'lucilla' is inspired from the name of Sebastian Vettel's 2020 Ferrari. It also sounds similar to lucene (from Apache Lucene), which is the industry standard full text search framework.

lucilla's implementation borrows from a JavaScript library MiniSearch.

License

MIT License

Copyright (c) 2022 Kshitij Chauhan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Comments
  • Add ability to restrict search to selected props

    Add ability to restrict search to selected props

    This PR adds the ability to restrict search queries to specific properties of a document when needed.

    • This capability is different than applying @Ignore on fields to exclude from indexing.
    • This PR also adds the base for a fancy DSL to build queries

    Fixes #10

    enhancement 
    opened by haroldadmin 0
  • Add ability to limit search query to specific document properties

    Add ability to limit search query to specific document properties

    Allow users to search the index for specific properties of a document only.

    For example, the ability to search only the titles of indexed books, even if other properties are indexed too:

    data class Book(
      @Id val id: Int,
      val title: String,
      val description: String,
    )
    
    val index = useFts(getBooks())
    
    // Only return results that contain "recursion" in the book title
    val results = index.search("recursion", Book::title)
    
    enhancement 
    opened by haroldadmin 0
  • Add support for autocompletion suggestions

    Add support for autocompletion suggestions

    This PR adds the ability to query the FTS index for autocompletion suggestions.

    • Uses prefix search to find keys with the input query as a prefix
    • Each prefix result is treated as a suggestion
    • The suggestions are ranked by their levenshtein distance from the query, normalized by the suggestion length

    Autocompletion suggestions might be unexpected with a text processing pipeline that includes stemming as a part of it.

    Fixes #8

    enhancement 
    opened by haroldadmin 0
  • Add support for auto-complete suggestions

    Add support for auto-complete suggestions

    Add ability to fetch auto completion suggestions for a search query.

    A simple autocomplete suggestions provider can be built using prefix search. For a search query, return matching words in the index that contain the query as a prefix.

    enhancement 
    opened by haroldadmin 0
  • Add levenshtein distance implementation

    Add levenshtein distance implementation

    This PR adds a utility function ld to calculate the Levenshtein Distance between two strings. It supports customizable weights for replace/delete/insert operations.

    This will be useful for the sorting the results of the upcoming autocomplete suggestions feature.

    enhancement 
    opened by haroldadmin 0
  • Switch to positional postings list based FTS index

    Switch to positional postings list based FTS index

    Switch to positional postings list based FTS index

    • Add a new Posting class that stores index information for a token, including the document ID, property name, property length, and positional offsets
    • Add IR functions to extract postings from a document
    • Modify the scoring algorithm to use postings

    The search result scoring algorithm is non-optimal currently, as it does not take advantage of property specific searching or proximity boosts. Improvements for this are planned in future releases.

    Fixes #5

    enhancement 
    opened by haroldadmin 0
  • Switch to Positional Postings List in the Index

    Switch to Positional Postings List in the Index

    Lucilla's FTS index stores a map of documents and frequencies for each term, which can also be used to calculate the document frequency.

    Modify the index to also store the starting position/offset for each term in the documents.

    enhancement 
    opened by haroldadmin 0
  • Switch to FTS index divided by document fields

    Switch to FTS index divided by document fields

    This change allows the FTS index to index data per document property.

    • Index changes from Map<String, Map<Int, Int> (token to document frequencies) - to Map<String, Map<String, Map<Int, Int>> (token to property based document frequencies).
    • Store property-based document lengths for scoring.
    • Benchmarks confirm no noticeable loss of performance.
    • Unit tests confirm no loss of functionality.

    Fixes #3.

    enhancement 
    opened by haroldadmin 0
  • Divide the FTS index per document field

    Divide the FTS index per document field

    Lucilla treats every document as a blob of text, which does not work well for field-specific or field boosts. Storing field related information in the document would make it easier to implement such functionality.

    enhancement 
    opened by haroldadmin 0
  • Add @Include as an option to @Ignore

    Add @Include as an option to @Ignore

    Some of my data classes are huge and adding @Ignore to every field I didn't want to search would be tedious, as there are only a few fields I'd like to search with, maybe an @Include would be good?

    opened by austinhodak 1
Releases(0.2.0)
  • 0.2.0(Feb 19, 2022)

    A new release for your friendly neighbourhood FTS library.

    Version 0.2.0 brings the ability to query Lucilla for autocompletion suggestions.

    val index = useFts(someData)
    
    search.onQuery { query ->
      val suggestions = index.autocomplete(query)
      showSuggestions(suggestions)
    }
    

    Build a custom pipeline that does not stem input documents to avoid unexpected suggestions.

    Source code(tar.gz)
    Source code(zip)
  • 0.1.0(Jan 30, 2022)

    This release contains improvements to setup Lucilla for more sophisticated scoring algorithms, as well as richer querying capabilities.

    Features:

    • Implement a positional postings based full text search index. Positional postings allow storing information about token offsets in a document per property.

    Changes:

    • Move to a modular project structure. This change does not affect how you install Lucilla. You should still depend on the ':core' module for most purposes.
    • Move to Gradle Versions Catalogue based dependency organization
    • Move from Adopt OpenJDK to Termurin in CI builds
    • Add benchmarks to track performance regressions using kotlinx.benchmarks and JMH.

    Please report any bugs you find!

    Lucilla is an early stage project under active development. We make no promises about API compatibility until we hit 1.0

    Source code(tar.gz)
    Source code(zip)
Owner
Kshitij Chauhan
Backend Engineer | Native Android | Open Source Enthusiast
Kshitij Chauhan
A basic, incomplete, buggy, far from efficient UI toolkit for Kotlin/Android. An experiment for fun and to learn.

Apex Apex is just a simple proof of concept to demonstrate how easily you can build your own UI Toolkit from scratch. This code base is most likely fu

Romain Guy 79 Sep 7, 2022
A basic, incomplete, buggy, far from efficient UI toolkit for Kotlin/Android. An experiment for fun and to learn.

Apex Apex is just a simple proof of concept to demonstrate how easily you can build your own UI Toolkit from scratch. This code base is most likely fu

Romain Guy 79 Sep 7, 2022
A simple plugin to patch the memory leak in Kotlin Gradle Plugin 1.5.0

kgp-150-leak-patcher A simple plugin to automatically patch the memory leak in Kotlin Gradle Plugin 1.5.0 in https://youtrack.jetbrains.com/issue/KT-4

Zac Sweers 40 Dec 20, 2021
MemoryGame - An Android memory game with customizable options

MemoryGame An Android memory game with customizable options Open source librarie

null 1 Feb 3, 2022
Kotlin TodoMVC – full-stack Kotlin application demo

Kotlin full stack TodoMVC This project is an example implementation of the TodoMVC app written in Kotlin. More specifically, it's the Kotlin port of t

Gyula Voros 22 Oct 3, 2022
A repository full of Forge 1.8.9 Kotlin utilities

kotlin-forge-api kotlin-forge-api is a repository full of different APIs to be used by mods for Forge 1.8.9 to make modding easier! Warning! To use an

Shalom Ademuwagun 7 Sep 28, 2022
Full stack examples of how to use Hotwire JS in Kotlin services

hotwire-kt A collection of Kotlin examples using the Hotwire JS framework to build interactive web apps with a Kotlin Armeria server backend. Using Ho

Andrew (Paradi) Alexander 9 Dec 14, 2022
🧶 Full-fledged Kotlin client for MikaBot/cluster-operator as a separate package

?? Eri Full-fledged Kotlin client for MikaBot/cluster-operator as a separate package Usage Connecting to operator fun main(args: Array<String>) {

Nino 3 Nov 17, 2021
AndroidIDE - an IDE for Android to develop full featured Android apps on Android smartphones.

AndroidIDE - an IDE for Android to develop full featured Android apps on Android smartphones.

Akash Yadav 615 Dec 27, 2022
🗼 yukata (浴衣) is a modernized and fast GraphQL implementation made in Kotlin

?? yukata 浴衣 - Modernized and fast implementation of GraphQL made in Kotlin yukata is never meant to be captialised, so it'll just be yukata if you me

Noel 5 Nov 4, 2022
A simple store project that includes a list of products, search on products, details of the product, and review submission.

AdidasTest A simple store project that includes a list of products, search on products, details of the product, and review submission. Summary Technol

Mohammad 5 May 8, 2021
Search in the categories of movies, music, e-books and podcasts with the keyword

CaseStudy In this application, you can search in the categories of movies, music, e-books and podcasts with the keyword you want, add them to your fav

Abdullah Furkan Titiz 4 Dec 6, 2021
Search image app [Adanian Labs Interview], Android developer role

Pixar This is App shows you Images from the Pixabay API Table of Contents Functionalities Approach Screenshots How To Setup Libraries Used Author Info

Abdulfatah Mohamed 2 Dec 20, 2022
An Android app consuming Pixabay API to search and display list of images, built with MVVM pattern

Images An Android app consuming Pixabay API to search and display list of images, built with MVVM pattern as well as Architecture Components. Min Api

Ronnie Otieno 8 Sep 21, 2022
Application to allow user to search his favourite repositories from Github.

Github Search Repositories App details: The application contain one screen with a search field and list of repositories. After the user inputs, the se

Arsalan Mehmood 1 Nov 26, 2022
PokeDexApi is a simple version of PokeApi with search functionality.

PokeDex Api PokeDexApi is a simple version of PokeApi with search functionality based on KTOR. Documentation Base Url https://poki-dex.herokuapp.com E

Rohit Sharma 1 Jan 15, 2022
A podcast proxy that sits between itunes search api and android apps allowing normalization of rss feeds to standard Json format that can be consumed by apps.

Podcasts Rss Feeds Search Proxy A podcast proxy written using kotlin dsl that sits between itunes search api, podcasts rss feeds and android apps allo

8BitsLives .❤️ 2 Nov 27, 2022
Add Ios Swipe Search TextField Component in Android Jetpack Compose.

IosSwipeSearchCompose Add Ios Swipe Search TextField Component in Android Jetpack Compose. How it looks Usage val text = remember { mutableStateOf("")

Emir Demirli 11 Jan 9, 2023