Home
Categories
Dictionnary
Download
Project Details
Changes Log
FAQ
License

Usage


    1  Creation of an index
       1.1  Creation and Fields definition
       1.2  Adding documents to the index
    2  Searching
    3  See Also

The usage of the library is very similar to the usage of the elasticlunr Javascript library. You can go to the elasticlunr documentation for more information.

Creation of an index

Main Article: Index creation

Creation and Fields definition

A very simple search index can be created using the following code:
      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);

Adding documents to the index

Adding documents to the index is as simple as:
      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);

Searching

Main Article: Searching

Searching is simple:

      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: For example, for the index above:

      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".

See Also


Categories: creation | general | search

Copyright 2017 Wei Song. Copyright 2018 Herve Girod. All Rights Reserved. Documentation and source under the MIT licence