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 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 )
]
, listFields = []
, initialTransformFactories = Index.Defaults.defaultInitialTransformFactories
, transformFactories = Index.Defaults.defaultTransformFactories
, filterFactories = [ createMyStopWordFilter ]
}Add a document to an index.
Starting with the ElmTextSearch.new example above this adds a document.
addDocToIndexExample : Result String (ElmTextSearch.Index ExampleDocType)
addDocToIndexExample =
ElmTextSearch.add
{ cid = "id1"
, title = "First Title"
, author = "Some Author"
, body = "Words in this example document with explanations."
}
createNewWithIndexExampleConditions that cause a result Err with message.
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 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."
}
createNewIndexExampleConditions that cause a result Err with message.
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."
}
createNewIndexExampleConditions that cause an error result are those for
ElmTextSearch.remove and
ElmTextSearch.add.
Add or Update a document in an index. This removes the document first if it is allready in index then adds it.
Search an index with query.
Tokens are extracted from the query string and passed through the same processing used when indexing documents.
Each token is expanded, so that the term "he" might be expanded to "hello" and "help" if those terms were already included in the document index.
Multiple tokens are allowed and will lead to an AND based query.
The following example runs a search for documents containing both "apple" and "banana".
searchResult =
Index.search "Apple banana" createNewIndexExampleResults are a list of matching document reference identifiers with there similarity to query score, ordered by score descending, so the best matches are earliest in the list.
An index is returned from search as well. This is because the data model may be updated to improve performance for later searches.
Adding or removing a new document will cause some of the internal caching to be reset.
Conditions that cause a result Err with message.
An Index holds the data to be able search for added documents.
A Config is required to create an Index.
A SimpleConfig is the least amount of configuration data required to create an Index.
See ElmTextSearch.new for fields.
ElmTextSearch.Json.Encoder.encoderElmTextSearch.Json.Decoder.decoder
to produce a Index.Model.CodecIndexRecord.Index.Model.CodecIndexRecord
using ElmTextSearch.Json.Encoder.codecIndexRecordEncoderStore an index to a Value.
You can also use ElmTextSearch.Json.Encoder.
Store an index to a String.
You can also use ElmTextSearch.Json.Encoder.
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.
Create an Index from a Value which has a stored Index in it.
See ElmTextSearch.fromStringWith for possible Err results.
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.
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-2017 Robin Luiten
Create new index.
Example
The
SimpleConfigparameter to new is.cid..title.body.titlefield (boost value 5.0) raise the document match score more than if found in the.bodyfield (boost value 1.0).