Elasticlunr.js
Project Address: http://elasticlunr.com/
Code Address: Https://github.com/weixsong/elasticlunr.js
Document Address: http://elasticlunr.com/docs/index.html
Elasticlurn.js is a lightweight full-text search engine in Javascript for browser search and offline search.
Elasticlunr.js is developed based on Lunr.js, but more flexible than lunr.js. Elasticlunr.js provides query-time boosting and field search.
Elasticlunr.js is a bit like SOLR, but much smaller and not as bright, but also provide flexible configuration and query-t IME boosting.
Key Features comparing with Lunr.js
- Query-time boosting, you don ' t need to setup boosting weight in index building procedure, this make it more flexi ble that you could try different boosting scheme.
- More rational scoring mechanism, elasticlunr.js use quite the same scoring mechanism as Elasticsearch, and also t His scoring mechanism are used by Lucene.
- Field-search, could choose which field to index and which field to search.
- Boolean model, could set which field to search and the Boolean model for each query token, such as "OR", "and ”.
- Combined Boolean model, TF/IDF model and the Vector Space model, make the results ranking more reliable.
- Fast, elasticlunr.js removed Tokencorpus and Vector from Lunr.js, by using combined model there is no ne Ed to compute the vector of a document and query string to compute similarity of query and matched document, this improve The search speed significantly.
- Small Index file, Elasticlunr.js did not store Tokencorpus because there are no need to compute query vector and D Ocument Vector, then the "index" file is very small, the "is" especially helpful when elasticlurn.js is used as offline Searc H.
Example
A very simple search index can created using the following scripts:
var index = elasticlunr(function () { this.addField(‘title‘); this.addField(‘body‘); this.setRef(‘id‘);});
Adding documents to the index are as simple as:
varDoc1 = {"id":1,"title":"Oracle released its latest database Oracle 12g","Body":"Yestaday Oracle has released it new database Oracle 12g, this would make + Ice profit report of annual year. "}varDOC2 = {"id":2,"title":"Oracle released its profit","Body":As expected, Oracle released its profit, during the good sales of database and hardware, Oracle ' s Prof It's reached 12.5 billion. "}index.adddoc (Doc1); Index.adddoc (DOC2);
Then searching are as simple:
index.search("Oracle database profit");
Also, you could does query-time boosting by passing in a configuration.
index.search("Oracle database profit", { fields: { 2}, 1} }});
This returns a list of matching documents with a score of how closely they match the search query:
[{ "ref"1, "score"0.5376053707962494},{ "ref"2, "score"0.5237481076838757}]
API documentation is available, as well as a full working example.
Description
Elasticlunr.js is developed based on Lunr.js, but more flexible than lunr.js. Elasticlunr.js provides query-time boosting and field search.
A bit like SOLR, but much smaller and not as bright, but also provide flexible configuration and query-time boosting.
Why
- In the some system, you do not want to deploy any Web Server (such as Apache, Nginx, etc.), and you are only provide some static Web pag ES and provide search function in client side. Then you could the build index in previous and load the index in client side.
- Provide offline search functionality. For some documents, user usually download these documents, your could build index and put index in the documents package, T Hen provide offline search functionality.
- For some limited or restricted network, such WAN or LAN, offline Search is a better choice.
- For mobile device, Iphone or Android phone, network traffic maybe very expensive, then provide offline search is a good ch Oice.
Installation
Simply include the elasticlunr.js source file in the page so you want to use it. Elasticlunr.js is supported in all modern browsers.
Browsers that does not support ES5 would require a JavaScript shim for elasticlunr.js to work. Can either use Augment.js, Es5-shim or any library that patches old browsers to provide an ES5 compatible JavaScript E Nvironment.
Documentation
This is only contain important apects of elasticlunr.js, for the whole documentation, and go to API documentation.
1. Build Index
When you first create an index instance, you need to specify which field you want to index. If you didn't specify which field to index and then no field would be searchable for your documents.
You could specify:
var index = elasticlunr(function () { this.addField(‘title‘); this.addField(‘body‘); this.setRef(‘id‘);});
Could also set the document reference this.setRef(‘id‘)
by, if you do not set document REF, Elasticlunr.js'll use ' id ' As default.
You could do the above index setup as followings:
var index = elasticlunr();index.addField(‘title‘);index.addField(‘body‘);index.setRef(‘id‘);
Default supported language of Elasticlunr.js is 中文版, if you want to use Elasticlunr.js to index other language documen TS, then you need-elasticlunr.js combined with lunr-languages.
Assume you ' re using lunr-language in node. js Envrionment, you could import lunr-language as followings:
var lunr = Span class= "hljs-built_in" >require ( './lib/lunr.js ' ); require (/lunr.stemmer.support.js ' ) (LUNR); require (/lunr.de.js ' ) (LUNR); var idx = LUNR (function () { //Use the Language (DE)
this . Use (lunr.de);
//then, the normal LUNR index initialization
this . Field (
' title ' )
this . Field (
)});
For more details, please go to lunr-languages.
2. Add Document to Index
Add document to index are very simple, just prepare your document in JSON format and then add it to index.
varDoc1 = {"id":1,"title":"Oracle released its latest database Oracle 12g","Body":"Yestaday Oracle has released it new database Oracle 12g, this would make + Ice profit report of annual year. "}varDOC2 = {"id":2,"title":"Oracle released its profit","Body":As expected, Oracle released its profit, during the good sales of database and hardware, Oracle ' s Prof It's reached 12.5 billion. "}index.adddoc (Doc1); Index.adddoc (DOC2);
If your JSON document contains field that isn't configured in index, then that field would not is indexed, which means that F Ield is not searchable.
3. Remove document from Index
Elasticlunr.js support Remove a document from index, just provide JSON document to elasticlunr.Index.prototype.removeDoc()
function.
For example:
var doc = { "id"1, "title""Oracle released its latest database Oracle 12g", "body""Yestaday Oracle has released its new database Oracle 12g, this would make more money for this company and lead to a nice profit report of annual year."}index.removeDoc(doc);
Remove a document would remove each token of this document ' s each field from Field-specified Inverted index.
4. Update a document in index
Elasticlunr.js Support Update a document in index, just provide JSON document to elasticlunr.Index.prototype.update()
function.
For example:
var doc = { "id"1, "title""Oracle released its latest database Oracle 12g", "body""Yestaday Oracle has released its new database Oracle 12g, this would make more money for this company and lead to a nice profit report of annual year."}index.update(doc);
5. Query from Index
Elasticlunr.js provides flexible query configuration, supports Query-time boosting and Boolean logic setting.
Could setup a configuration tell elasticlunr.js how to do query-time boosting, which field to search Boolean logic.
Or you could just with it by simply provide a query string, this would aslo works perfectly because the scoring mechanism is Very efficient.
5.1 Simple Query
Because Elasticlunr.js have a very perfect scoring mechanism, so for most of the your requirement, simple search would is easy To meet your requirement.
index.search("Oracle database profit");
Output is a results array with each element of results array is an Object contain a ref
field and a score
field.
ref
is the document reference.
score
is the similarity measurement.
Results array is sorted descent by score
.
5.2 Configuration Query5.2.1
Query-time Boosting
Setup which fields to search in by passing in a JSON configuration, and setup boosting for each search field.
If you setup this configuration and then Elasticlunr.js would only search the query string in the specified fields with Boosti ng weight.
The scoring mechanism used in elasticlunr.js are very complex, please goto details for more information.
index.search("Oracle database profit", { fields: { 2}, 1} }});
5.2.2
Boolean Model
Elasticlunr.js also support Boolean logic setting, if no Boolean logic was setted, elasticlunr.js use "OR" logic defaulty. By ' OR ' default logic, Elasticlunr.js could reach a high Recall.
index.search("Oracle database profit", { fields: { 2}, 1} }, "OR"});
Boolean operation is performed based on field. This means so if you choose "and" logic, documents with the query tokens in the query field would be returned as a fi ELD results. If you query in multiple fields, different field results'll be merged together to give a final query results.
Elasticlunr.js Brief Introduction