Commit 982ea41f authored by Alexandra Rogova's avatar Alexandra Rogova

implemented elasticlunr, search is now much faster!

parent f90ac3f3
......@@ -5,6 +5,9 @@
<script src="../jio/external/rsvp-2.0.4.js"></script>
<script src="../jio/dist/jio-latest.js"></script>
<script src="../renderjs/dist/renderjs-latest.js"></script>
<script src="./elasticlunr/elasticlunr.js"></script>
<script src="./lunr-languages/lunr.stemmer.support.js"></script>
<script src="./lunr-languages/lunr.fr.js"></script>
<script src="gadget_model.js"></script>
</head>
<body>
......
......@@ -5,6 +5,14 @@
rJS(window)
.ready(function(){
this.index = elasticlunr(function () {
this.use(elasticlunr.fr);
this.addField('title');
this.addField('body');
this.addField('link');
this.setRef('id'); //id = x."/a/b" where x = substorage id, "/a/b" = item id in x
});
this.feeds = jIO.createJIO({
type : "indexeddb",
......@@ -22,21 +30,43 @@
})
.declareMethod ("add_attachment", function (title, rss){
var gadget = this;
var gadget = this, sub_id;
return this.feeds.putAttachment("feeds_doc", title, new Blob([rss], {type : "text/xml"}))
.push(function (){
var new_sub_storage = {
type : "my_parser",
document_id : "feeds_doc",
attachment_id : title,
parser : "rss",
sub_storage : {
type : "indexeddb",
database : "mynij-v1.2"
}
};
type : "my_parser",
document_id : "feeds_doc",
attachment_id : title,
parser : "rss",
sub_storage : {
type : "indexeddb",
database : "mynij-v1.2"
}
};
var tmp = jIO.createJIO(new_sub_storage);
gadget.sub_storages.push(tmp);
sub_id = gadget.sub_storages.length-1;
return gadget.sub_storages[sub_id].allDocs();
})
.push(function(all_new_items){
var i, promise_list = [];
for (i = 0; i<all_new_items.data.rows.length; i+=1){
promise_list.push(gadget.sub_storages[sub_id].get(all_new_items.data.rows[i].id));
}
return RSVP.all(promise_list);
})
.push(function(items){
var i, id, doc;
for (i=1; i<items.length; i++){
id = sub_id+"./0/"+i;
doc = {
"id": id,
"title": items[i].title,
"body": items[i].description,
"link": items[i].link
};
gadget.index.addDoc(doc);
}
});
})
......@@ -54,7 +84,7 @@
})
.declareMethod("search", function() {
return this.search_storage.allDocs.apply(this.search_storage, arguments);
return this.index.search(arguments[0]);
})
.declareMethod("loaded_doc", function(){
......
......@@ -37,7 +37,7 @@
body.innerHTML = "";
list.appendChild(list_item);
} else {
return this.cut_description(item.description, key)
return this.cut_description(item.body, key)
.push(function (result){
if (result === null) body.innerHTML = "";
else body.innerHTML = result.slice(1);
......
......@@ -6,6 +6,9 @@
<script src="../jio/dist/jio-latest.js"></script>
<script src="jio.my_parser_storage.js"></script>
<script src="../renderjs/dist/renderjs-latest.js"></script>
<script src="./elasticlunr/elasticlunr.js"></script>
<script src="./lunr-languages/lunr.stemmer.support.js"></script>
<script src="./lunr-languages/lunr.fr.js"></script>
<script src="search.js"></script>
<link rel="stylesheet" type="text/css" href="mynij.css">
</head>
......
......@@ -69,30 +69,21 @@
var gadget = this;
return gadget.state.result_gadget.clear()
.push(function(){
return gadget.state.model_gadget.allDocs();
return gadget.state.model_gadget.search(key);
})
.push(function (all_attachments){
var i, j, promise_list = [];
for (i=0; i<all_attachments.length; i+=1){
for (j=1; j<all_attachments[i].data.rows.length; j+=1){
promise_list.push(gadget.state.model_gadget.get(i, all_attachments[i].data.rows[j].id));
.push(function(result){
if (result.length === 0) {
return gadget.state.result_gadget.addItem({
title : "No results found",
link : ""
}, "");
} else {
var i, promise_list = [];
for (i=0; i<result.length; i+=1){
promise_list.push(gadget.state.result_gadget.addItem(result[i].doc, key));
}
return RSVP.all(promise_list);
}
return RSVP.all(promise_list);
}).push(function(all_items){
var i,
regEx = new RegExp('((.* '+key+' .*)|('+key+' .*))', 'gi'),
promise_list = [];
for (i=0; i<all_items.length; i+=1){
if (regEx.test(all_items[i].title) || regEx.test(all_items[i].description)) promise_list.push(gadget.state.result_gadget.addItem(all_items[i], key));
}
if (promise_list.length === 0) {
return gadget.state.result_gadget.addItem({
title : "No results found",
link : ""
}, "");
}
else return RSVP.all(promise_list);
});
})
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment