Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kimchy
Created May 3, 2012 18:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimchy/2587993 to your computer and use it in GitHub Desktop.
Save kimchy/2587993 to your computer and use it in GitHub Desktop.
import com.google.common.collect.UnmodifiableIterator;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
/**
*/
public class Test {
public static void main(String[] args) {
ImmutableSettings.Builder nodeSettings = ImmutableSettings.settingsBuilder().put("gateway.type", "none");
Node node1 = NodeBuilder.nodeBuilder().settings(nodeSettings).node();
Node node2 = NodeBuilder.nodeBuilder().settings(nodeSettings).node();
TransportClient client = new TransportClient()
.addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
ClusterState clusterState = client.admin().cluster().prepareState().execute().actionGet().state();
DiscoveryNodes nodes = clusterState.nodes();
// create an index with 2 shards
client.admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 2)).execute().actionGet();
client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet();
// index a single doc
client.prepareIndex("test", "type", "1").setSource("field", "value1").execute().actionGet();
// make the previous index visible to search with refresh
client.admin().indices().prepareRefresh().execute().actionGet();
UnmodifiableIterator<DiscoveryNode> iterator = nodes.nodes().values().iterator();
String firstNodeId = iterator.next().id();
String secondNodeId = iterator.next().id();
// one of hte next search requests will return 1
SearchResponse searchResponse = client.prepareSearch("test")
.setPreference("_shards:0;_prefer_node:" + firstNodeId)
.setQuery(QueryBuilders.matchAllQuery())
.setSearchType(SearchType.SCAN)
.setScroll(TimeValue.timeValueSeconds(10))
.execute().actionGet();
System.out.println("Got " + searchResponse.hits().totalHits() + " hits");
searchResponse = client.prepareSearch("test")
.setPreference("_shards:1;_prefer_node:" + firstNodeId)
.setQuery(QueryBuilders.matchAllQuery())
.setSearchType(SearchType.SCAN)
.setScroll(TimeValue.timeValueSeconds(10))
.execute().actionGet();
System.out.println("Got " + searchResponse.hits().totalHits() + " hits");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment