http://blog.csdn.net/changong28/article/details/38445805#commentsjava
import static org.elasticsearch.node.NodeBuilder.nodeBuilder; import org.elasticsearch.client.Client; import org.elasticsearch.node.Node; Node node = nodeBuilder().clusterName("escluster2").client(true). node(); Client client = node.client();
import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "escluster2").build(); TransportClient client = new TransportClient(settings); client.addTransportAddress(new InetSocketTransportAddress("127.0.0.1",9300));
GetResponse response = client.prepareGet("library", "book", "1") .setFields("title", "_source") .execute().actionGet();
import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.Client; IndexResponse response = client.prepareIndex("library", "book", "2") .setSource("{ \"title\": \"Mastering ElasticSearch\"}") .execute().actionGet();
import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.Client; import java.util.Map; import org.elasticsearch.common.collect.Maps; Map<String, Object> params = Maps.newHashMap(); params.put("ntitle", "ElasticSearch Server Book"); UpdateResponse response = client.prepareUpdate("library", "book", "2") .setScript("ctx._source.title = ntitle") .setScriptParams(params) .execute().actionGet();
import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.client.Client; DeleteResponse response = client.prepareDelete("library", "book", "2") .execute().actionGet();
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; import org.elasticsearch.search.SearchHit; SearchResponse response = client.prepareSearch("library") .addFields("title", "_source") .execute().actionGet(); for(SearchHit hit: response.getHits().getHits()) { <span style="white-space:pre"> </span>System.out.println(hit.getId()); <span style="white-space:pre"> </span>if (hit.getFields().containsKey("title")) { <span style="white-space:pre"> </span>System.out.println("field.title: "+ hit.getFields().get("title").getValue()); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>System.out.println("source.title: " + hit.getSource().get("title")); }
import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; QueryBuilder queryBuilder = QueryBuilders .disMaxQuery() .add(QueryBuilders.termQuery("title", "Elastic")) .add(QueryBuilders.prefixQuery("title", "el")); System.out.println(queryBuilder.toString()); SearchResponse response = client.prepareSearch("library") .setQuery(queryBuilder) .execute().actionGet();
queryBuilder = QueryBuilders.matchAllQuery() .boost(11f).normsField("title");
queryBuilder = QueryBuilders .matchQuery("message", "a quick brown fox") .operator(Operator.AND) .zeroTermsQuery(ZeroTermsQuery.ALL);
queryBuilder = QueryBuilders.geoShapeQuery("location", ShapeBuilder.newRectangle() .topLeft(13, 53) .bottomRight(14, 52) .build());
SearchResponse response = client.prepareSearch("library") .setQuery(QueryBuilders.matchAllQuery()) .setFrom(10) .setSize(20) .execute().actionGet();
SearchResponse response = client.prepareSearch("library") .setQuery(QueryBuilders.matchAllQuery()) .addSort(SortBuilders.fieldSort("title")) .addSort("_score", SortOrder.DESC) .execute().actionGet()
FilterBuilder filterBuilder = FilterBuilders .andFilter( FilterBuilders.existsFilter("title").filterName("exist"), FilterBuilders.termFilter("title", "elastic") ); SearchResponse response = client.prepareSearch("library") .setFilter(filterBuilder) .execute().actionGet();
FacetBuilder facetBuilder = FacetBuilders .filterFacet("test") .filter(FilterBuilders.termFilter("title", "elastic")); SearchResponse response = client.prepareSearch("library") .addFacet(facetBuilder) .execute().actionGet();
SearchResponse response = client.prepareSearch("wikipedia") .addHighlightedField("title") .setQuery(QueryBuilders.termQuery("title", "actress")) .setHighlighterPreTags("<1>", "<2>") .setHighlighterPostTags("</1>", "</2>") .execute().actionGet(); for(SearchHit hit: response.getHits().getHits()) { <span style="white-space:pre"> </span>HighlightField hField = hit.getHighlightFields().get("title"); <span style="white-space:pre"> </span>for (Text t : hField.fragments()) { <span style="white-space:pre"> </span>System.out.println(t.string()); <span style="white-space:pre"> </span>} }
SearchResponse response = client.prepareSearch("wikipedia") .setQuery(QueryBuilders.matchAllQuery()) .addSuggestion(new TermSuggestionBuilder("first_suggestion") .text("graphics designer") .field("_all")) .execute().actionGet(); for( Entry<? extends Option> entry : response.getSuggest().getSuggestion("first_suggestion").getEntries()) { <span style="white-space:pre"> </span>System.out.println("Check for: " + entry.getText() + ". Options:"); <span style="white-space:pre"> </span>for( Option option : entry.getOptions()) { <span style="white-space:pre"> </span>System.out.println("\t" + option.getText()); <span style="white-space:pre"> </span>} }
CountResponse response = client.prepareCount("library") .setQuery(QueryBuilders.termQuery("title", "elastic")) .execute().actionGet();
SearchResponse responseSearch = client.prepareSearch("library") .setScroll("1m") .setSearchType(SearchType.SCAN) .execute().actionGet(); String scrollId = responseSearch.getScrollId(); SearchResponse response = client.prepareSearchScroll(scrollId).execute().actionGet();
BulkResponse response = client.prepareBulk() .add(client.prepareIndex("library", "book", "5") .setSource("{ \"title\" : \"Solr Cookbook\"}") .request()) .add(client.prepareDelete("library", "book", "2").request()).execute().actionGet();
DeleteByQueryResponse response = client.prepareDeleteByQuery("library") .setQuery(QueryBuilders.termQuery("title", "ElasticSearch")) .execute().actionGet();
MultiGetResponse response = client.prepareMultiGet() .add("library", "book", "1", "2") .execute().actionGet();
MultiSearchResponse response = client.prepareMultiSearch() .add(client.prepareSearch("library", "book").request()) .add(client.prepareSearch("news"). .setFilter(FilterBuilders.termFilter("tags", "important"))) .execute().actionGet();
IndexResponse response = client .prepareIndex("library", "book", "2") .setSource("{ \"title\": \"Mastering ElasticSearch\"}") .execute().actionGet(); Map<String, Object> m = Maps.newHashMap(); m.put("1", "Introduction"); m.put("2", "Basics"); m.put("3", "And the rest"); XContentBuilder json = XContentFactory.jsonBuilder().prettyPrint() .startObject() .field("id").value("2123") .field("lastCommentTime", new Date()) .nullField("published") .field("chapters").map(m) .field("title", "Mastering ElasticSearch") .array("tags", "search", "ElasticSearch", "nosql") .field("values") .startArray() .value(1) .value(10) .endArray() .endObject();
ClusterHealthResponse response = client.admin().cluster() .prepareHealth("library") .execute().actionGet();
ClusterStateResponse response = client.admin().cluster() .prepareState() .execute().actionGet();
Map<String, Object> map = Maps.newHashMap(); map.put("indices.ttl.interval", "10m"); ClusterUpdateSettingsResponse response = client.admin().cluster() .prepareUpdateSettings() .setTransientSettings(map) .execute().actionGet();
ClusterRerouteResponse response = client.admin().cluster() .prepareReroute() .setDryRun(true) .add(new MoveAllocationCommand(new ShardId("library", 3), "G3czOt4HQbKZT1RhpPCULw",PvHtEMuRSJ6rLJ27AW3U6w"), new CancelAllocationCommand(new ShardId("library", 2), "G3czOt4HQbKZT1RhpPCULw",rue)) .execute().actionGet();
NodesInfoResponse response = client.admin().cluster() .prepareNodesInfo() .setNetwork(true) .setPlugin(true) .execute().actionGet();
NodesStatsResponse response = client.admin().cluster() .prepareNodesStats() .all() .execute().actionGet();
NodesHotThreadsResponse response = client.admin().cluster() .prepareNodesHotThreads() .execute().actionGet();
NodesShutdownResponse response = client.admin().cluster() .prepareNodesShutdown() .execute().actionGet();
ClusterSearchShardsResponse response = client.admin().cluster() .prepareSearchShards() .setIndices("library") .setRouting("12") .execute().actionGet();
IndicesExistsResponse response = client.admin().indices() .prepareExists("books", "library") .execute().actionGet();
TypesExistsResponse response = client.admin().indices() .prepareTypesExists("library") .setTypes("book") .execute().actionGet();
IndicesStatsResponse response = client.admin().indices() .prepareStats("library") .all() .execute().actionGet();
IndicesStatusResponse response = client.admin().indices() .prepareStatus("library") .setRecovery(true) .setSnapshot(true) .execute().actionGet();
IndicesSegmentResponse response = client.admin().indices() .prepareSegments("library") .execute().actionGet();
3.4.2.6 Creating an index APInode
CreateIndexResponse response = client.admin().indices() .prepareCreate("news") .setSettings(ImmutableSettings.settingsBuilder() .put("number_of_shards", 1)) .addMapping("news", XContentFactory.jsonBuilder() .startObject() .startObject("news") .startObject("properties") .startObject("title") .field("analyzer", "whitespace") .field("type", "string") .endObject() .endObject() .endObject() .endObject()) .execute().actionGet();
DeleteIndexResponse response = client.admin().indices() .prepareDelete("news") .execute().actionGet();
CloseIndexResponse response = client.admin().indices() .prepareClose("library") .execute().actionGet();
OpenIndexResponse response = client.admin().indices() .prepareOpen("library") .execute().actionGet();
RefreshResponse response = client.admin().indices() .prepareRefresh("library") .execute().actionGet();
FlushResponse response = client.admin().indices() .prepareFlush("library") .setFull(false) .execute().actionGet();
OptimizeResponse response = client.admin().indices() .prepareOptimize("library") .setMaxNumSegments(2) .setFlush(true) .setOnlyExpungeDeletes(false) .execute().actionGet();
PutMappingResponse response = client.admin().indices() .preparePutMapping("news") .setType("news") .setSource(XContentFactory.jsonBuilder() .startObject() .startObject("news") .startObject("properties") .startObject("title") .field("analyzer", "whitespace") .field("type", "string") .endObject() .endObject() .endObject() .endObject()) .execute().actionGet();
DeleteMappingResponse response = client.admin().indices() .prepareDeleteMapping("news") .setType("news") .execute().actionGet();
GatewaySnapshotResponse response = client.admin().indices() .prepareGatewaySnapshot("news") .execute().actionGet();
IndicesAliasesResponse response = client.admin().indices() .prepareAliases() .addAlias("news", "n") .addAlias("library", "elastic_books", FilterBuilders.termFilter("title", "elasticsearch")) .removeAlias("news", "current_news") .execute().actionGet();
IndicesGetAliasesResponse response = client.admin().indices() .prepareGetAliases("elastic_books", "n") .execute().actionGet();
AliasesExistResponse response = client.admin().indices() .prepareAliasesExist("elastic*", "unknown") .execute().actionGet();
ClearIndicesCacheResponse response = client.admin().indices() .prepareClearCache("library") .setFieldDataCache(true) .setFields("title") .setFilterCache(true) .setIdCache(true) .execute().actionGet();
UpdateSettingsResponse response = client.admin().indices() .prepareUpdateSettings("library") .setSettings(ImmutableSettings.builder() .put("index.number_of_replicas", 2)) .execute().actionGet();
AnalyzeResponse response = client.admin().indices() .prepareAnalyze("library", "ElasticSearch Servers") .setTokenizer("whitespace") .setTokenFilters("nGram") .execute().actionGet();
PutIndexTemplateResponse response = client.admin().indices() .preparePutTemplate("my_template") .setTemplate("product*") .setSettings(ImmutableSettings.builder() .put("index.number_of_replicas", 2) .put("index.number_of_shards", 1)) .addMapping("item", XContentFactory.jsonBuilder() .startObject() .startObject("item") .startObject("properties") .startObject("title") .field("type", "string") .endObject() .endObject() .endObject() .endObject()) .execute().actionGet();
DeleteIndexTemplateResponse response = client.admin().indices() .prepareDeleteTemplate("my_*") .execute().actionGet();
ValidateQueryResponse response = client.admin().indices() .prepareValidateQuery("library") .setExplain(true) .setQuery(XContentFactory.jsonBuilder() .startObject() .field("name").value("elastic search") .endObject().bytes()) .execute().actionGet();
PutWarmerResponse response = client.admin().indices() .preparePutWarmer("library_warmer") .setSearchRequest(client.prepareSearch("library") .addFacet(FacetBuilders .termsFacet("tags").field("tags"))) .execute().actionGet();
DeleteWarmerResponse response = client.admin().indices() .prepareDeleteWarmer() .setName("library_*") .execute().actionGet();