Home
Categories
Dictionnary
Download
Project Details
Changes Log
FAQ
License

Index creation


    1  Index creation and Fields definition
       1.1  Compressed index creation
       1.2  Specifying the index language
       1.3  Adding a Metaphone function
    2  Adding documents to the index
    3  Removing documents from the index
    4  Updating documents in the index
    5  Saving and loading the index
       5.1  Compatibility with the Javascript library
    6  See Also

Index creation and Fields definition

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

Compressed index creation


You can create an index using the compressed format by:

      Index index = ElasticLunr.createIndex(true);
      index.addField("title");
      index.addField("body");
      index.setRef("id");

Specifying the index language

By default, the index is created with the English language. However, it is possible to create the index by specifying which language you want to use for the pipeline. For example, here the Pipeline will use the French language:

      Index index = ElasticLunr.createIndex("fr");
      index.addField("title");
      index.addField("body");

Adding a Metaphone function

It is possible to add a Metaphoning processor to the index Pipeline:

      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.

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);

Adding a document in the index will call the Pipeline for each field content in the document.

Removing documents from the index

To remove a document from the index, just call removeDocByRef and pass the document reference.

For example:

      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);

Updating documents in the index

To update a document in the index, just call updateDoc and pass the new document associated JSON object.

For example:

      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);

Saving and loading the index

It is possible to serialize the index as a JSON file by:
      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);

Compatibility with the Javascript library

Main Article: Format compatibility

The format of the serialized JSON index is compatible with the elasticlunr Javascript library. For example you could save the index as JSON in Java and load the same index in Javascript. However, if you are saving the index as compressed, you will not be able to use the serialized index with the regular elasticlunr Javascript library. However if you only use the library in Java you will have no problem.

See Also


Categories: creation

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