Giter VIP home page Giter VIP logo

datascript-fulltext's Introduction

Fulltext Index Search Adapter for DataScript in ClojureScript

I needed fulltext search in a text-heavy application that uses DataScript.

Is it any good?

Yes, but it's still early.

Design

The search adapter maintains a fulltext index in a separate DataScript database. This is not ideal, but the current design of Datomic & DataScript do not support extensible indices.

The fulltext adapter:

  1. listens for changes in the source connection using (d/listen! conn),
  2. inspects the incoming tx-report,
  3. filters on attributes that have :db/fulltext true in their schema,
  4. tokenises the string value,
  5. removes stop words like "the" and "and",
  6. maintains a multi-cardinality attribute in the fulltext DataScript DB.

Using a separate connection makes it convenient to have a one-to-one attribute mapping and to manage cache eviction since it could grow large. In practice this is not an issue because you can query across DataScript databases, e.g. (d/q '[:in $ $1 ...] db1 db2).

Usage

(ns datascript-fulltext.example
    :require [[reagent.core :as r :refer [atom]]
              [datascript.core :as d]
              [com.theronic.datascript.fulltext :as ft]])

(def conn (d/create-conn {:message/text {:db/fulltext true}})
(def !ft-conn (atom nil))

(defn parent-component [conn]
    (let [!input (atom "hi")] ;; todo text input
      [:div [:code "Matching fulltext entities: " (ft/query! @!input)]]))

(defn init! []
  (let [fulltext-conn (ft/install-fulltext! conn)] 
    (d/transact! conn [{:db/id -1 :message/text "hi there"}]) ;; load from storage after connecting to sync.
    (ft/search @ft/ft-conn "hi") ;; => fill yield message ID.
    (reset! !ft-conn fulltext-conn))
  (reagent/render [parent-component conn]))
  
(init!)

Todo

  1. Delete index values on mutation. Should be quick to add, and then do a smart diff to avoid writes.
  2. Store hashed token values instead of strings.
  3. Use schema definition of source connection.
  4. Track source schema and rebuild index on change.
  5. Batch updates using queued web workers to prevent locking main thread.
  6. Add adapter for off-site storage, e.g. Redis.
  7. Match source transaction IDs if possible.
  8. Fork & extend Datascript to support (fulltext ...) search function.
  9. [in progress] Add soundex or double-metaphone
  10. Maintain indexed token counts for relevance ranking. Maybe :db/index does this already?
  11. Support n-grams (can get heavy).
  12. Bloom filters.
  13. seq over matching datoms directly with pagination.

datascript-fulltext's People

Contributors

theronic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

jiasws

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.