java elasticsearch index
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()); } }
分类:
elasticsearch
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)