forked from iweisi/Jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'logzio' of https://github.com/logzio/Jest into logzio
- Loading branch information
Showing
4 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
jest-common/src/main/java/io/searchbox/fields/FieldStats.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package io.searchbox.fields; | ||
|
||
import io.searchbox.action.AbstractAction; | ||
import io.searchbox.action.GenericResultAbstractAction; | ||
import io.searchbox.params.Parameters; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class FieldStats extends GenericResultAbstractAction { | ||
|
||
protected FieldStats(FieldStats.Builder builder) { | ||
super(builder); | ||
|
||
this.indexName = builder.index; | ||
|
||
Map<String, Object> fieldStatsBody = new HashMap<>(); | ||
fieldStatsBody.put("fields", builder.fields); | ||
|
||
this.payload = fieldStatsBody; | ||
|
||
setURI(buildURI()); | ||
} | ||
|
||
@Override | ||
public String getRestMethodName() { | ||
return "POST"; | ||
} | ||
|
||
@Override | ||
protected String buildURI() { | ||
String buildURI = super.buildURI(); | ||
if (buildURI.isEmpty()) | ||
return "_field_stats"; | ||
|
||
return buildURI + "/_field_stats"; | ||
} | ||
|
||
|
||
public static class Builder extends AbstractAction.Builder<FieldStats, FieldStats.Builder> { | ||
|
||
private String index; | ||
private Object fields; | ||
|
||
public Builder(Object fields) { | ||
this.fields = fields; | ||
} | ||
|
||
public FieldStats.Builder setIndex(String index) { | ||
this.index = index; | ||
return this; | ||
} | ||
|
||
public FieldStats.Builder setLevel(String level) { | ||
parameters.put(Parameters.LEVEL, level); | ||
return this; | ||
} | ||
|
||
@Override | ||
public FieldStats build() { | ||
return new FieldStats(this); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
jest-common/src/test/java/io/searchbox/fields/FieldsStatsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.searchbox.fields; | ||
|
||
import com.google.gson.Gson; | ||
import org.junit.Test; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class FieldsStatsTest { | ||
static final String TEST_FIELD = "test_name"; | ||
static final String INDEX = "twitter"; | ||
static final List FIELDS = Collections.singletonList(TEST_FIELD); | ||
|
||
@Test | ||
public void testBasicUriGeneration() { | ||
FieldStats fieldStats = new FieldStats.Builder(FIELDS).setIndex(INDEX).build(); | ||
assertEquals("POST", fieldStats.getRestMethodName()); | ||
assertEquals(INDEX + "/_field_stats", fieldStats.getURI()); | ||
assertEquals("{\"fields\":[\"" + TEST_FIELD + "\"]}", fieldStats.getData(new Gson())); | ||
} | ||
|
||
@Test | ||
public void testBasicUriGenerationNoIndex() { | ||
FieldStats fieldStats = new FieldStats.Builder(FIELDS).build(); | ||
assertEquals("POST", fieldStats.getRestMethodName()); | ||
assertEquals("_field_stats", fieldStats.getURI()); | ||
assertEquals("{\"fields\":[\"" + TEST_FIELD + "\"]}", fieldStats.getData(new Gson())); | ||
} | ||
|
||
@Test | ||
public void testBasicUriGenerationWithLevel() { | ||
FieldStats fieldStats = new FieldStats.Builder(FIELDS).setIndex(INDEX).setLevel("indices").build(); | ||
assertEquals("POST", fieldStats.getRestMethodName()); | ||
assertEquals(INDEX + "/_field_stats?level=indices", fieldStats.getURI()); | ||
assertEquals("{\"fields\":[\"" + TEST_FIELD + "\"]}", fieldStats.getData(new Gson())); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
jest/src/test/java/io/searchbox/fields/FieldStatsIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package io.searchbox.fields; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import io.searchbox.client.JestResult; | ||
import io.searchbox.common.AbstractIntegrationTest; | ||
import io.searchbox.core.DocumentResult; | ||
import io.searchbox.core.Index; | ||
import org.elasticsearch.test.ESIntegTestCase; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.SUITE, numDataNodes = 1) | ||
public class FieldStatsIntegrationTest extends AbstractIntegrationTest { | ||
|
||
static final String INDEX = "twitter"; | ||
static final String TYPE = "tweet"; | ||
static final String TEST_FIELD = "test_name"; | ||
static final List FIELDS = Collections.singletonList(TEST_FIELD); | ||
|
||
@Test | ||
public void testFieldStats() throws IOException { | ||
|
||
Map<String, String> source = ImmutableMap.of( | ||
TEST_FIELD, "testFieldStats"); | ||
|
||
DocumentResult documentResult = client.execute( | ||
new Index.Builder(source) | ||
.index(INDEX) | ||
.type(TYPE) | ||
.refresh(true) | ||
.build() | ||
); | ||
|
||
assertTrue(documentResult.getErrorMessage(), documentResult.isSucceeded()); | ||
|
||
FieldStats fieldStats = new FieldStats.Builder(FIELDS).setIndex(INDEX).build(); | ||
|
||
JestResult fieldStatsResult = client.execute(fieldStats); | ||
|
||
assertTrue(fieldStatsResult.getErrorMessage(), fieldStatsResult.isSucceeded()); | ||
} | ||
|
||
} |