Created
April 28, 2011 07:22
-
-
Save Olric/945948 to your computer and use it in GitHub Desktop.
Smile parser bug
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
import org.elasticsearch.action.ActionFuture; | |
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest; | |
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.index.IndexRequest; | |
import org.elasticsearch.action.index.IndexResponse; | |
import org.elasticsearch.action.search.SearchRequest; | |
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.index.query.xcontent.QueryBuilders; | |
import org.elasticsearch.search.SearchHit; | |
import org.elasticsearch.search.builder.SearchSourceBuilder; | |
import java.net.InetSocketAddress; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class EsBugTest { | |
String es_clusterName = "testCluster"; | |
String es_hostName = "192.168.1.102"; | |
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 = "bugtype"; | |
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); | |
HashMap props = new HashMap(); | |
props.put("a_aaaabbbbccccddddee", "start"); | |
props.put("z_aaaabbbbccccddddee", "end"); | |
IndexRequest indexRequest = new IndexRequest(indexName); | |
indexRequest.type(typeName); | |
indexRequest.source(props, Requests.CONTENT_TYPE); | |
indexRequest.opType(IndexRequest.OpType.INDEX); | |
ActionFuture<IndexResponse> future = client.index(indexRequest); | |
future.actionGet(timeout); | |
client.admin().indices().refresh(new RefreshRequest(indexName)).actionGet(); | |
SearchRequest searchRequest = new SearchRequest(); | |
SearchSourceBuilder ssb = new SearchSourceBuilder(); | |
ssb.query(QueryBuilders.queryString("*:*")); | |
searchRequest.source(ssb); | |
searchRequest.indices(new String[]{indexName}); | |
searchRequest.types(typeName); | |
SearchHit[] searchHits = client.search(searchRequest).actionGet().hits().hits(); | |
System.out.println("source start"); | |
System.out.println(new String(searchHits[0].source())); | |
System.out.println("source end"); | |
System.out.println("a_aaaabbbbccccddddee : " + searchHits[0].sourceAsMap().get("a_aaaabbbbccccddddee")); | |
System.out.println("z_aaaabbbbccccddddee : " + searchHits[0].sourceAsMap().get("z_aaaabbbbccccddddee")); | |
} | |
finally { | |
disconnect(); | |
} | |
} | |
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