ICode9

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

elasticsearch java api

2019-08-11 14:00:37  阅读:89  来源: 互联网

标签:java System client api elasticsearch user println new out



依赖

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>transport</artifactId>
    <version>6.8.1</version>
</dependency>

 

一、连接

// 设置集群名字
Settings settings = Settings.builder().put("cluster.name", "elasticsearch-6.2.3-local").build();
// 获取连接
TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

// 关闭连接
client.close();

 

二、索引

// 创建索引
client.admin().indices().prepareCreate("user").execute().actionGet();
// 删除索引
client.admin().indices().prepareDelete("user").get();

 

三、查询

// 构建查询条件
QueryBuilder match = QueryBuilders.matchQuery("sex", "男");
// 根据查询条件构建查询语句
SearchRequestBuilder search = client.prepareSearch("user")
        .setQuery(match)
        .setTypes("user") // 指定类型,可选
        .setFrom(0 * 100).setSize(100) // 分页,可选
        .addSort("age", SortOrder.DESC); // 排序,可选
// 获取搜索结果
SearchResponse response = search.get();
// 命中的文档
SearchHits hits = response.getHits();
System.out.println("当前索引总数:" + hits.getTotalHits());
System.out.println("查询结果总数:" + hits.getHits().length);

for (SearchHit hit : hits.getHits()) {
    System.out.println("文档元数据 JSON:" + hit.getIndex());
    System.out.println("文档元数据 Map:" + hit.getSourceAsMap());
}


// 单条记录查询
GetResponse documentFields = client.prepareGet("user", "user", "10086").execute().actionGet();
documentFields.getSourceAsMap();

 

四、插入

// 单条插入
Map<String, Object> userMap = new HashMap<>();
userMap.put("age", "18");
client.prepareIndex("user", "user", "10086").setSource(userMap).execute().actionGet();

// 批量插入
BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
for (Map<String, Object> map : list) {
    bulkRequestBuilder.add(yofc.prepareIndex("user", "user", map.get("id").toString()).setSource(map));
}
bulkRequestBuilder.execute().actionGet();

// 批量插入
BulkProcessor bulkProcessor = BulkProcessor.builder(
        client,
        new BulkProcessor.Listener() {
            @Override
            public void beforeBulk(long l, BulkRequest bulkRequest) {
                System.out.println("插入" + bulkRequest.numberOfActions() + "条数据开始");
            }

            @Override
            public void afterBulk(long l, BulkRequest bulkRequest, BulkResponse bulkResponse) {
                System.out.println("插入" + bulkRequest.numberOfActions() + "条数据成功");
            }

            @Override
            public void afterBulk(long l, BulkRequest bulkRequest, Throwable throwable) {
                System.out.println("插入" + bulkRequest.numberOfActions() + "条数据失败");
            }
        })
        .setBulkActions(1000)
        .setBulkSize(new ByteSizeValue(100, ByteSizeUnit.MB))
        .setFlushInterval(TimeValue.timeValueSeconds(5))
        .setConcurrentRequests(1)
        .build();
for (Map<String, Object> map : list) {
    bulkProcessor.add(new IndexRequest("user", "user", map.get("id").toString()).source(map));
}
bulkProcessor.flush();
bulkProcessor.close();

 


https://www.elastic.co/guide/en/elasticsearch/client/java-api/6.8/index.html

https://www.ruanyifeng.com/blog/2017/08/elasticsearch.html



标签:java,System,client,api,elasticsearch,user,println,new,out

专注分享技术,共同学习,共同进步。

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

ICode9版权所有