-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.searchbox.core.search.aggregation; | ||
|
||
import com.google.gson.JsonObject; | ||
|
||
import static io.searchbox.core.search.aggregation.AggregationField.VALUE; | ||
|
||
/** | ||
* @author cfstout | ||
*/ | ||
public abstract class SingleValueAggregation extends Aggregation { | ||
|
||
private Double value; | ||
|
||
protected SingleValueAggregation(String name, JsonObject singleValueAggregation) { | ||
super(name, singleValueAggregation); | ||
value = singleValueAggregation.get(String.valueOf(VALUE)).getAsDouble(); | ||
} | ||
|
||
protected Double getValue() { | ||
return value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package io.searchbox.core.search.aggregation; | ||
|
||
import io.searchbox.common.AbstractIntegrationTest; | ||
import io.searchbox.core.Index; | ||
import io.searchbox.core.Search; | ||
import io.searchbox.core.SearchResult; | ||
import io.searchbox.params.Parameters; | ||
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; | ||
import org.elasticsearch.test.ElasticsearchIntegrationTest; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author cfstout | ||
*/ | ||
@ElasticsearchIntegrationTest.ClusterScope (scope = ElasticsearchIntegrationTest.Scope.SUITE, numDataNodes = 1) | ||
public class AvgAggregationIntegrationTest extends AbstractIntegrationTest { | ||
|
||
private SearchResult _result; | ||
|
||
@Before | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
cfstout
Owner
|
||
public void getResult() | ||
throws IOException { | ||
createIndex("avg_aggregation"); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
client().admin().indices().putMapping(new PutMappingRequest("avg_aggregation") | ||
This comment has been minimized.
Sorry, something went wrong. |
||
.type("document") | ||
.source("{\"document\":{\"properties\":{\"delivery\":{\"store\":true,\"type\":\"date\"}}}}") | ||
).actionGet(); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
String query = "{\n" + | ||
" \"query\" : {\n" + | ||
" \"match_all\" : {}\n" + | ||
" },\n" + | ||
" \"aggs\" : {\n" + | ||
" \"avg1\" : {\n" + | ||
" \"avg\" : {\n" + | ||
" \"field\" : \"num\"\n" + | ||
" }\n" + | ||
" }\n" + | ||
" }\n" + | ||
"}"; | ||
|
||
Index index = new Index.Builder("{\"num\":2}") | ||
.index("avg_aggregation") | ||
.type("document") | ||
.setParameter(Parameters.REFRESH, true) | ||
.build(); | ||
client.execute(index); | ||
This comment has been minimized.
Sorry, something went wrong.
kramer
|
||
|
||
index = new Index.Builder("{\"num\":3}") | ||
.index("avg_aggregation") | ||
.type("document") | ||
.setParameter(Parameters.REFRESH, true) | ||
.build(); | ||
client.execute(index); | ||
|
||
Search search = new Search.Builder(query) | ||
.addIndex("avg_aggregation") | ||
.addType("document") | ||
.build(); | ||
_result = client.execute(search); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} | ||
|
||
@Test | ||
public void testGetAvgAggregationByName() { | ||
AvgAggregation average = _result.getAggregations().getAvgAggregation("avg1"); | ||
assertEquals("avg1", average.getName()); | ||
assertEquals(new Double(2.5) , average.getAvg()); | ||
} | ||
|
||
@Test | ||
public void testGetAvgAggregationByType() { | ||
Aggregation aggregation = _result.getAggregations().getAggregation("avg1", AvgAggregation.class); | ||
assertTrue(aggregation instanceof AvgAggregation); | ||
AvgAggregation average = (AvgAggregation) aggregation; | ||
assertEquals("avg1", average.getName()); | ||
assertEquals(new Double(2.5), average.getAvg()); | ||
} | ||
|
||
@Test | ||
public void testGetAvgAggregationWithMap() { | ||
Map<String, Class> nameToTypeMap = new HashMap<String, Class>(); | ||
nameToTypeMap.put("avg1", AvgAggregation.class); | ||
List<Aggregation> aggregations = _result.getAggregations().getAggregations(nameToTypeMap); | ||
assertEquals(1, aggregations.size()); | ||
assertTrue(aggregations.get(0) instanceof AvgAggregation); | ||
AvgAggregation average = (AvgAggregation) aggregations.get(0); | ||
assertEquals("avg1", average.getName()); | ||
assertEquals(new Double(2.5), average.getAvg()); | ||
} | ||
|
||
} |
Create 3 separate test cases instead please. Readability and isolation is more important than code duplication in tests.