Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of common operations in Elasticsearch

2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

创建Maven管理的Java项目

在pom.xml中添加依赖:

6.1.1 org.elasticsearch.client transport ${es.version}

然后创建一个单元测试类ESApp:

private TransportClient client; @Before public void setUp() throws Exception { Settings settings = Settings.builder() .put("cluster.name", "mycluster") .put("client.transport.sniff", "true")//增加自动嗅探配置 .build(); client = new PreBuiltTransportClient(settings); client.addTransportAddress(new TransportAddress(InetAddress.getByName("10.8.24.94"), 9300)); System.out.println(client.toString()); }

运行后报错

java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonFactory com.fasterxml.jackson.core jackson-core 2.9.3 com.fasterxml.jackson.core jackson-databind 2.9.3 com.fasterxml.jackson.core jackson-annotations 2.9.3

运行后成功拿到ES的client:

创建一个Index@Test public void createIndex() { client.admin().indices().prepareCreate(INDEX).get(); System.out.println("创建Index成功"); }删除一个Index@Test public void deleteIndex() { client.admin().indices().prepareDelete(INDEX).get(); System.out.println("删除Index成功"); }放入数据的三种方式//不推荐使用,太繁琐拼json格式 @Test public void createDoc() { String json = "{\"name\":\"若泽数据\"}"; IndexResponse response = client.prepareIndex(INDEX, TYPE, "100") .setSource(json, XContentType.JSON) .get(); } //推荐使用 @Test public void test01() throws Exception { Map json = new HashMap(); json.put("name", "ruozedata"); json.put("message", "trying out Elasticsearch"); IndexResponse response = client.prepareIndex(INDEX, TYPE, "101").setSource(json).get(); System.out.println(response.getVersion()); }//推荐使用 @Test public void test02() throws Exception { XContentBuilder builder = jsonBuilder() .startObject() .field("user", "ruoze") .field("postDate", new Date()) .field("message", "trying out Elasticsearch") .endObject(); IndexResponse response = client.prepareIndex(INDEX, TYPE, "102").setSource(builder).get(); System.out.println(response.getVersion()); }拿到一条数据@Test public void getDoc() { GetResponse response = client.prepareGet(INDEX, TYPE, "100").get(); System.out.println(response.getSourceAsString()); }拿到多条数据@Test public void getDocsByIds() { MultiGetResponse responses = client.prepareMultiGet() .add(INDEX, TYPE,"100") .add(INDEX, TYPE, "101", "102", "1000") .get(); for (MultiGetItemResponse response : responses) { GetResponse res = response.getResponse(); if (res.isExists()) { System.out.println(res); } else { System.out.println("没有这条数据"); } } }

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report