Created
May 3, 2012 18:38
-
-
Save kimchy/2587993 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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