美文网首页
springboot Elasticsearch Get Ind

springboot Elasticsearch Get Ind

作者: 觉释 | 来源:发表于2020-06-06 09:44 被阅读0次
package com.wzw.springbootelasticsearch.DocumentAPIs;

import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.VersionType;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.Map;


@SpringBootTest
@Slf4j
public class Document_Get_Index {

@Autowired
    RestHighLevelClient restHighLevelClient;


    @Test
    public void testIndex() throws IOException {

        String INDEX_TEST="goods_1";
        getIndex(INDEX_TEST);

    }

    /**
     * Index
     * @param index
     * @throws IOException
     */
    public void getIndex(String index) throws IOException {

        try {
            GetRequest request = new GetRequest(
                    index,
                    "bvrIU2kfSAWKQHycnLZG1w");

            request.fetchSourceContext(FetchSourceContext.DO_NOT_FETCH_SOURCE);

            String[] includes = new String[]{"message", "*Date"};
            String[] excludes = Strings.EMPTY_ARRAY;
            FetchSourceContext fetchSourceContext =
                    new FetchSourceContext(true, includes, excludes);
            request.fetchSourceContext(fetchSourceContext);
            request.routing("routing");

            request.preference("preference");
            request.realtime(false);
            request.storedFields("message");
            request.refresh(true);
            request.version(2);
            request.versionType(VersionType.EXTERNAL);
            GetResponse getResponse = restHighLevelClient.get(request, RequestOptions.DEFAULT);
//            String message = getResponse.getField("message").getValue();

            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 {

            }
            System.out.println("getResponse:"+getResponse);
        } catch (ElasticsearchException exception) {
            if (exception.status() == RestStatus.NOT_FOUND) {

            }
        }




    }
}

相关文章

网友评论

      本文标题:springboot Elasticsearch Get Ind

      本文链接:https://www.haomeiwen.com/subject/vvmgzhtx.html