Skip to content

Commit

Permalink
Merge pull request #12 from rahul1193/rahul/rest-client-1.4.1
Browse files Browse the repository at this point in the history
convert pre_zone and post_zone to pre_offset and post_offset resp. in…
  • Loading branch information
brandonkearby authored Jan 22, 2017
2 parents 6b4da25 + 99e732e commit 80db68a
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 38 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>1.4.1-rest-1.0.35</version>
<version>1.4.1-rest-1.0.36</version>
<packaging>jar</packaging>
<description>Elasticsearch - Open Source, Distributed, RESTful Search Engine</description>
<inceptionYear>2009</inceptionYear>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class SearchResponse extends ActionResponse implements StatusToXContent {

private int successfulShards;

private ShardSearchFailure[] shardFailures;
private ShardSearchFailure[] shardFailures = ShardSearchFailure.EMPTY_ARRAY;

private long tookInMillis;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
package org.elasticsearch.search.aggregations.bucket.histogram;

import org.elasticsearch.Version;
import org.elasticsearch.common.joda.DateMathParser;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContentUtils;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.search.aggregations.ValuesSourceAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilderException;
import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;

import java.io.IOException;

Expand Down Expand Up @@ -203,15 +205,34 @@ protected XContentBuilder doInternalXContent(XContentBuilder builder, Params par
}

if (ToXContentUtils.getVersionFromParams(params).onOrAfter(Version.V_5_0_0)) {
//TODO fix this to relevant preOffset and postOffset.

Long preOffsetInLong = convertZoneToOffset(preOffset, preZone);
if (preOffsetInLong != null && preOffsetInLong != 0) {
builder.field("pre_offset", preOffsetInLong);
}

Long postOffsetInLong = convertZoneToOffset(postOffset, postZone);
if (postOffsetInLong != null) {
builder.field("post_offset", postOffsetInLong);
}

} else {

if (preZone != null) {
builder.field("pre_zone", preZone);
}

if (postZone != null) {
builder.field("post_zone", postZone);
}

if (preOffset != null) {
builder.field("pre_offset", preOffset);
}

if (postOffset != null) {
builder.field("post_offset", postOffset);
}
}

if (preZoneAdjustLargeInterval) {
Expand All @@ -222,14 +243,6 @@ protected XContentBuilder doInternalXContent(XContentBuilder builder, Params par
builder.field("reversePostTz", true);
}

if (preOffset != null) {
builder.field("pre_offset", preOffset);
}

if (postOffset != null) {
builder.field("post_offset", postOffset);
}

if (factor != 1.0f) {
builder.field("factor", factor);
}
Expand All @@ -252,4 +265,24 @@ protected XContentBuilder doInternalXContent(XContentBuilder builder, Params par
return builder;
}

private Long convertZoneToOffset(String offset, String zone) throws IOException {
Long offsetAsLong = 0L;
if (offset != null) {
offsetAsLong = parseOffset(offset);
}
if (zone != null) {
int offsetInMillis = DateMathParser.parseZone(zone).getOffset(DateTimeUtils.currentTimeMillis());
offsetAsLong += offsetInMillis;
}
return offsetAsLong;
}

private long parseOffset(String offset) throws IOException {
if (offset.charAt(0) == '-') {
return -TimeValue.parseTimeValue(offset.substring(1), null).millis();
}
int beginIndex = offset.charAt(0) == '+' ? 1 : 0;
return TimeValue.parseTimeValue(offset.substring(beginIndex), null).millis();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
Expand Down Expand Up @@ -61,7 +62,7 @@ public abstract class AbstractRestClientTest {
protected IndicesAdminClient indicesAdminClient;
protected ClusterAdminClient clusterAdminClient;
protected String index;
protected String type="stats";
protected String type = "stats";
protected Client client;

public static final String POSTS_INDEX = "posts";
Expand Down Expand Up @@ -95,26 +96,33 @@ enum Genre {
@Before
public void setUp() {
if (USE_REST) {
client = RestClient.builder("localhost")
.setMaxRetryTimeout(new TimeValue(60, TimeUnit.SECONDS))
.setMaxResponseSize(new ByteSizeValue(1, ByteSizeUnit.GB))
.setSocketTimeout(new TimeValue(60, TimeUnit.SECONDS))
.build();
}
else {
TransportClient transportClient = new TransportClient();
transportClient.addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
client = transportClient;
client = createRestClient();
} else {
client = createTransportClient();
}

this.indicesAdminClient = client.admin().indices();
this.clusterAdminClient = client.admin().cluster();
this.index = createIndex();
}

protected TransportClient createTransportClient() {
TransportClient transportClient = new TransportClient();
transportClient.addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
return transportClient;
}

protected RestClient createRestClient() {
return RestClient.builder("localhost")
.setMaxRetryTimeout(new TimeValue(60, TimeUnit.SECONDS))
.setMaxResponseSize(new ByteSizeValue(1, ByteSizeUnit.GB))
.setSocketTimeout(new TimeValue(60, TimeUnit.SECONDS))
.build();
}

protected List<IndexResponse> indexDocument(int numberOfDocs) throws InterruptedException, ExecutionException {
List<IndexResponse> responses = Lists.newArrayList();
for (int i =0; i < numberOfDocs; i++) {
for (int i = 0; i < numberOfDocs; i++) {
responses.add(indexDocument());
}
return responses;
Expand All @@ -128,6 +136,14 @@ protected IndexResponse indexDocument() throws InterruptedException, ExecutionEx
return indexResponse;
}

protected void indexDocument(Client... clients) throws InterruptedException, ExecutionException {
IndexRequest request = newIndexRequest();
for (Client client : clients) {
IndexResponse indexResponse = client.index(request).get();
assertTrue(indexResponse.isCreated());
}
}

protected IndexRequest newPost() {
IndexRequest request = newCommentOrPost(POST_TYPE);
return request;
Expand Down Expand Up @@ -191,20 +207,20 @@ protected IndexRequest newIndexRequest() {
}

protected List<String> names;

protected String randomName() {
if (names == null) {
names = Lists.newArrayList();
InputStream in = this.getClass().getResourceAsStream("/config/names.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in, Charsets.UTF_8));
String name;
try {
while( (name=reader.readLine()) != null) {
while ((name = reader.readLine()) != null) {
names.add(name);
}
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
finally {
} finally {
try {
reader.close();
} catch (IOException e) {
Expand Down Expand Up @@ -253,6 +269,11 @@ protected void deleteIndex(String index) {
assertAcknowledged(response);
}

protected void deleteIndex(String index, IndicesAdminClient client) {
DeleteIndexResponse response = client.prepareDelete(index).get();
assertAcknowledged(response);
}

protected String createIndex() {
String index = UUID.randomUUID().toString();
CreateIndexResponse response = indicesAdminClient.prepareCreate(index)
Expand Down
95 changes: 94 additions & 1 deletion src/test/java/org/elasticsearch/client/rest/RestClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.elasticsearch.action.support.QuerySourceBuilder;
import org.elasticsearch.action.support.replication.ReplicationType;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.unit.DistanceUnit;
Expand Down Expand Up @@ -106,7 +107,6 @@
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.omg.CORBA.PUBLIC_MEMBER;

import java.io.IOException;
import java.util.*;
Expand Down Expand Up @@ -375,6 +375,99 @@ public void testBulkIndex() throws ExecutionException, InterruptedException {
}
}

@Test
public void testDateHistogram() throws ExecutionException, InterruptedException {
TransportClient transportClient = createTransportClient();
try {
for (int i = 0; i < 5; i++) {
indexDocument(client, transportClient);
}
ConstantScoreQueryBuilder constantScoreQuery = QueryBuilders.constantScoreQuery(QueryBuilders.matchAllQuery());
DateHistogramBuilder dateHistogramBuilder = AggregationBuilders.dateHistogram("rahul")
.interval(DateHistogram.Interval.MONTH)
.field("datePretty")
.preZone("Asia/Kolkata")
.postZone("Asia/Kolkata");
SearchRequestBuilder restSearchRequest = new SearchRequestBuilder(client).setIndices(index);
restSearchRequest.setQuery(constantScoreQuery);
restSearchRequest.addAggregation(dateHistogramBuilder);
restSearchRequest.setSize(0);
SearchResponse restSearchResponse = restSearchRequest.execute().get();
SearchRequestBuilder transportSearchRequest = new SearchRequestBuilder(transportClient).setIndices(index);
transportSearchRequest.setQuery(constantScoreQuery);
transportSearchRequest.addAggregation(dateHistogramBuilder);
transportSearchRequest.setSize(0);
SearchResponse transportSearchResponse = transportSearchRequest.execute().get();
Aggregations aggregations = transportSearchResponse.getAggregations();
DateHistogram rahul = aggregations.get("rahul");
DateHistogram rest_rahul = restSearchResponse.getAggregations().get("rahul");
Set<String> keys = new HashSet<>();
for (DateHistogram.Bucket bucket : rahul.getBuckets()) {
keys.add(bucket.getKey());
assert bucket.getDocCount() == rest_rahul.getBucketByKey(bucket.getKey()).getDocCount();
}
for (DateHistogram.Bucket bucket : rest_rahul.getBuckets()) {
if (keys.contains(bucket.getKey())) {
continue;
}
assert bucket.getDocCount() == 0;
}
} finally {
try {
deleteIndex(index, transportClient.admin().indices());
transportClient.close();
} catch (Exception e) {

}
}
}


@Test
public void testDateHistogramWithOffset() throws ExecutionException, InterruptedException {
TransportClient transportClient = createTransportClient();
try {
for (int i = 0; i < 5; i++) {
indexDocument(client, transportClient);
}
ConstantScoreQueryBuilder constantScoreQuery = QueryBuilders.constantScoreQuery(QueryBuilders.matchAllQuery());
DateHistogramBuilder dateHistogramBuilder = AggregationBuilders.dateHistogram("rahul")
.interval(DateHistogram.Interval.MONTH)
.field("datePretty").preOffset("5h").postOffset("5h");
SearchRequestBuilder restSearchRequest = new SearchRequestBuilder(client).setIndices(index);
restSearchRequest.setQuery(constantScoreQuery);
restSearchRequest.addAggregation(dateHistogramBuilder);
restSearchRequest.setSize(0);
SearchResponse restSearchResponse = restSearchRequest.execute().get();
SearchRequestBuilder transportSearchRequest = new SearchRequestBuilder(transportClient).setIndices(index);
transportSearchRequest.setQuery(constantScoreQuery);
transportSearchRequest.addAggregation(dateHistogramBuilder);
transportSearchRequest.setSize(0);
SearchResponse transportSearchResponse = transportSearchRequest.execute().get();
Aggregations aggregations = transportSearchResponse.getAggregations();
DateHistogram rahul = aggregations.get("rahul");
DateHistogram rest_rahul = restSearchResponse.getAggregations().get("rahul");
Set<String> keys = new HashSet<>();
for (DateHistogram.Bucket bucket : rahul.getBuckets()) {
keys.add(bucket.getKey());
assert bucket.getDocCount() == rest_rahul.getBucketByKey(bucket.getKey()).getDocCount();
}
for (DateHistogram.Bucket bucket : rest_rahul.getBuckets()) {
if (keys.contains(bucket.getKey())) {
continue;
}
assert bucket.getDocCount() == 0;
}
} finally {
try {
deleteIndex(index, transportClient.admin().indices());
transportClient.close();
} catch (Exception e) {

}
}
}

@Test
public void testBulkIndexWithOptTypeCreate() throws ExecutionException, InterruptedException {
BulkRequest request = new BulkRequest();
Expand Down
Loading

0 comments on commit 80db68a

Please sign in to comment.