CRUD operations using Java

In this section, we will go through the Elasticsearch Java client to perform the CRUD operations. To use a Java client of Elasticsearch, you can either build a Maven project (recommended) or simply add Elasticsearch jar files, which ship with the Elasticsearch installation file, into your project classpath.

You can include an Elasticsearch dependency in your Maven project by adding the following code to the project's pom.xml file:

  <dependency>
      <groupId>org.elasticsearch</groupId>
      <artifactId>elasticsearch</artifactId>
      <version>2.0.0</version>
    </dependency>

Connecting with Elasticsearch

To connect with Elasticsearch using its transport client, you need to add the following imports:

import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

After this, a connection can be created with the following code snippet:

static Client client;
static Settings settings;
public static Client getEsConnection()
  {
  settings = Settings.settingsBuilder().put("cluster.name", "elasticsearch").put("path.home", "/").put("client.transport.ping_timeout","10s").build();
  try {
    client = TransportClient.builder().settings(settings)
          .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
  } catch (UnknownHostException e) {
    e.printStackTrace();
  }
  return client;
  }

To connect with more than one node of a single cluster, you can add more transport addresses in this way:

 client = TransportClient.builder().settings(settings).build()
          .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("some_other_host"), 9300));

Note that to create a connection with Elasticsearch using Java API, you need to first create settings by specifying the cluster name and can optionally provide a timeout that defaults to 5s.

This setting is then used by the transport client to create a connection with the Elasticsearch cluster over the TCP port 9300.

Indexing a document

To index a single document at once (sequential indexing), you can create documents in multiple ways, such as using plain JSON strings, or using Jackson API or your familiar HashMap. The following example shows the use of HashMap to create a document:

  1. The first import will be as follows:
    import org.elasticsearch.action.index.IndexResponse;
  2. Then create the document:
    Map<String, Object> document1= new HashMap<String, Object>();
        document1.put("screen_name", "d_bharvi");
        document1.put("followers_count", 2000);
        document1.put("create_at", "2015-09-20");
  3. The preceding document can be indexed with the following code, assuming you have an object of the client available in your code:
    IndexResponse response = client.prepareIndex()
        .setIndex("IndexName").setType("docType")
        .setId("1").setSource(document1)
        .execute().actionGet();

    In the preceding code, the setIndex and setType methods take the index name and the name of the document type correspondingly.

    • The setSource method takes the actual data for indexing.
    • The setId method takes the unique document identifier. This is optional; Elasticsearch will generate it dynamically if it is not set.

There are many other methods available, which will see in the upcoming chapter.

Fetching a document

To fetch a document from Elasticsearch, you need its document ID. Once you know the document ID, it is simple to fetch it. Just add the following import:

import org.elasticsearch.action.get.GetResponse;

Then, you can get the document using prepareGet:

GetResponse response = client.prepareGet()
    .setIndex(indexName).setType(docType)
    .setId("1").execute().actionGet();

Updating a document

As you are aware, documents can be updated in two ways; first using doc, and the other way is to use script. In both cases, you need to import UpdateResponse to you, code:

import org.elasticsearch.action.delete.UpdateResponse;

Updating a document using doc

To do a partial update, you can create the object to be appended or replace an existing value for a field:

Map<String, Object> partialDoc1= new HashMap<String, Object>();
partialDoc1.put("user_name", "Bharvi Dixit");

Then, you can send it to Elasticsearch using the prepareUpdate method by setting the partial document inside the setDoc method:

UpdateResponse response = client.prepareUpdate()
  .setIndex(indexName).setType(docType)
  .setId("1").setDoc(partialDoc1)
  .execute().actionGet();

Updating a document using script

To use scripts for updating, first you need to make sure that you have enabled dynamic scripting in your elasticsearch.yml file. Then, you need to import the following classes into your code:

import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptService.ScriptType;

Once the import is done, you can do the update in the following way:

String script = "ctx._source.user_name = "Alberto Paro"";
UpdateResponse response = client.prepareUpdate()
  .setIndex(indexName).setType(docType)
  .setScript(new Script(script, ScriptType.INLINE, "groovy", null)).setId("1").execute().actionGet();

Note that in this example, the INLINE scripts have been used. You can also use file scripts or indexed scripts . You can find more about scripting here: https://www.elastic.co/guide/en/elasticsearch/reference/2.0/modules-scripting.html.

Deleting documents

To delete a single document in a single request, import the following line of code:

import org.elasticsearch.action.delete.DeleteResponse;

You can delete the document with the prepareDelete method using the document ID:

DeleteResponse response = client.prepareDelete()
      .setIndex(indexName).setType(docType)
      .setId("1").execute().actionGet();
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset