Skip to content

Instantly share code, notes, and snippets.

@enguzekli
Created June 19, 2011 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enguzekli/1034810 to your computer and use it in GitHub Desktop.
Save enguzekli/1034810 to your computer and use it in GitHub Desktop.
Strange Smile Bug
import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;
public class EsBugTest {
String es_clusterName = "cluster";
String es_hostName = "localhost";
int es_port = 9300;
int timeout = 1000;
Client client;
public static void main(String[] args) throws Exception {
new EsBugTest().testBug();
}
private void testBug() throws Exception {
String indexName = "bugindex";
String typeName = "type1_v0";
try {
connect();
try {
System.out.println("delete index " + indexName);
deleteIndex(indexName);
}
catch (Exception e) {
System.out.println("Can not delete index " + indexName + "Reason:" + e);
}
Thread.sleep(1000);
//we have a type which has an integer property, a long property and 10 other dynamically mapped properties.
CreateIndexRequest indexReq = new CreateIndexRequest(indexName);
XContentBuilder mapping = getMappingString(typeName);
indexReq.mapping(typeName, mapping);
client.admin().indices().create(indexReq);
Thread.sleep(1000);
HashMap props = new HashMap();
for(int i=0; i < 10; i++){
props.put("prop"+i, System.currentTimeMillis());
}
// we assigned floating point number to a long field by mistake in our unit tests.
props.put("longProp", 1.0f);
props.put("intProp", 3);
IndexRequest indexRequest = new IndexRequest(indexName);
indexRequest.type(typeName);
indexRequest.source(new HashMap(props), Requests.CONTENT_TYPE);
indexRequest.opType(IndexRequest.OpType.INDEX);
ActionFuture<IndexResponse> future = client.index(indexRequest);
IndexResponse response = future.actionGet(timeout);
client.admin().indices().refresh(new RefreshRequest(indexName)).actionGet();
GetRequest getRequest = new GetRequest(indexName, typeName, response.getId());
GetResponse getResponse = client.get(getRequest).actionGet();
//Value of intProp should be integer however it returns as float
System.out.println("intProp value : " + getResponse.sourceAsMap().get("intProp"));
}
finally {
disconnect();
}
}
private XContentBuilder getMappingString(String type) throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject().
startObject(type).startObject("properties");
builder.startObject("longProp").field("include_in_all", "true").field("store", "no").field("type", "float").field("null_value", 1).endObject();
builder.startObject("intProp").field("include_in_all", "true").field("store", "no").field("type", "integer").field("null_value", -1).endObject();
builder.endObject().endObject().endObject();
return builder;
}
private boolean deleteIndex(String indexName) throws Exception {
DeleteIndexRequest deleteRequest = new DeleteIndexRequest(indexName);
IndicesAdminClient adminClient = client.admin().indices();
ActionFuture<DeleteIndexResponse> future = adminClient.delete(deleteRequest);
DeleteIndexResponse response = future.actionGet(timeout);
return response.acknowledged();
}
private void connect() throws Exception {
Map<String, String> m = new HashMap<String, String>();
m.put("cluster.name", es_clusterName);
Settings s = ImmutableSettings.settingsBuilder().put(m).build();
client = new TransportClient(s);
try {
((TransportClient) client).addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(es_hostName, es_port)));
ClusterHealthRequest healthRequest = new ClusterHealthRequest();
client.admin().cluster().health(healthRequest).actionGet();
}
catch (Exception e) {
client.close();
throw e;
}
}
private void disconnect() {
client.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment