Elasticlunr.js

项目地址:http://elasticlunr.com/
代码地址:https://github.com/weixsong/elasticlunr.js
文档地址: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-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 could try different boosting scheme.
  • More rational scoring mechanism, Elasticlunr.js use quite the same scoring mechanism as Elasticsearch, and also this scoring mechanism is used by lucene.
  • Field-search, you could choose which field to index and which field to search.
  • Boolean Model, you 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 need 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 as:

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 could 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 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

  1. In some system, you don’t want to deploy any Web Server(such as Apache, Nginx, etc.), you only provide some static web pages and provide search function in client side. Then you could build index in previous and load index in client side.
  2. Provide offline search functionality. For some documents, user usually download these documents, you could build index and put index in the documents package, then provide offline search functionality.
  3. For some limited or restricted network, such WAN or LAN, offline search is a better choice.
  4. For mobile device, Iphone or Android phone, network traffic maybe very expensive, then provide offline search is a good choice.

Installation

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.
You could specify fields by:

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

You could also set the document reference by this.setRef('id'), if you did not set document ref, elasticlunr.js will 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 English, if you want to use elasticlunr.js to index other language documents, then you need to use 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 = 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 initializationthis.field('title')this.field('body')
});

For more details, please go to lunr-languages.

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 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 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 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.
You could 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 could just use it by simply provide a query string, this will aslo works perfectly because the scoring mechanism is very efficient.

5.1 Simple Query

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

index.search("Oracle database profit");

Output is a results array, 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 Query

5.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 mechanism 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 could reach a high Recall.

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

Boolean operation is performed based on field. This means that if you choose “AND” logic, documents 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.

Elasticlunr.js 简单介绍相关推荐

  1. 第一次预习作业(html,css,js简单介绍,HTML常用标签)

    目录 一.HTML,CSS,JS,简单介绍 1.HTML 2.CSS 3.JS 二.HTML常用标签 一.HTML,CSS,JS,简单介绍 1.HTML HTML 是用来描述网页的一种语言. HTML ...

  2. html,css,js简单介绍,html常用标签

    目录 简单介绍 HTML常用标签 1.html骨架标签 2.标题标签 3.段落标签 4.换行标签 5.文本格式化标签 6.div标签,span标签和pre标签 7.图像标签 8.超链接标签 9.注释标 ...

  3. JavaScript(一)js简单介绍

    JavaScript JS历史简述: javascript 是 netscape 网景公司 的  布兰德·艾奇  研发的, 网景要求  布兰德·艾奇 10天开发出来一个与Java相似 但要比java简 ...

  4. HTML、CSS、JS简单介绍以及HTML的标签

    目录 一.HTML--结构 1.了解网页 2.了解HTML 二.CSS--样式 1.CSS-网页的美容师 2.CSS 的规则 3.CSS的格式 三.JavaScript--行为 1.JavaScrip ...

  5. [事件处理] js实现的文本框内容发生改变立马触发事件简单介绍

    js实现的文本框内容发生改变立马触发事件简单介绍: 本章节介绍一下如何在文本框的内容发生变化的时候,立马触发一个事件执行响应的操作,而不是像是keydow或者keyup事件一样,只能够检测通过键盘输入 ...

  6. NodeJS环境搭建以及运行Node.js项目、饿了么ui(elementui)开发模式简单介绍

    一.Node.js是什么? 1.1 Node.js是一个基于Chrome V8引擎的[JavaScript运行环境]. Node.js使用了一个事件驱动.非阻塞式I/O 的模型. 1.2 Node.j ...

  7. Vue.js 内置了10个过滤器,下面简单介绍它们的功能和用法。

    Vue.js 内置了10个过滤器,下面简单介绍它们 的功能和用法. ①capitalize:字符串首字符转化成大 写 ②uppercase:字符串转化成大写 ③lowercase :字符串转化成小写 ...

  8. 简单介绍JS与JSP的区别

    参考了一些网上的资料,总结了一下 1.JSP全称是java server page    JS全称是javaScript 2.最主要的区别是运行位置不同. JSP运行在后台服务器上,混合在HTML中的 ...

  9. 总结Vue第一天~简单介绍、基本知识、辅助函数和js数组的高阶函数

    目录 vue中文官网 一.简单介绍: (1)vue.js :本质就是一个js 核心类库[跟咱使用的其他组件插件而安装他们]: ■ 安装方式: (2)小demo了解一下vue.js: (3)响应式: 二 ...

最新文章

  1. 文件重定向(hook IRP_MJ_CREATE)
  2. java 实现生产者-消费者模式
  3. Redis发布与订阅(pub/sub)
  4. 拥抱 Java 8 并行流吧,速度飞起!
  5. Python 学习记录1
  6. 一个web项目在myeclipse中add deployment时无法被识别出来的原因
  7. Mysql中用SQL增加、删除字段,修改字段名、字段类型、注释,调整字段顺序总结...
  8. AD16原理图.schdot中批量修改标签中的文本字体、大小、颜色
  9. Unity3D的LightProbe动态光探头用法介绍
  10. 机器学习实战(八)分类回归树CART(Classification And Regression Tree)
  11. 批量画同心不同半径圆lisp_【微课视频】青岛版数学六年级上册5.1圆的认识
  12. linux vnc 安装目录,Linux环境VNC服务安装、配置与使用(图)
  13. 知识点收录01---关于Tomcat的一些知识点
  14. 【答题助手】只用2秒!搞定百万英雄 芝士超人 冲顶大会
  15. Thingworx配置mysql的jdbc
  16. 常用数字电路模块:边沿检测电路
  17. 好用的待办事项APP有哪些
  18. 实现一个canvas画板
  19. iOS 应用商店评分StoreReview
  20. 从WebService到面向服务架构SOA理解【二】

热门文章

  1. c++ 获取64位进程模块地址_针对银行木马BokBot核心模块的深入分析
  2. 数据挖掘之人工神经网络BP算法
  3. android 自定义 listView
  4. 【CSS3】background-origin和background-clip的区别
  5. 数据可视化工具zeppelin安装
  6. 更好地认知Azure
  7. C++ Builder技巧集锦
  8. ListView分页
  9. Linux下同步工具inotify+rsync使用详解
  10. 背景建模与前景检测2(Background Generation And Foreground Detection Phase 2)