下载安装ES
1.官网下载地址:https://www.elastic.co/downloads/elasticsearch 目前最新本6.0 windows上面直接选择zip压缩包 注意:要求jdk1.8
2.安装
解压缩下载下来的包-进入bin目录-运行elasticsearch.bat文件 http://127.0.0.1:9200/ 访问成功表示elasticsearch安装成功
java连接ES
1.添加依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.6.3</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.6.3</version>
</dependency>
2.java code
@Test
public voidtest1() {
try{
//集群名称可以在配置文件设置
Settings settings =Settings.builder().put("cluster.name","my-application").build();
TransportClient client =newPreBuiltTransportClient(settings)
.addTransportAddress(newInetSocketTransportAddress(InetAddress.getByName("localhost"),9300));
IndexResponse response = client.prepareIndex("item","itemList","830972")
.setSource(jsonBuilder()
.startObject()
.field("productId","830972")
.field("salePrice","254")
.field("productName","254")
.field("sub_title","524")
.field("productImageBig","252")
.field("category_name","2542")
.endObject()
).get();
//查询
GetResponse getResponse = client.prepareGet("item","itemList","830972").get();
String json = getResponse.getSourceAsString();
System.out.println(json);
client.close();
}catch(Exception e) {
e.printStackTrace();
}
}
head可视化插件安装
说明:es 6.0版本不支持命令行直接安装,需要依赖node环境(可自行百度安装)
1.安装grunt
npm install -g grunt --registry=https://registry.npm.taobao.org
2.通过git下载elasticsearch-head源码
yum install –y git
git clone https://github.com/mobz/elasticsearch-head.git
3.修改head源码,默认是localhost,本地机器不用修改。head/Gruntfile.js
connect: {
server: {
options: {
port: 9100,
hostname: '*',
base: '.',
keepalive: true
}
}
}
4.head插件连接地址head/_site/app.js
this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://localhost:9200";
5.修改es配置文件
#增加新的参数,这样head插件可以访问es
http.cors.enabled: true
http.cors.allow-origin: "*"
6.正常其启动elasticsearch
7.进入elasticsearch-head根目录,执行npm install
npm install
8.完成后启动elasticsearch-head,在源码根目录执行
grunt server
访问:http://localhost:9100 就可以访问head插件