{"version":"0.9.5","fields":["title","body"],"ref":"id","documentStore":{"docs"...By default the Java library uses the 0.9.5 version, however it is possible to change the version used for loading and serializing a JSON index by:
ElasticLunr.setVersion(<version String>);
js/compressed
folder. The associated elasticlunr fork is available at hervegirod/elasticlunr.js.
Index index = ElasticLunr.createIndex(); index.addField("title"); index.addField("body"); index.setRef("id"); JSONObject doc1 = new JSONObject(); doc1.put("id", 1); doc1.put("title", "Oracle released its latest database Oracle 12g"); doc1.put("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."); JSONObject doc2 = new JSONObject(); doc2.put("id", 2); doc2.put("title", "Oracle released its profit report of 2015"); doc2.put("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);We can serialize the index:
FileUtils.toFile("theIndex.json", index.toJSON());
FileUtils.toFile("theIndex.js", index.toJSON(), "indexDump");
. This can be useful because Chrome refuse to load JSON data locally[1]
var indexDump = {"version":"0.9.5","fields":["title","body"],"ref":"id","documentStore":{"docs"...and the
indexDump
variable will be filled with the content of the JSON data.<HEAD> <script type="text/javascript" src="elasticlunr.js"></script> <script type="text/javascript" src="jQuery.js"></script> <script> function onLoad() { document.getElementById("dwn-btn").addEventListener("click", function() { $.getJSON("theIndex.json", function(json){ var index = elasticlunr.Index.load(json); var result = index.search("Oracle database"); alert(result[0].score + " =>" + result[0].doc.body); }); }, false); } </script> </HEAD> <BODY onload="onLoad()"> Click here to show the first result from the JSON file <input type="button" id="dwn-btn" value="Show result"/> </BODY>Note that this will not work on Chrome for local files, because by default Chjrome does not a allow to load data from the local filesystem. To load the index, you will need to use the script method.
<HEAD> <script type="text/javascript" src="elasticlunr.js"></script> <script type="text/javascript" src="theIndex.js"></script> <script> function onLoad() { document.getElementById("dwn-btn").addEventListener("click", function(){ var index = elasticlunr.Index.load(indexDump); var result = index.search("Oracle database"); alert(result[0].score + " =>" + result[0].doc.body); }, false); } </script> </HEAD> <BODY onload="onLoad()"> Click here to show the first result from the Javascript script <input type="button" id="dwn-btn" value="Show result"/> </BODY>
var indexDump = {"version":"0.9.5","fields":["title","body"],"ref":"id","documentStore":{"docs"...and the
indexDump
variable will be filled with the content of the JSON data.Copyright 2017 Wei Song. Copyright 2018 Herve Girod. All Rights Reserved. Documentation and source under the MIT licence