rluiten/elm-text-search/2.1.1

NOTE: this package is not compatible with Elm 0.19.1

ElmTextSearch

A full text indexer written in Elm language inspired by lunr.js.

A useful article about lunr.js https://www.new-bamboo.co.uk/blog/2013/02/26/full-text-search-in-your-browser/

Create Index

new : SimpleConfig doc -> Index doc

Create new index.

Example

import ElmTextSearch

type alias ExampleDocType =
    { cid : String
    , title : String
    , author : String
    , body : String
    }


createNewIndexExample : ElmTextSearch.Index ExampleDocType
createNewIndexExample =
  ElmTextSearch.new
    { ref = .cid
    , fields =
        [ ( .title, 5.0 )
        , ( .body, 1.0 )
        ]
    }

The SimpleConfig parameter to new is

  • ref * The unique document reference will be extracted from each document using .cid.
  • fields * The following fields will be indexed from each document
    • .title
    • .body * When searching the index any word matches found in the .title field (boost value 5.0) raise the document match score more than if found in the .body field (boost value 1.0).
    • The document match score determines the order of the list of matching documents returned.
newWith : Config doc -> Index doc

Create new index with additional configuration.

Example.

import ElmTextSearch
import Index.Defaults
import StopWordFilter


type alias ExampleDocType =
    { cid : String
    , title : String
    , author : String
    , body : String
    }


createMyStopWordFilter =
    StopWordFilter.createFilterFuncWith
      [ "explanations" ]


createNewWithIndexExample : ElmTextSearch.Index ExampleDocType
createNewWithIndexExample =
  ElmTextSearch.newWith
    { indexType = "ElmTextSearch - Customized Stop Words v1"
    , ref = .cid
    , fields =
        [ ( .title, 5.0 )
        , ( .body, 1.0 )
        ]
    , transformFactories = Index.Defaults.defaultTransformFactories
    , filterFactories = [ createMyStopWordFilter ]
    }

Modify Index

add : doc -> Index doc -> Result String (Index doc)

Add a document to an index.

Starting with the ElmTextSearch.new example above this adds a document.

addDocToIndexExample =
    ElmTextSearch.add
      { cid = "id1"
      , title = "First Title"
      , author = "Some Author"
      , body = "Words in this example document with explanations."
      }
      createNewWithIndexExample

Conditions that cause a result Err with message.

  • Error document ref is empty.
  • Error after tokenisation there are no terms to index.
  • Error adding document that allready exists.
addDocs : List doc -> Index doc -> ( Index doc, List ( Int, String ) )

Add multiple documents. Tries to add all docs and collects errors.. It does not stop adding at first error encountered.

The result part List (Int, String) is the list of document index and the error string message result of adding. Returns the index unchanged if all documents error when added. Returns the updated index after adding the documents.

remove : doc -> Index doc -> Result String (Index doc)

Remove a document from an index.

Starting with the ElmTextSearch.new example above this removes a document.

removeDocFromIndexExample =
    ElmTextSearch.remove
      { cid = "123"
      , title = "Examples of a Banana"
      , author = "Sally Apples"
      , body = "Sally writes words about a banana."
      }
      createNewIndexExample

Conditions that cause a result Err with message.

  • Error document has an empty unique id (ref).
  • Error document is not in index.
update : doc -> Index doc -> Result String (Index doc)

Update a document in an index.

Starting with the ElmTextSearch.new example above this updates a document.

    updatedIndex =
      ElmTextSearch.update
        { cid = "123"
        , title = "Examples of a Bananas in every day life."
        , author = "Sally Apples"
        , body = "Sally writes more words about a banana."
        }
        createNewIndexExample

Conditions that cause an error result are those for ElmTextSearch.remove and ElmTextSearch.add.

Query Index

Types

type alias Index doc =
Index doc

An Index holds the data to be able search for added documents.

type alias Config doc =
Config doc

A Config is required to create an Index.

type alias SimpleConfig doc =
{ ref : doc -> String
, fields : List ( doc -> String, Float )
}

A SimpleConfig is the least amount of configuration data required to create an Index.

Save and Load an Index

storeToValue : Index doc -> Value

Store an index to a Value.

You can also use ElmTextSearch.Json.Encoder.

storeToString : Index doc -> String

Store an index to a String.

You can also use ElmTextSearch.Json.Encoder.

fromString : SimpleConfig doc -> String -> Result String (Index doc)

Create an Index from a String which has a stored Index in it and the supplied basic configurations.

See ElmTextSearch.fromStringWith for possible Err results.

fromValue : SimpleConfig doc -> Value -> Result String (Index doc)

Create an Index from a Value which has a stored Index in it.

See ElmTextSearch.fromStringWith for possible Err results.

fromStringWith : List (Config doc) -> String -> Result String (Index doc)

Create an Index from a String which has a stored Index in it.

If none of the indexVersion in the list of Config match the index type being loaded it will return an Err.

The list of configurations wil be searched for a matching indexType so you should provide configs for all types you may be trying to load. No more than the config that matches is required though.

If the none of the supplied Config match the loaded Index then it will try if the index being loaded matches the default version if so it will still load the index.

The following Err results may be returned.

  • "Error cannot load Index. Tried to load index of type "__IndexTest Type -". It is not in supported index configurations." * It contains the loaded version index type which comes from input.
  • "Error cannot load Index. Version supported is 1.0.0. Version tried to load is 1.0.1." * It includes both expected and loaded versions which may vary.
fromValueWith : List (Config doc) -> Value -> Result String (Index doc)

Create an Index from a String which has a stored Index in it.

If none of the indexVersion in the list of SimpleConfig match the index being decoded it will return an Err.

See ElmTextSearch.fromStringWith for possible Err results.

Copyright (c) 2016 Robin Luiten