Home
Categories
Dictionnary
Download
Project Details
Changes Log
FAQ
License

Format compatibility


    1  Format version
    2  Compressed index format
    3  Example
       3.1  Creation of an index in the Java code
       3.2  Usage in Javascript
          3.2.1  Loading the serialized JSON data
          3.2.2  Loading the serialized script
    4  Notes
    5  See Also

The format of the serialized JSON index is compatible with the elasticlunr Javascript library. For example you can save the index as JSON in Java and load the same index in Javascript.

Format version

When loading an existing JSON index, the elasticlunr Javascript library checks the version of the index. For example:
      {"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>);

Compressed index format

Main Article: Compressed format

The compressed format is not compatible with the regular elasticlunr Javascript library. However if you only use the library in Java you will have no problem. However a modified version of the Javascript library, compatible with this compressed format (but also the regular uncompressed format) is provided in this project, under the js/compressed folder. The associated elasticlunr fork is available at hervegirod/elasticlunr.js.

Example

Creation of an index in the Java code

Consider for example the creation of the following index:
      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:

Usage in Javascript

Loading the serialized 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.

Loading the serialized script

We can load this index in the elasticlunr Javascript library by:
      <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>

Notes

  1. ^ In that case the script will look as:
                   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.

See Also


Categories: creation | format

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