ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java elasticsearch index

2021-04-16 22:02:30  阅读:159  来源: 互联网

标签:index java client elasticsearch org indices import


es版本6.8.*及以下,7.9.*不适用。

直接贴代码

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;

import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.admin.indices.stats.IndexStats;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsRequest;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * 定义es index格式
 * 
 * @author zhanchaohan
 * @see https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-index.html
 */
public class index {
    private TransportClient client;

    @Before
    public void init() throws FileNotFoundException, IOException {
        ElasticConfig ec = new ElasticConfig();
        client = ec.init();
    }

    @After
    public void destroy() {
        if (client != null) {
            client.close();
        }
    }

    @Test
    public void testIndex() throws IOException {
        // json
        String json = "{" + "\"user\":\"kimchy\"," + "\"postDate\":\"2013-01-30\","
                + "\"message\":\"trying out Elasticsearch\"" + "}";
        // 通过Map
        Map<String, Object> mapJson = new HashMap<String, Object>();
        mapJson.put("user", "kimchy");
        mapJson.put("postDate", new Date());
        mapJson.put("message", "trying out Elasticsearch");
        // Jackson实现
//        ObjectMapper mapper = new ObjectMapper(); // create once, reuse
//        byte[] json = mapper.writeValueAsBytes(yourbeaninstance);

        // 官方给出的对象实现
        XContentBuilder builder = jsonBuilder();
        String builderJson = Strings.toString(builder);
        System.out.println(builderJson);
    }

    private XContentBuilder jsonBuilder() throws IOException {
        XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("properties")
                .startObject("user").field("type", "string").field("index", "not_analyzed").endObject()
                .startObject("postDate").field("type", "date").endObject().startObject("message")
                .field("type", "string").field("index", "not_analyzed").endObject().startObject("address")
                .field("type", "string").endObject().startObject("carPsc").field("type", "string").field("index", "ik")
                .endObject().endObject().endObject();
        return builder;
    }

    /***
     * 获取全部节点
     */
    @Test
    public void discoveryNode() {
        List<DiscoveryNode> dnList = client.listedNodes();
        for (DiscoveryNode discoveryNode : dnList) {
            System.out.println(discoveryNode.getHostName());
        }
    }

    /***
     * 获取索引
     */
    @Test
    public void getIndex() {
        ActionFuture<IndicesStatsResponse> isr = client.admin().indices().stats(new IndicesStatsRequest().all());
        IndicesAdminClient indicesAdminClient = client.admin().indices();
        Map<String, IndexStats> indexStatsMap = isr.actionGet().getIndices();
        Set<String> set = isr.actionGet().getIndices().keySet();

        for (String key : set) {
            System.out.println("索引名称:" + key);
        }
    }

    /***
     * 删除全部索引
     * @throws ExecutionException 
     * @throws InterruptedException 
     */
    @Test
    public void deleteAllIndex() throws InterruptedException, ExecutionException {
        ActionFuture<IndicesStatsResponse> isr = client.admin().indices().stats(new IndicesStatsRequest().all());
        Set<String> set = isr.actionGet().getIndices().keySet();

        for (String key : set) {
            DeleteIndexResponse deleteIndexResponse= client.admin().indices().prepareDelete(key).execute().get();
        
            System.out.println(key+":\t"+deleteIndexResponse.isAcknowledged());
        }

    }

    /***
     * 删除指定索引
     */
    @Test
    public void deleteIndex() {
        ActionFuture<DeleteIndexResponse> dResponse = client.admin().indices()
                .delete(new DeleteIndexRequest("ui_template"));
        DeleteIndexResponse deleteIndexResponse = dResponse.actionGet();

        System.out.println(deleteIndexResponse.isAcknowledged());
    }
}

 

标签:index,java,client,elasticsearch,org,indices,import
来源: https://www.cnblogs.com/zhanchaohan/p/14668868.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有