Skip to content
Snippets Groups Projects
Commit 0eb22d13 authored by Jason Tedor's avatar Jason Tedor
Browse files

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.
parent db8674a3
No related branches found
No related tags found
No related merge requests found
/** /*
* Copyright (c) 2017 YCSB contributors. All rights reserved. * Copyright (c) 2017 YCSB contributors. All rights reserved.
* <p> * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you * Licensed under the Apache License, Version 2.0 (the "License"); you
...@@ -174,7 +174,7 @@ public class ElasticsearchClient extends DB { ...@@ -174,7 +174,7 @@ public class ElasticsearchClient extends DB {
@Override @Override
public Status delete(final String table, final String key) { public Status delete(final String table, final String key) {
try { try {
final SearchResponse searchResponse = search(key); final SearchResponse searchResponse = search(table, key);
if (searchResponse.getHits().totalHits == 0) { if (searchResponse.getHits().totalHits == 0) {
return Status.NOT_FOUND; return Status.NOT_FOUND;
} }
...@@ -200,19 +200,22 @@ public class ElasticsearchClient extends DB { ...@@ -200,19 +200,22 @@ public class ElasticsearchClient extends DB {
final Set<String> fields, final Set<String> fields,
final Map<String, ByteIterator> result) { final Map<String, ByteIterator> result) {
try { try {
final SearchResponse searchResponse = search(key); final SearchResponse searchResponse = search(table, key);
if (searchResponse.getHits().totalHits == 0) { if (searchResponse.getHits().totalHits == 0) {
return Status.NOT_FOUND; return Status.NOT_FOUND;
} }
final SearchHit hit = searchResponse.getHits().getAt(0); final SearchHit hit = searchResponse.getHits().getAt(0);
if (fields != null) { if (fields != null) {
for (String field : fields) { for (final String field : fields) {
result.put(field, new StringByteIterator( result.put(field, new StringByteIterator(
(String) hit.getField(field).getValue())); (String) hit.getField(field).getValue()));
} }
} else { } else {
for (final Map.Entry<String, SearchHitField> e : hit.getFields().entrySet()) { 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())); result.put(e.getKey(), new StringByteIterator((String) e.getValue().getValue()));
} }
} }
...@@ -227,7 +230,7 @@ public class ElasticsearchClient extends DB { ...@@ -227,7 +230,7 @@ public class ElasticsearchClient extends DB {
@Override @Override
public Status update(final String table, final String key, final Map<String, ByteIterator> values) { public Status update(final String table, final String key, final Map<String, ByteIterator> values) {
try { try {
final SearchResponse response = search(key); final SearchResponse response = search(table, key);
if (response.getHits().totalHits == 0) { if (response.getHits().totalHits == 0) {
return Status.NOT_FOUND; return Status.NOT_FOUND;
} }
...@@ -237,7 +240,7 @@ public class ElasticsearchClient extends DB { ...@@ -237,7 +240,7 @@ public class ElasticsearchClient extends DB {
hit.getSource().put(entry.getKey(), entry.getValue()); 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; return Status.OK;
...@@ -268,6 +271,9 @@ public class ElasticsearchClient extends DB { ...@@ -268,6 +271,9 @@ public class ElasticsearchClient extends DB {
} else { } else {
entry = new HashMap<>(hit.getFields().size()); entry = new HashMap<>(hit.getFields().size());
for (final Map.Entry<String, SearchHitField> field : hit.getFields().entrySet()) { 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())); entry.put(field.getKey(), new StringByteIterator((String) field.getValue().getValue()));
} }
} }
...@@ -281,8 +287,8 @@ public class ElasticsearchClient extends DB { ...@@ -281,8 +287,8 @@ public class ElasticsearchClient extends DB {
} }
private SearchResponse search(final String key) { private SearchResponse search(final String table, final String key) {
return client.prepareSearch(indexKey).setQuery(new TermQueryBuilder("key", key)).get(); return client.prepareSearch(indexKey).setTypes(table).setQuery(new TermQueryBuilder("key", key)).get();
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment