Index index = ElasticLunr.createIndex(); index.addField("title"); index.addField("body"); index.setRef("id");Note that if you don't set the document ref, the library will use "id" as default. For example, the following code is equivalent to the one above:
Index index = ElasticLunr.createIndex(); index.addField("title"); index.addField("body");It is possible to choose not to store the documents themselves in the index to save space in the index iself. For example:
Index index = ElasticLunr.createIndex(); index.addField("title"); index.addField("body"); index.setRef("id"); index.saveDocument(false);
Index index = ElasticLunr.createIndex(true); index.addField("title"); index.addField("body"); index.setRef("id");
Index index = ElasticLunr.createIndex("fr"); index.addField("title"); index.addField("body");
Index index = ElasticLunr.createIndex("en", PipelineFactory.METAPHONE_ENABLED);Note that you need to include a Metaphoning processor to non-English languages if you want to search without having to take care about accentuation. For example, with the following document:
Index index = ElasticLunr.createIndex("fr"); JSONObject doc1 = new JSONObject(); doc1.put("id", 1); doc1.put("title", "Louis XVI"); doc1.put("body", "Louis XVI a été exécuté en 1792."); index.addDoc(doc1);The following search will return nothing:
List<JSONObject> results = index.search("execute");While by adding Metaphoning:
Index index = ElasticLunr.createIndex("fr", PipelineFactory.METAPHONE_ENABLED); JSONObject doc1 = new JSONObject(); doc1.put("id", 1); doc1.put("title", "Louis XVI"); doc1.put("body", "Louis XVI a été exécuté en 1792."); index.addDoc(doc1);The same search will return our document.
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);Adding a document in the index will call the Pipeline for each field content in the document.
removeDocByRef
and pass the document reference.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."); index.addDoc(doc1); index.removeDocByRef(1);
updateDoc
and pass the new document associated JSON object.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."); index.addDoc(doc1); JSONObject doc2 = new JSONObject(); doc2.put("id", 1); doc2.put("title", "Oracle released its latest database"); doc2.put("body", "Changed text for the document"); index.updateDoc(doc2);
Index index = ElasticLunr.createIndex(); ... // add documents to the index ... FileUtils.toFile(<the file>, index.toJSON());And to load in existing serialized JSON index by:
JSONObject jsonIndex = FileUtils.toJSON(<the file>); Index index = Index.load(jsonIndex);As for the compressed index creation, you can load an index in compressed format by:
JSONObject jsonIndex = FileUtils.toJSON(<the file>); Index index = Index.load(jsonIndex, true);
Copyright 2017 Wei Song. Copyright 2018 Herve Girod. All Rights Reserved. Documentation and source under the MIT licence