Lucene is just a library and to leverage its power you need to use Java. Integrating Lucene directly with your application is a very complex task.
Elastic Search uses the indexing and searching capabilities of Lucene but hides the complexities behind a simple RESTful API.
In this post we will learn to perform basic CRUD operations using Elastic search transport client in Scala with sbt as our build-tool.
Let us start by downloading Elasticsearch from here and unzipping it.
Execute the following command to run Elastic search in foreground:
Test it out by opening another terminal window and running the following:
To start with the coding part, create a new sbt project and add the following dependency in the build.sbt file.
Next, we need to create a client that will talk to the elaticsearch server.
Once the client is created we can query the Elastic search server.
The following example inserts a JSON document into an index called library, under a type called books.
An index is like a database and a type is like a table in Elastic search.
Lets create our first json document.
To add a json into the Elasticsearch add the following code to your project:
The prepareIndex method takes 3 arguments:- index name,type,id. The id argument is optional. If you do not specify an id Elastic search will automatically generate an id for the document.
Note that the title of the book is Elastic and not Elastic search. Lets correct this by executing an update on the document:
Lets search for our document and see whether the document is updated or not
Execute the following code to search for a document:
The id that we specified while adding the document is stored as “_id”.
If you do not specify the setQuery method then Elastic search will get all the documents in the type books.
Finally to delete a document execute the following code:
Elasticsearch also provides bulk API used to insert multiple documents onto the Elastic search server in a single API call.
To use the bulk API create a file in the following format:
Now lets create a bulk request and add the following documents to the request:
Open an InputStream and read the json file you just created and store the data in a list named fileData.