JDK1.8环境下,maven仓库依赖
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.9.0</version>
</dependency>
Elasticsearch两种操作模式可以使用
该应用程序可在Elasticsearch集群中扮演更加主动或更加被动的角色。在更加主动的情况下(称为Node Client),应用程序实例将从集群接收请求,确定哪个节点应处理该请求,就像正常节点所做的一样。(应用程序甚至可以托管索引和处理请求。)另一种模式称为Transport Client,它将所有请求都转发到另一个Elasticsearch节点,由后者来确定最终目标。
获取Transport Client
(1)ElasticSearch服务默认端口9300。
(2)Web管理平台端口9200。
(3) 插件使用的端口 9100
@Test
public void getClient() throws UnknownHostException {
Settings settings = Settings.builder().put("", "my-application").build();
//获取客户端对象
PreBuiltTransportClient client = new PreBuiltTransportClient(settings);
//9100 插件端口 9200web端口 9300 客户端访问端口
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("testnote01"),9300));
System.out.println(client.toString());
}
结果
org.elasticsearch.transport.client.PreBuiltTransportClient@1440c311
创建索引和删除索引
package com.zyd;
import static org.junit.Assert.assertTrue;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.Transport;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Before;
import org.junit.Test;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* Unit test for simple App.
*/
public class AppTest {
TransportClient client;
@SuppressWarnings("unchecked")
@Before
public void getClient() throws UnknownHostException {
Settings settings = Settings.builder().put("", "my-application").build();
//获取客户端对象
client = new PreBuiltTransportClient(settings);
//9100 插件端口 9200web端口 9300 客户端访问端口
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("testnote01"),9300));
System.out.println(client.toString());
}
//创建索引
@Test
public void createIndex(){
//创建索引
client.admin().indices().prepareCreate("blog").get();
//关闭资源
client.close();
}
/**
浏览器访问 9100端口 显示信息
*/
//删除索引
@Test
public void deleteIndex(){
//删除索引
client.admin().indices().prepareDelete("blog").get();
//关闭资源
client.close();
}
/**
浏览器访问 9100端口 信息被删除
*/
}
新建文档(源数据JSON串)
当直接在ElasticSearch建立文档对象时,如果索引不存在,默认会自动创建,映射采用默认方式
//创建文档以json形式
@Test
public void createIndexByJson(){
//1. 文档准备
String json = "{" + "\"id\":\"1\"," + "\"title\":\"基于Lucene的搜索服务器\","
+ "\"content\":\"它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口\"" + "}";
//创建
IndexResponse indexResponse = client.prepareIndex("blog", "article", "1").setSource(json).execute().actionGet();
//打印返回值
System.out.println("索引:"+indexResponse.getIndex());
System.out.println("类型:"+indexResponse.getType());
System.out.println("id:"+indexResponse.getId());
System.out.println("版本号:"+indexResponse.getVersion());
System.out.println("结果:"+indexResponse.getResult());
client.close();
}
结果
org.elasticsearch.transport.client.PreBuiltTransportClient@1440c311
索引:blog
类型:article
id:1
版本号:1
结果:CREATED
通过插件页面的数据浏览可以看到
创建文档以hashmap
//创建文档以hashmap
@Test
public void createIndexByMap(){
HashMap<String, Object> map = new HashMap<>();
map.put("id","2");
map.put("title","大数据");
map.put("name","zyd");
IndexResponse reponse = client.prepareIndex("blog", "article", "2").setSource(map).execute().actionGet();
//打印返回值
System.out.println("索引:"+reponse.getIndex());
System.out.println("类型:"+reponse.getType());
System.out.println("id:"+reponse.getId());
System.out.println("版本号:"+reponse.getVersion());
System.out.println("结果:"+reponse.getResult());
client.close();
}
结果
org.elasticsearch.transport.client.PreBuiltTransportClient@783ec989
索引:blog
类型:article
id:2
版本号:1
结果:CREATED
通过插件页面的数据浏览可以看到
创建文档以builder方式
//创建文档以builder方式
@Test
public void createIndexByBuilder() throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject()
.field("id", "5").field("title", "云计算")
.field("content", "未来").endObject();
IndexResponse response = client.prepareIndex("blog", "article", "3").setSource(builder).execute().actionGet();
//打印返回值
System.out.println("索引:"+response.getIndex());
System.out.println("类型:"+response.getType());
System.out.println("id:"+response.getId());
System.out.println("版本号:"+response.getVersion());
System.out.println("结果:"+response.getResult());
client.close();
}
通过插件页面的数据浏览可以看到
















