es7.9.0 rest-high-level-client

官方客户端api文档 https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.x/java-rest-high-document-index.html

@Slf4j
public class EsMain {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(HttpHost.create("localhost:9200")));
        log.info("{}", restHighLevelClient.info(RequestOptions.DEFAULT));

        new TestIndex().create(restHighLevelClient);
    }

     static class TestIndex {
        public void create(RestHighLevelClient client) throws IOException {
            XContentBuilder builder = XContentFactory.jsonBuilder();
            builder.startObject();
            {
                builder.field("user", "kimchy");
                builder.timeField("postDate", new Date());
                builder.field("message", "trying out Elasticsearch");
            }
            builder.endObject();
            IndexRequest request = new IndexRequest("posts")
                    .id("2").source(builder);

//            IndexRequest indexRequest = new IndexRequest("posts")
//                    .id("1")
//                    .source("user", "kimchy",
//                            "postDate", new Date(),
//                            "message", "trying out Elasticsearch");

            IndexResponse response = client.index(request, RequestOptions.DEFAULT);

            log.info("{}", response);
        }

        public void update(RestHighLevelClient client) throws IOException {
            UpdateRequest request = new UpdateRequest("posts", "1")
                    .doc("field", "value")
                    .setIfSeqNo(101L)
                    .setIfPrimaryTerm(200L);
            request.retryOnConflict(3);

//            request.waitForActiveShards(2);
//            request.waitForActiveShards(ActiveShardCount.ALL);

            ActionListener<UpdateResponse> listener = new ActionListener<UpdateResponse>() {
                @Override
                public void onResponse(UpdateResponse updateResponse) {
                    String index = updateResponse.getIndex();
                    String id = updateResponse.getId();
                    long version = updateResponse.getVersion();
                    if (updateResponse.getResult() == DocWriteResponse.Result.CREATED) {

                    } else if (updateResponse.getResult() == DocWriteResponse.Result.UPDATED) {

                    } else if (updateResponse.getResult() == DocWriteResponse.Result.DELETED) {

                    } else if (updateResponse.getResult() == DocWriteResponse.Result.NOOP) {

                    }

                    ReplicationResponse.ShardInfo shardInfo = updateResponse.getShardInfo();
                    if (shardInfo.getTotal() != shardInfo.getSuccessful()) {

                    }
                    if (shardInfo.getFailed() > 0) {
                        for (ReplicationResponse.ShardInfo.Failure failure :
                                shardInfo.getFailures()) {
                            String reason = failure.reason();
                        }
                    }
                }

                @Override
                public void onFailure(Exception e) {
                    if (e instanceof ElasticsearchException) {
                        ElasticsearchException elasticsearchException = (ElasticsearchException) e;
                        if (elasticsearchException.status() == RestStatus.CONFLICT) {

                        }
                    }
                }
            };;
            client.updateAsync(request, RequestOptions.DEFAULT, listener);
        }

        public void delete(RestHighLevelClient client) throws IOException {
            DeleteRequest request = new DeleteRequest(
                    "posts",
                    "1");

            request.routing("routing");

            request.timeout(TimeValue.timeValueMinutes(2));
//            request.timeout("2m");

            request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
//            request.setRefreshPolicy("wait_for");

//            request.version(2);
            request.versionType(VersionType.EXTERNAL);

            DeleteResponse deleteResponse = client.delete(
                    request, RequestOptions.DEFAULT);
            if (deleteResponse.getResult() == DocWriteResponse.Result.NOT_FOUND) {

            }
        }
    }

    static class TestDocument {
        public void get(RestHighLevelClient client) throws IOException {
            GetRequest getRequest = new GetRequest(
                    "posts",
                    "1");

            GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
            String index = getResponse.getIndex();
            String id = getResponse.getId();
            if (getResponse.isExists()) {
                long version = getResponse.getVersion();
                String sourceAsString = getResponse.getSourceAsString();
                Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
                byte[] sourceAsBytes = getResponse.getSourceAsBytes();
            } else {

            }
        }

        public void bulk(RestHighLevelClient client) throws IOException {
            BulkRequest request = new BulkRequest();
            request.add(new DeleteRequest("posts", "3"));
            request.add(new UpdateRequest("posts", "2")
                    .doc(XContentType.JSON,"other", "test"));
            request.add(new IndexRequest("posts").id("4")
                    .source(XContentType.JSON,"field", "baz"));

            request.pipeline("pipelineId");
            request.routing("routingId");
            BulkRequest defaulted = new BulkRequest("posts");
            BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT);

            // https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.x/java-rest-high-document-bulk.html
            for (BulkItemResponse bulkItemResponse : bulkResponse) {
                DocWriteResponse itemResponse = bulkItemResponse.getResponse();

                switch (bulkItemResponse.getOpType()) {
                    case INDEX:
                    case CREATE:
                        IndexResponse indexResponse = (IndexResponse) itemResponse;
                        break;
                    case UPDATE:
                        UpdateResponse updateResponse = (UpdateResponse) itemResponse;
                        break;
                    case DELETE:
                        DeleteResponse deleteResponse = (DeleteResponse) itemResponse;
                }
            }
        }

        public void multiGet(RestHighLevelClient client) throws IOException {
            MultiGetRequest request = new MultiGetRequest();
            request.add(new MultiGetRequest.Item(
                    "index",
                    "example_id"));
            request.add(new MultiGetRequest.Item("index", "another_id"));
            request.add(new MultiGetRequest.Item("index", "example_id")
                    .fetchSourceContext(FetchSourceContext.DO_NOT_FETCH_SOURCE));
            request.add(new MultiGetRequest.Item("index", "example_id")
                    .fetchSourceContext(FetchSourceContext.DO_NOT_FETCH_SOURCE));
            request.add(new MultiGetRequest.Item("index", "example_id")
                    .storedFields("foo"));
            MultiGetResponse response = client.mget(request, RequestOptions.DEFAULT);
            MultiGetItemResponse item = response.getResponses()[0];
//            String value = item.getResponse().getField("foo").getValue();
//            request.add(new MultiGetRequest.Item("index", "with_routing")
//                    .routing("some_routing"));
//            request.add(new MultiGetRequest.Item("index", "with_version")
//                    .versionType(VersionType.EXTERNAL)
//                    .version(10123L));
//            request.preference("some_preference");
//            request.realtime(false);
//            request.refresh(true);

            if (!item.isFailed()) {
                GetResponse firstGet = item.getResponse();
                String index = item.getIndex();
                String id = item.getId();
                if (firstGet.isExists()) {
                    long version = firstGet.getVersion();
                    String sourceAsString = firstGet.getSourceAsString();
                    Map<String, Object> sourceAsMap = firstGet.getSourceAsMap();
                    byte[] sourceAsBytes = firstGet.getSourceAsBytes();
                } else {

                }
            }
        }
    }

    static class TestQueryString {
        public void updateByQuery(RestHighLevelClient client) throws IOException {
            UpdateByQueryRequest request =
                    new UpdateByQueryRequest("source1", "source2");
            request.setConflicts("proceed");
            request.setQuery(new TermQueryBuilder("user", "kimchy"));
            request.setMaxDocs(10);
            request.setBatchSize(100);
            request.setPipeline("my_pipeline");
            request.setSlices(2);
            request.setScroll(TimeValue.timeValueMinutes(10));
            request.setRouting("=cat");
            request.setTimeout(TimeValue.timeValueMinutes(2));
            request.setRefresh(true);
            request.setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN);

            BulkByScrollResponse bulkResponse =
                    client.updateByQuery(request, RequestOptions.DEFAULT);

            TimeValue timeTaken = bulkResponse.getTook();
            boolean timedOut = bulkResponse.isTimedOut();
            long totalDocs = bulkResponse.getTotal();
            long updatedDocs = bulkResponse.getUpdated();
            long deletedDocs = bulkResponse.getDeleted();
            long batches = bulkResponse.getBatches();
            long noops = bulkResponse.getNoops();
            long versionConflicts = bulkResponse.getVersionConflicts();
            long bulkRetries = bulkResponse.getBulkRetries();
            long searchRetries = bulkResponse.getSearchRetries();
            TimeValue throttledMillis = bulkResponse.getStatus().getThrottled();
            TimeValue throttledUntilMillis =
                    bulkResponse.getStatus().getThrottledUntil();
            List<ScrollableHitSource.SearchFailure> searchFailures =
                    bulkResponse.getSearchFailures();
            List<BulkItemResponse.Failure> bulkFailures =
                    bulkResponse.getBulkFailures();
        }

        public void deleteByQuery(RestHighLevelClient client) {
            DeleteByQueryRequest request =
                    new DeleteByQueryRequest("source1", "source2");
            request.setConflicts("proceed");
            request.setQuery(new TermQueryBuilder("user", "kimchy"));
            request.setMaxDocs(10);
            request.setBatchSize(100);
            request.setSlices(2);
            request.setScroll(TimeValue.timeValueMinutes(10));
            request.setRouting("=cat");

            client.deleteByQueryAsync(request, RequestOptions.DEFAULT, new ActionListener<BulkByScrollResponse>() {
                @Override
                public void onResponse(BulkByScrollResponse bulkResponse) {
                    TimeValue timeTaken = bulkResponse.getTook();
                    boolean timedOut = bulkResponse.isTimedOut();
                    long totalDocs = bulkResponse.getTotal();
                    long deletedDocs = bulkResponse.getDeleted();
                    long batches = bulkResponse.getBatches();
                    long noops = bulkResponse.getNoops();
                    long versionConflicts = bulkResponse.getVersionConflicts();
                    long bulkRetries = bulkResponse.getBulkRetries();
                    long searchRetries = bulkResponse.getSearchRetries();
                    TimeValue throttledMillis = bulkResponse.getStatus().getThrottled();
                    TimeValue throttledUntilMillis =
                            bulkResponse.getStatus().getThrottledUntil();
                    List<ScrollableHitSource.SearchFailure> searchFailures =
                            bulkResponse.getSearchFailures();
                    List<BulkItemResponse.Failure> bulkFailures =
                            bulkResponse.getBulkFailures();
                }

                @Override
                public void onFailure(Exception e) {

                }
            });
        }
    }

    static class TestDSL {
        public void search(RestHighLevelClient client) throws IOException {
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//            searchSourceBuilder.query(QueryBuilders.matchAllQuery());

            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            sourceBuilder.query(QueryBuilders.termQuery("user", "kimchy"));
            sourceBuilder.from(0);
            sourceBuilder.size(5);
            sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

            SearchRequest searchRequest = new SearchRequest();
            searchRequest.indices("posts");
            searchRequest.source(sourceBuilder);

            QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("user", "kimchy")
                    .fuzziness(Fuzziness.AUTO)
                    .prefixLength(3)
                    .maxExpansions(10);

            searchSourceBuilder.query(matchQueryBuilder);

//            sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
            sourceBuilder.sort(new FieldSortBuilder("id").order(SortOrder.ASC));
            sourceBuilder.fetchSource(false);
            String[] includeFields = new String[] {"title", "innerObject.*"};
            String[] excludeFields = new String[] {"user"};
            sourceBuilder.fetchSource(includeFields, excludeFields);

            HighlightBuilder highlightBuilder = new HighlightBuilder();
            HighlightBuilder.Field highlightTitle =
                    new HighlightBuilder.Field("title");
            highlightTitle.highlighterType("unified");
            highlightBuilder.field(highlightTitle);
            HighlightBuilder.Field highlightUser = new HighlightBuilder.Field("user");
            highlightBuilder.field(highlightUser);
            searchSourceBuilder.highlighter(highlightBuilder);

            TermsAggregationBuilder aggregation = AggregationBuilders.terms("by_company")
                    .field("company.keyword");
            aggregation.subAggregation(AggregationBuilders.avg("average_age")
                    .field("age"));
            searchSourceBuilder.aggregation(aggregation);

            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            RestStatus status = searchResponse.status();
            TimeValue took = searchResponse.getTook();
            Boolean terminatedEarly = searchResponse.isTerminatedEarly();
            boolean timedOut = searchResponse.isTimedOut();

            int totalShards = searchResponse.getTotalShards();
            int successfulShards = searchResponse.getSuccessfulShards();
            int failedShards = searchResponse.getFailedShards();
            for (ShardSearchFailure failure : searchResponse.getShardFailures()) {
                // failures should be handled here
            }

            SearchHits hits = searchResponse.getHits();
            TotalHits totalHits = hits.getTotalHits();
// the total number of hits, must be interpreted in the context of totalHits.relation
            long numHits = totalHits.value;
// whether the number of hits is accurate (EQUAL_TO) or a lower bound of the total (GREATER_THAN_OR_EQUAL_TO)
            TotalHits.Relation relation = totalHits.relation;
            float maxScore = hits.getMaxScore();

            SearchHit[] searchHits = hits.getHits();
            for (SearchHit hit : searchHits) {
                String index = hit.getIndex();
                String id = hit.getId();
                float score = hit.getScore();

                String sourceAsString = hit.getSourceAsString();
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String documentTitle = (String) sourceAsMap.get("title");
                List<Object> users = (List<Object>) sourceAsMap.get("user");
                Map<String, Object> innerObject =
                        (Map<String, Object>) sourceAsMap.get("innerObject");

                Map<String, HighlightField> highlightFields = hit.getHighlightFields();
                HighlightField highlight = highlightFields.get("title");
                Text[] fragments = highlight.fragments();
                String fragmentString = fragments[0].string();
            }

            Aggregations aggregations = searchResponse.getAggregations();
//            Terms byCompanyAggregation = aggregations.get("by_company");
//            Terms.Bucket elasticBucket = byCompanyAggregation.getBucketByKey("Elastic");
//            Avg averageAge = elasticBucket.getAggregations().get("average_age");
//            double avg = averageAge.getValue();

            Range range = aggregations.get("by_company");
            Map<String, Aggregation> aggregationMap = aggregations.getAsMap();
            Terms companyAggregation = (Terms) aggregationMap.get("by_company");
            List<Aggregation> aggregationList = aggregations.asList();
            for (Aggregation agg : aggregations) {
                String type = agg.getType();
                if (type.equals(TermsAggregationBuilder.NAME)) {
                    Terms.Bucket elasticBucket = ((Terms) agg).getBucketByKey("Elastic");
                    long numberOfDocs = elasticBucket.getDocCount();
                }
            }
        }

        public void searchScroll(RestHighLevelClient client) throws IOException {
            SearchRequest searchRequest = new SearchRequest("posts");
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(QueryBuilders.matchQuery("title", "Elasticsearch"));
            searchSourceBuilder.size(5);
            searchRequest.source(searchSourceBuilder);
            searchRequest.scroll(TimeValue.timeValueMinutes(1L));
            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            String scrollId = searchResponse.getScrollId();
            SearchHit[] searchHits = searchResponse.getHits().getHits();

            SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
            scrollRequest.scroll(TimeValue.timeValueSeconds(30));
//            scrollRequest.scroll("60s");
            SearchResponse searchScrollResponse = client.scroll(scrollRequest, RequestOptions.DEFAULT);
            scrollId = searchScrollResponse.getScrollId();

            final Scroll scroll = new Scroll(TimeValue.timeValueMinutes(1L));
            while (searchHits != null && searchHits.length > 0) {

                scrollRequest = new SearchScrollRequest(scrollId);
                scrollRequest.scroll(scroll);
                searchResponse = client.scroll(scrollRequest, RequestOptions.DEFAULT);
                scrollId = searchResponse.getScrollId();
                searchHits = searchResponse.getHits().getHits();
            }

            ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
            clearScrollRequest.addScrollId(scrollId);
            ClearScrollResponse clearScrollResponse = client.clearScroll(clearScrollRequest, RequestOptions.DEFAULT);
            boolean succeeded = clearScrollResponse.isSucceeded();
        }

        public void multiSearch(RestHighLevelClient client) throws IOException {
            MultiSearchRequest request = new MultiSearchRequest();
            SearchRequest firstSearchRequest = new SearchRequest();
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(QueryBuilders.matchQuery("user", "kimchy"));
            firstSearchRequest.source(searchSourceBuilder);
            request.add(firstSearchRequest);
            SearchRequest secondSearchRequest = new SearchRequest();
            searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(QueryBuilders.matchQuery("user", "luca"));
            secondSearchRequest.source(searchSourceBuilder);
            request.add(secondSearchRequest);

            MultiSearchResponse response = client.msearch(request, RequestOptions.DEFAULT);

            MultiSearchResponse.Item firstResponse = response.getResponses()[0];
            SearchResponse searchResponse = firstResponse.getResponse();
            MultiSearchResponse.Item secondResponse = response.getResponses()[1];
            searchResponse = secondResponse.getResponse();
        }

        public void fieldCabilities(RestHighLevelClient client) throws IOException {
            FieldCapabilitiesRequest request = new FieldCapabilitiesRequest();
            FieldCapabilitiesResponse response = client.fieldCaps(request, RequestOptions.DEFAULT);

            Map<String, FieldCapabilities> userResponse = response.getField("user");
            FieldCapabilities textCapabilities = userResponse.get("keyword");

            boolean isSearchable = textCapabilities.isSearchable();
            boolean isAggregatable = textCapabilities.isAggregatable();

            String[] indices = textCapabilities.indices();
            String[] nonSearchableIndices = textCapabilities.nonSearchableIndices();
            String[] nonAggregatableIndices = textCapabilities.nonAggregatableIndices();
        }

        public void rankingEvaluation(RestHighLevelClient client) {

        }

        public void count(RestHighLevelClient client) throws IOException {
            CountRequest countRequest = new CountRequest("blog")
                    .routing("routing")
                    .indicesOptions(IndicesOptions.lenientExpandOpen())
                    .preference("_local");

            countRequest.indices("blog", "author");
            countRequest.query(QueryBuilders.termQuery("user", "kimchy"));
            countRequest.query(QueryBuilders.matchAllQuery());

            client.countAsync(countRequest, RequestOptions.DEFAULT, new ActionListener<CountResponse>() {
                @Override
                public void onResponse(CountResponse countResponse) {
                    long count = countResponse.getCount();
                    RestStatus status = countResponse.status();
                    Boolean terminatedEarly = countResponse.isTerminatedEarly();

                    int totalShards = countResponse.getTotalShards();
                    int skippedShards = countResponse.getSkippedShards();
                    int successfulShards = countResponse.getSuccessfulShards();
                    int failedShards = countResponse.getFailedShards();
                    for (ShardSearchFailure failure : countResponse.getShardFailures()) {
                        // failures should be handled here
                    }
                }
@Override
public void onFailure(Exception e) { } }); } } }
posted @ 2021-02-04 11:17  agasha  阅读(127)  评论(0编辑  收藏  举报