Introduction to Elasticlunr. js and introduction to elasticlunr. js

Source: Internet
Author: User

Introduction to Elasticlunr. js and introduction to elasticlunr. js
Elasticlunr. js

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-time 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 flexible that you cocould try different boosting scheme.
  • More rational scoring mechanic, Elasticlunr. js use quite the same scoring mechanic as Elasticsearch, and also this scoring mechanic is used by lucene.
  • Field-search, You cocould choose which field to index and which field to search.
  • Boolean Model, You cocould 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 isNoNeed 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 is no need to compute query vector and document vector, then the index file is very small, this is especially helpful when elasticlurn. js is used as offline search.
Example

A very simple search index can be created using the following scripts:

var index = elasticlunr(function () {    this.addField('title');    this.addField('body');    this.setRef('id');});

Adding documents to the index is as simple:

var doc1 = {    "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."}var doc2 = {    "id": 2,    "title": "Oracle released its profit report of 2015",    "body": "As expected, Oracle released its profit report of 2015, during the good sales of database and hardware, Oracle's profit of 2015 reached 12.5 Billion."}index.addDoc(doc1);index.addDoc(doc2);

Then searching is as simple:

index.search("Oracle database profit");

Also, you cocould do query-time boosting by passing in a configuration.

index.search("Oracle database profit", {    fields: {        title: {boost: 2},        body: {boost: 1}    }});

This returns a list of matching events 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.

WhyInstallation

Simply include the elasticlunr. js source file in the page that you want to use it. Elasticlunr. js is supported in all modern browsers.

Browsers that do not support ES5 will require a JavaScript shim for Elasticlunr. js to work. you can either use Augment. js, ES5-Shim or any library that patches old browsers to provide an ES5 compatible JavaScript environment.

Documentation

This part only contain important apects of elasticlunr. js, for the whole documentation, please go to API documentation.

1. Build Index

When you first create a index instance, you need to specify which field you want to index. If you did not specify which field to index, then no field will be searchable for your documents ENTs.
You coshould specify fields:

var index = elasticlunr(function () {    this.addField('title');    this.addField('body');    this.setRef('id');});

You coshould also set the document referencethis.setRef('id'), If you did not set document ref, elasticlunr. js will use'Id'As default.

You cocould 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 English, if you want to use elasticlunr. js to index other language documents, then you need to use elasticlunr. js combined with lunr-runtime ages.
Assume you're using lunr-language in Node. js envrionment, you cocould import lunr-language as followings:

var lunr = 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('body')});

For more details, please go to lunr-related ages.

2. Add document to index

Add document to index is very simple, just prepare you document in JSON format, then add it to index.

var doc1 = {    "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."}var doc2 = {    "id": 2,    "title": "Oracle released its profit report of 2015",    "body": "As expected, Oracle released its profit report of 2015, during the good sales of database and hardware, Oracle's profit of 2015 reached 12.5 Billion."}index.addDoc(doc1);index.addDoc(doc2);

If your JSON document contains field that not configured in index, then that field will not be indexed, which means that field is not searchable.

3. Remove document from index

Elasticlunr. js support remove a document from index, just provide JSON documentelasticlunr.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 will remove each token of that 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 documentelasticlunr.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.
You cocould setup a configuration tell elasticlunr. js how to do query-time boosting, which field to search in, how to do the boolean logic.
Or you coshould just use it by simply provide a query string, this will aslo works perfectly because the scoring mechanic is very efficient.

5.1 Simple Query

Because elasticlunr. js has a very perfect scoring mechanic, so for most of your requirement, simple search wocould be easy to meet your requirement.

index.search("Oracle database profit");

Output is a results array, each element of results array is an Object containrefField andscoreField.
refIs the document reference.
scoreIs the similarity measurement.

Results array is sorted descentscore.

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, then elasticlunr. js will only search the query string in the specified fields with boosting weight.

The scoring mechanic used in elasticlunr. js is very complex, Please goto details for more information.

index.search("Oracle database profit", {    fields: {        title: {boost: 2},        body: {boost: 1}    }});
5.2.2 Boolean Model

Elasticlunr. js also support boolean logic setting, if no boolean logic is setted, elasticlunr. js use "OR" logic defaulty. By "OR" default logic, elasticlunr. js cocould reach a highRecall.

index.search("Oracle database profit", {    fields: {        title: {boost: 2},        body: {boost: 1}    },    boolean: "OR"});

Boolean operation is saved med based on field. this means that if you choose "AND" logic, statements with all the query tokens in the query field will be returned as a field results. if you query in multiple fields, different field results will be merged together to give a final query results.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.