From 0eb22d138e78471f857726e28aa0bfeeab285057 Mon Sep 17 00:00:00 2001
From: Jason Tedor <jason@tedor.me>
Date: Tue, 8 Aug 2017 18:42:47 +0900
Subject: [PATCH] Elasticsearch 5: Fix unreleased bugs in client

This commit fixes a bug in the Elasticsearch 5 transport client
binding. Namely, the update method was not indexing using the ID of the
read document, but instead the provided key. As auto-generated IDs are
used, this is a mistake as this is not the ID of the document. This
commit fixes this issue.

Additionally, the type is passed to the search method and used in
searches. While this is not a correctness issue per se, we fix it for
clarity.
---
 .../elasticsearch5/ElasticsearchClient.java   | 22 ++++++++++++-------
 1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/elasticsearch5/src/main/java/com/yahoo/ycsb/db/elasticsearch5/ElasticsearchClient.java b/elasticsearch5/src/main/java/com/yahoo/ycsb/db/elasticsearch5/ElasticsearchClient.java
index eb305c8d..a34dc0a2 100644
--- a/elasticsearch5/src/main/java/com/yahoo/ycsb/db/elasticsearch5/ElasticsearchClient.java
+++ b/elasticsearch5/src/main/java/com/yahoo/ycsb/db/elasticsearch5/ElasticsearchClient.java
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (c) 2017 YCSB contributors. All rights reserved.
  * <p>
  * Licensed under the Apache License, Version 2.0 (the "License"); you
@@ -174,7 +174,7 @@ public class ElasticsearchClient extends DB {
   @Override
   public Status delete(final String table, final String key) {
     try {
-      final SearchResponse searchResponse = search(key);
+      final SearchResponse searchResponse = search(table, key);
       if (searchResponse.getHits().totalHits == 0) {
         return Status.NOT_FOUND;
       }
@@ -200,19 +200,22 @@ public class ElasticsearchClient extends DB {
           final Set<String> fields,
           final Map<String, ByteIterator> result) {
     try {
-      final SearchResponse searchResponse = search(key);
+      final SearchResponse searchResponse = search(table, key);
       if (searchResponse.getHits().totalHits == 0) {
         return Status.NOT_FOUND;
       }
 
       final SearchHit hit = searchResponse.getHits().getAt(0);
       if (fields != null) {
-        for (String field : fields) {
+        for (final String field : fields) {
           result.put(field, new StringByteIterator(
                   (String) hit.getField(field).getValue()));
         }
       } else {
         for (final Map.Entry<String, SearchHitField> e : hit.getFields().entrySet()) {
+          if ("key".equals(e.getKey())) {
+            continue;
+          }
           result.put(e.getKey(), new StringByteIterator((String) e.getValue().getValue()));
         }
       }
@@ -227,7 +230,7 @@ public class ElasticsearchClient extends DB {
   @Override
   public Status update(final String table, final String key, final Map<String, ByteIterator> values) {
     try {
-      final SearchResponse response = search(key);
+      final SearchResponse response = search(table, key);
       if (response.getHits().totalHits == 0) {
         return Status.NOT_FOUND;
       }
@@ -237,7 +240,7 @@ public class ElasticsearchClient extends DB {
         hit.getSource().put(entry.getKey(), entry.getValue());
       }
 
-      client.prepareIndex(indexKey, table, key).setSource(hit.getSource()).get();
+      client.prepareIndex(indexKey, table, hit.getId()).setSource(hit.getSource()).get();
 
       return Status.OK;
 
@@ -268,6 +271,9 @@ public class ElasticsearchClient extends DB {
         } else {
           entry = new HashMap<>(hit.getFields().size());
           for (final Map.Entry<String, SearchHitField> field : hit.getFields().entrySet()) {
+            if ("key".equals(field.getKey())) {
+              continue;
+            }
             entry.put(field.getKey(), new StringByteIterator((String) field.getValue().getValue()));
           }
         }
@@ -281,8 +287,8 @@ public class ElasticsearchClient extends DB {
   }
 
 
-  private SearchResponse search(final String key) {
-    return client.prepareSearch(indexKey).setQuery(new TermQueryBuilder("key", key)).get();
+  private SearchResponse search(final String table, final String key) {
+    return client.prepareSearch(indexKey).setTypes(table).setQuery(new TermQueryBuilder("key", key)).get();
   }
 
 }
-- 
GitLab