JavaScript 搜索引擎 lunr.js
lunr.js 实现了在网页上的搜索引擎,类似 Solr。
示例代码:
01 |
//定义索引 |
02 |
var idx = lunr( function () { |
03 |
this .field( 'title' , { boost: 10 }) |
04 |
this .field( 'body' ) |
05 |
}) |
06 |
07 |
//添加索引 |
08 |
var doc = { |
09 |
"title" : "Twelfth-Night" , |
10 |
"body" : "If music be the food of love, play on: Give me excess of it…" , |
11 |
"author" : "William Shakespeare" , |
12 |
"id" : 1 |
13 |
} |
14 |
idx.add(doc) |
15 |
16 |
//搜索 |
17 |
idx.search( "love" ) |
18 |
19 |
//返回结果 |
20 |
[{ |
21 |
"ref" : 1, |
22 |
"score" : 0.87533 |
23 |
}] |