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

Return source as String + Cleanup #249

Merged
merged 3 commits into from
Nov 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions jest-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

<modelVersion>4.0.0</modelVersion>

<groupId>io.searchbox</groupId>
<artifactId>jest-common</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>Jest Common Jar</name>
<description>ElasticSearch Java REST client - shared library package</description>
<url>https://github.com/searchbox-io/Jest</url>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.searchbox.action;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import com.google.gson.Gson;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package io.searchbox.action;

import io.searchbox.params.Parameters;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

/**
* @author cihat keser
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

Expand Down
15 changes: 14 additions & 1 deletion jest-common/src/main/java/io/searchbox/client/JestResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ public void setJsonMap(Map<String, Object> resultMap) {
String json = gson.toJson(resultMap, Map.class);
setJsonObject(new JsonParser().parse(json).getAsJsonObject());
}

public String getSourceAsString() {
String[] keys = getKeys();
if(!isSucceeded || jsonObject == null || keys == null || keys.length == 0 || !jsonObject.has(keys[0])) {
return null;
}

JsonElement obj = jsonObject.get(keys[0]);
for (int i = 1; i < keys.length; i++) {
obj = ((JsonObject) obj).get(keys[i]);
}
return obj.toString();
}

public <T> T getSourceAsObject(Class<T> clazz) {
T sourceAsObject = null;
Expand Down Expand Up @@ -271,7 +284,7 @@ private <T> T getAs(JsonElement id, Class<T> fieldType) throws IllegalAccessExce
}

protected String[] getKeys() {
return pathToResult == null ? null : (pathToResult + "").split("/");
return pathToResult == null ? null : pathToResult.split("/");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public boolean equals(Object obj) {
return false;
}

UpdateSettings rhs = (UpdateSettings) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.isEquals();
Expand Down
1 change: 0 additions & 1 deletion jest-common/src/main/java/io/searchbox/core/Bulk.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import io.searchbox.action.AbstractAction;
import io.searchbox.action.BulkableAction;
import io.searchbox.action.GenericResultAbstractAction;
import io.searchbox.client.JestResult;
import io.searchbox.params.Parameters;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public boolean equals(Object obj) {
return false;
}

DeleteByQuery rhs = (DeleteByQuery) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.isEquals();
Expand Down
1 change: 0 additions & 1 deletion jest-common/src/main/java/io/searchbox/core/Explain.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public boolean equals(Object obj) {
return false;
}

Explain rhs = (Explain) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.isEquals();
Expand Down
1 change: 0 additions & 1 deletion jest-common/src/main/java/io/searchbox/core/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public boolean equals(Object obj) {
return false;
}

Index rhs = (Index) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.isEquals();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public boolean equals(Object obj) {
return false;
}

MoreLikeThis rhs = (MoreLikeThis) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.isEquals();
Expand Down
1 change: 0 additions & 1 deletion jest-common/src/main/java/io/searchbox/core/MultiGet.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public boolean equals(Object obj) {
return false;
}

MultiGet rhs = (MultiGet) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.isEquals();
Expand Down
1 change: 0 additions & 1 deletion jest-common/src/main/java/io/searchbox/core/Percolate.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public boolean equals(Object obj) {
return false;
}

Percolate rhs = (Percolate) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.isEquals();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public boolean equals(Object obj) {
return false;
}

SearchScroll rhs = (SearchScroll) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.isEquals();
Expand Down
1 change: 0 additions & 1 deletion jest-common/src/main/java/io/searchbox/core/Suggest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public boolean equals(Object obj) {
return false;
}

Suggest rhs = (Suggest) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.isEquals();
Expand Down
1 change: 0 additions & 1 deletion jest-common/src/main/java/io/searchbox/core/Validate.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public boolean equals(Object obj) {
return false;
}

Validate rhs = (Validate) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.isEquals();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public boolean equals(Object obj) {
return false;
}

Analyze rhs = (Analyze) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.isEquals();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public boolean equals(Object obj) {
return false;
}

PutMapping rhs = (PutMapping) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.isEquals();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.searchbox.action.AbstractAction;
import io.searchbox.action.GenericResultAbstractAction;

import java.io.*;
import java.nio.charset.Charset;

import static io.searchbox.indices.script.ScriptLanguage.GROOVY;
import static java.net.URLEncoder.encode;

public class CreateIndexedScript extends AbstractIndexedScript {

protected CreateIndexedScript(Builder builder) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package io.searchbox.indices.script;

import io.searchbox.action.AbstractAction;
import io.searchbox.action.GenericResultAbstractAction;

import java.io.UnsupportedEncodingException;

import static io.searchbox.indices.script.ScriptLanguage.GROOVY;
import static java.net.URLEncoder.encode;

public class DeleteIndexedScript extends AbstractIndexedScript {

protected DeleteIndexedScript(Builder builder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public boolean equals(Object obj) {
return false;
}

UpdateSettings rhs = (UpdateSettings) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.isEquals();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public boolean equals(Object obj) {
return false;
}

PutTemplate rhs = (PutTemplate) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.isEquals();
Expand Down
74 changes: 70 additions & 4 deletions jest-common/src/test/java/io/searchbox/client/JestResultTest.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package io.searchbox.client;

import static org.junit.Assert.*;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import io.searchbox.annotations.JestId;

import org.junit.Test;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static junit.framework.Assert.*;

/**
* @author Dogukan Sonmez
*/
Expand Down Expand Up @@ -125,6 +127,70 @@ public void getGetSourceAsObject() {
assertEquals("trying out Elastic Search", twitter.getMessage());
assertEquals("2009-11-15T14:12:12", twitter.getPostDate());
}

