Loading

Elastic Stack:es JavaApi实现索引管理

一.创建索引

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
@SpringBootTest(classes = SearchApplication.class)
@RunWith(SpringRunner.class)
public class TestIndex {
    @Qualifier("restHighLevelClient")
    @Autowired
    RestHighLevelClient client;
 
    @Test
    public void test1() throws IOException, InterruptedException {
        CreateIndexRequest request = new CreateIndexRequest("my_index");
        //设置参数
        request.settings(Settings.builder().put("number_of_shards","1").put("number_of_replicas","1").build());
        //1.指定mapping映射
        /*Map<String, Object> field1 = new HashMap<>();
        field1.put("type","text");
        field1.put("analyzer","standard");
        Map<String, Object> field2 = new HashMap<>();
        field2.put("type","text");
        Map<String, Object> properties =new HashMap<>();
        properties.put("field1",field1);
        properties.put("field2",field2);
        Map<String,Object> mapping = new HashMap<>();
        mapping.put("properties",properties);
        request.mapping(mapping);*/
        //2.指定mapping映射
        XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.startObject();
        {
            builder.startObject("properties");
            {
                builder.startObject("field1");
                {
                    builder.field("type","text");
                }
                builder.endObject();
                builder.startObject("field2");
                {
                    builder.field("type","text");
                }
                builder.endObject();
            }
            builder.endObject();
        }
        builder.endObject();
        request.mapping(builder);
        //设置别名
        request.alias(new Alias("prod_index"));
        //可选参数
        //超时时间
        request.setTimeout(TimeValue.timeValueSeconds(5));
        //主节点超时时间
        request.setMasterTimeout(TimeValue.timeValueSeconds(5));
        //设置创建索引api返回相应之前等待活动分片的数量
        request.waitForActiveShards(ActiveShardCount.from(1));
 
        //执行
        //CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
 
        //异步
        ActionListener<CreateIndexResponse> listener = new ActionListener<>() {
 
            @Override
            public void onResponse(CreateIndexResponse createIndexResponse) {
                System.out.println(createIndexResponse.isAcknowledged());
                System.out.println(createIndexResponse.isShardsAcknowledged());
            }
 
            @Override
            public void onFailure(Exception e) {
                e.printStackTrace();
            }
        };
        client.indices().createAsync(request,RequestOptions.DEFAULT,listener);
        Thread.sleep(5000);
    }
}

 二.删除索引

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Test
public void testDelete() throws InterruptedException {
    DeleteIndexRequest indexRequest = new DeleteIndexRequest("my_index");
    /*AcknowledgedResponse response = client.indices().delete(indexRequest,RequestOptions.DEFAULT);
    System.out.println(response.isAcknowledged());*/
    //异步
    ActionListener<AcknowledgedResponse> responseActionListener = new ActionListener<>() {
        @Override
        public void onResponse(AcknowledgedResponse response) {
            System.out.println(response.isAcknowledged());
        }
 
        @Override
        public void onFailure(Exception e) {
            e.printStackTrace();
        }
    };
    client.indices().deleteAsync(indexRequest,RequestOptions.DEFAULT,responseActionListener);
    Thread.sleep(5000);
}

 三.查看索引是否存在

1
2
3
4
5
6
7
8
9
10
11
@Test
public void testExist() throws IOException {
    GetIndexRequest request = new GetIndexRequest("my_index");
    //从主节点返回本地索引信息状态
    request.local(false);
    //可读性
    request.humanReadable(true);
    //是否返回每个索引的所有默认设置
    request.includeDefaults(false);
    System.out.println(client.indices().exists(request, RequestOptions.DEFAULT));
}

 四.关闭索引

关闭索引后,该索引就不能添加文档了,需要重新开启索引。

1
2
3
4
5
6
@Test
public void testClose() throws IOException {
    CloseIndexRequest request = new CloseIndexRequest("my_index");
    CloseIndexResponse close = client.indices().close(request, RequestOptions.DEFAULT);
    System.out.println(close.isAcknowledged());
}

 五.打开索引

1
2
3
4
5
6
@Test
public void testOpen() throws IOException {
    OpenIndexRequest request = new OpenIndexRequest("my_index");
    OpenIndexResponse open = client.indices().open(request, RequestOptions.DEFAULT);
    System.out.println(open.isAcknowledged());
}
posted @   秋风飒飒吹  阅读(411)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示