Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
ES5 support for _update_by_query.
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbode committed Aug 25, 2017
1 parent 6a297a1 commit c19aed8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 40 deletions.
13 changes: 4 additions & 9 deletions jest-common/src/main/java/io/searchbox/core/UpdateByQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import com.google.gson.Gson;
import io.searchbox.action.AbstractAction;
import io.searchbox.action.AbstractMultiTypeActionBuilder;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import java.util.Objects;

/**
* @author Lior Knaany
Expand All @@ -25,7 +24,7 @@ protected String buildURI() {

@Override
public String getPathToResult() {
return null;
return "ok";
}

@Override
Expand All @@ -35,9 +34,7 @@ public String getRestMethodName() {

@Override
public int hashCode() {
return new HashCodeBuilder()
.appendSuper(super.hashCode())
.toHashCode();
return Objects.hash(super.hashCode());
}

@Override
Expand All @@ -52,9 +49,7 @@ public boolean equals(Object obj) {
return false;
}

return new EqualsBuilder()
.appendSuper(super.equals(obj))
.isEquals();
return super.equals(obj);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public UpdateByQueryResult(Gson gson) {
}

public boolean didTimeOut() {
return (jsonObject != null && jsonObject.has("timedOut")) ? jsonObject.get("timedOut").getAsBoolean() : false;
return (jsonObject != null && jsonObject.has("timed_out")) ? jsonObject.get("timed_out").getAsBoolean() : false;
}

public long getConflictsCount() {
Expand All @@ -34,7 +34,15 @@ public int getBatchCount() {
}

public int getRetryCount() {
return (jsonObject != null && jsonObject.has("retries")) ? jsonObject.get("retries").getAsInt() : 0;
return getBulkRetryCount() + getSearchRetryCount();
}

public int getBulkRetryCount() {
return (jsonObject != null && jsonObject.has("retries") && jsonObject.getAsJsonObject("retries").has("bulk")) ? jsonObject.getAsJsonObject("retries").get("bulk").getAsInt() : 0;
}

public int getSearchRetryCount() {
return (jsonObject != null && jsonObject.has("retries") && jsonObject.getAsJsonObject("retries").has("search")) ? jsonObject.getAsJsonObject("retries").get("search").getAsInt() : 0;
}

public int getNoopCount() {
Expand All @@ -47,12 +55,14 @@ public JsonArray getFailures() {

@Override
public String toString() {
return "Updated: " + getUpdatedCount()
+ ", conflicts: " + getConflictsCount()
+ ", time taken: " + getMillisTaken()
+ ", did time out: " + didTimeOut()
+ ", batches: " + getBatchCount()
+ ", retries: " + getRetryCount()
+ ", noops: " + getNoopCount();
return "Updated: " + getUpdatedCount()
+ ", conflicts: " + getConflictsCount()
+ ", time taken: " + getMillisTaken()
+ ", did time out: " + didTimeOut()
+ ", batches: " + getBatchCount()
+ ", retries: " + getRetryCount()
+ ", bulk retries: " + getBulkRetryCount()
+ ", search retries: " + getSearchRetryCount()
+ ", noops: " + getNoopCount();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void multipleSearchRequests() throws IOException {
SearchResult complexSearchResult = complexSearchResponse.searchResult;
assertTrue(complexSearchResult.isSucceeded());
assertNull(complexSearchResult.getErrorMessage());
assertEquals(Integer.valueOf(2), complexSearchResult.getTotal());
assertEquals(Long.valueOf(2), complexSearchResult.getTotal());
List<SearchResult.Hit<Comment, Void>> complexSearchHits = complexSearchResult.getHits(Comment.class);
assertEquals(2, complexSearchHits.size());

Expand All @@ -52,7 +52,7 @@ public void multipleSearchRequests() throws IOException {
SearchResult simpleSearchResult = simpleSearchResponse.searchResult;
assertTrue(simpleSearchResult.isSucceeded());
assertNull(simpleSearchResult.getErrorMessage());
assertEquals(Integer.valueOf(3), simpleSearchResult.getTotal());
assertEquals(Long.valueOf(3), simpleSearchResult.getTotal());
List<SearchResult.Hit<Comment, Void>> simpleSearchHits = simpleSearchResult.getHits(Comment.class);
assertEquals(3, simpleSearchHits.size());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package io.searchbox.core;

import com.google.gson.GsonBuilder;
import io.searchbox.client.JestResult;

import io.searchbox.common.AbstractIntegrationTest;
import org.elasticsearch.action.index.IndexRequest;
import io.searchbox.core.UpdateByQueryResult;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.ReindexPlugin;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.script.groovy.GroovyPlugin;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.test.ESIntegTestCase;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
Expand All @@ -30,33 +32,35 @@ public class UpdateByQueryIntegrationTest extends AbstractIntegrationTest {

@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
// ReindexPlugin.class is needed in order to make the _update_by_query REST action available in the embedded Elasticsearch test server
// GroovyPlugin is needed for running the update script
return pluginList(GroovyPlugin.class, ReindexPlugin.class);
final ArrayList<Class<? extends Plugin>> plugins = new ArrayList<>(super.nodePlugins());
plugins.add(ReindexPlugin.class);
return plugins;
}

@Override
protected Settings nodeSettings(int nodeOrdinal) {
return Settings.settingsBuilder()
final Settings.Builder builder = Settings.builder().put(super.nodeSettings(nodeOrdinal));
return builder
.put(super.nodeSettings(nodeOrdinal))
.put("script.inline", "on")
.put("script.indexed", "on")
.put("plugin.types", ReindexPlugin.class.getName())
.put("script.inline", "true")
.build();
}

@Test
public void update() throws IOException, InterruptedException {

// create a tweet
assertTrue(client().index(new IndexRequest(INDEX, TYPE).source("{\"user\":\"lior\",\"num\":1}").refresh(true)).actionGet().isCreated());
assertTrue(client().index(new IndexRequest(INDEX, TYPE).source("{\"user\":\"kimchy\",\"num\":2}").refresh(true)).actionGet().isCreated());
assertTrue(index(INDEX, TYPE, "1", "{\"user\":\"lior\",\"num\":1}").getResult().equals(DocWriteResponse.Result.CREATED));
assertTrue(index(INDEX, TYPE, "2", "{\"user\":\"kimchy\",\"num\":2}").getResult().equals(DocWriteResponse.Result.CREATED));

refresh();
ensureSearchable(INDEX);

// run the search and update
final BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.termQuery("user", "lior"));
final String script = "ctx._source.user = ctx._source.user + '_updated';";

final String payload = jsonBuilder()
final String payload = jsonBuilder()
.startObject()
.field("query", queryBuilder)
.startObject("script")
Expand All @@ -69,17 +73,20 @@ public void update() throws IOException, InterruptedException {
.addType(TYPE)
.build();

System.out.println("uri: " + updateByQuery.getURI());
System.out.println("+query: " + updateByQuery.getData(new GsonBuilder().setPrettyPrinting().create()));

JestResult result = client.execute(updateByQuery);
UpdateByQueryResult result = client.execute(updateByQuery);

// Checks
assertTrue(result.getErrorMessage(), result.isSucceeded());
System.out.println("+jsonobj: " + result.getJsonObject());

assertEquals(0, result.getJsonObject().get("failures").getAsJsonArray().size());
assertEquals(1, result.getJsonObject().get("updated").getAsInt());
assertFalse(result.didTimeOut());
assertEquals(0, result.getConflictsCount());
assertTrue(result.getMillisTaken() > 0);
assertEquals(1, result.getUpdatedCount());
assertEquals(0, result.getRetryCount());
assertEquals(0, result.getBulkRetryCount());
assertEquals(0, result.getSearchRetryCount());
assertEquals(0, result.getNoopCount());
assertEquals(0, result.getFailures().size());
}

}

0 comments on commit c19aed8

Please sign in to comment.