@Test
public void getGetSourceAsString() {
String response = "{\n" +
" \"_index\" : \"twitter\",\n" +
" \"_type\" : \"tweet\",\n" +
" \"_id\" : \"1\", \n" +
" \"_source\" : {\n" +
" \"user\" : \"kimchy\",\n" +
" \"postDate\" : \"2009-11-15T14:12:12\",\n" +
" \"message\" : \"trying out Elastic Search\"\n" +
" }\n" +
"}\n";

result.setJsonMap(new Gson().fromJson(response, Map.class));
result.setPathToResult("_source");
result.setSucceeded(true);

String onlySource = "{" +
"\"user\":\"kimchy\"," +
"\"postDate\":\"2009-11-15T14:12:12\"," +
"\"message\":\"trying out Elastic Search\"" +
"}";
assertEquals(onlySource, result.getSourceAsString());
}

@Test
public void getGetSourceAsStringArray() {
String response = "{\n" +
" \"_index\" : \"twitter\",\n" +
" \"_type\" : \"tweet\",\n" +
" \"_id\" : \"1\", \n" +
" \"_source\" : [" +
" { \"user\" : \"kimch\" }, " +
" { \"user\" : \"bello\" }," +
" { \"user\" : \"ionex\" }" +
" ]\n" +
"}\n";

result.setJsonMap(new Gson().fromJson(response, Map.class));
result.setPathToResult("_source");
result.setSucceeded(true);

String onlySource = "[" +
"{\"user\":\"kimch\"}," +
"{\"user\":\"bello\"}," +
"{\"user\":\"ionex\"}" +
"]";
assertEquals(onlySource, result.getSourceAsString());
}

@Test
public void getGetSourceAsStringNoResult() {
String response = "{\n" +
" \"_index\" : \"twitter\",\n" +
" \"_type\" : \"tweet\",\n" +
" \"_id\" : \"1\" \n" +
"}\n";

result.setJsonMap(new Gson().fromJson(response, Map.class));
result.setPathToResult("_source");
result.setSucceeded(true);
assertNull(result.getSourceAsString());
}

@Test
public void getUnFoundGetResultAsAnObject() {
Expand Down Expand Up @@ -418,7 +484,7 @@ public void extractCountResult() {
result.setJsonMap(new Gson().fromJson(response, Map.class));
result.setPathToResult("count");
Double actual = result.extractSource().get(0).getAsDouble();
assertEquals(1.0, actual);
assertEquals(1.0, actual, 0.01);
}

@Test
Expand All @@ -435,7 +501,7 @@ public void getCountSourceAsObject() {
result.setPathToResult("count");
result.setSucceeded(true);
Double count = result.getSourceAsObject(Double.class);
assertEquals(1.0, count);
assertEquals(1.0, count, 0.01);
}

@Test
Expand Down
2 changes: 0 additions & 2 deletions jest-droid/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

<modelVersion>4.0.0</modelVersion>

<groupId>io.searchbox</groupId>
<artifactId>jest-droid</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>Jest Android Jar</name>
<description>ElasticSearch Java REST client - Android library package</description>
<url>https://github.com/searchbox-io/Jest</url>
Expand Down
2 changes: 0 additions & 2 deletions jest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

<modelVersion>4.0.0</modelVersion>

<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>Jest Apache HC Jar</name>
<description>ElasticSearch Java REST client - Apache HTTP Client package</description>
<url>https://github.com/searchbox-io/Jest</url>
Expand Down
Loading