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);
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);
List<JSONObject> results = index.search("Oracle database profit");The result array is sorted in descencing order, sorted by the score. Each result is a
JSONObject
with the following fields: List<JSONObject> results = index.search("Oracle database profit"); JSONObject result = results.get(0); float score = result.getFloat("score"); String ref = result.getString("ref");
ref
will be equal to "1", so will refer to the document with the id "1".Copyright 2017 Wei Song. Copyright 2018 Herve Girod. All Rights Reserved. Documentation and source under the MIT licence