1.安装elesticsearch
1.下载elesticsearch
下载地址:https://www.elastic.co/downloads/elasticsearch,我这里下载的是6.4.2版本的
下载之后解压到任意盘符
2.下载node.js(需要用到npm)
下载地址:https://nodejs.org/en/download/,我这里的版本是10.13.0
3.下载head插件
下载地址:https://github.com/mobz/elasticsearch-head,下载zip格式文件
4.安装grunt环境
安装好node之后,cmd执行 npm install -g grunt-cli
5.修改elesticsearch配置文件
解压elesticsearch到任意盘符之后,编辑config文件夹下的elasticsearch.yml文件
在最下面添加
node.name: node-1 #节点名称
network.host: 0.0.0.0
http.port: 9200 #端口号
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
修改完成,双击bin目录下的elasticsearch.bat 启动
访问localhost:9200地址如果出现以下提示,表示启动成功
{
"name": "node-1",
"cluster_name": "elasticsearch",
"cluster_uuid": "5m9ZdhaRSRmOm8AsJyUcEg",
"version": {
"number": "6.4.2",
"build_flavor": "default",
"build_type": "zip",
"build_hash": "04711c2",
"build_date": "2018-09-26T13:34:09.098244Z",
"build_snapshot": false,
"lucene_version": "7.4.0",
"minimum_wire_compatibility_version": "5.6.0",
"minimum_index_compatibility_version": "5.0.0"
},
"tagline": "You Know, for Search"
}
6.配置head插件
解压elasticsearch-head-master文件到elesticsearch目录下与bin目录同级,进入elasticsearch-head-master目录
修改Gruntfile.js文件,添加hostname:'*'
connect: {
server: {
options: {
hostname:'*',
port: 9100,
base: '.',
keepalive: true
}
}
}
在elasticsearch-head-master目录下 cmd执行 npm install 运行完成之后 cmd 执行 npm run start 启动插件
启动之后 访问 http://localhost:9100/ 显示集群健康值为绿色 即为成功
7.创建索引
选择索引tab页 点击创建索引按钮 输入索引名称
有类似如下提示,创建成功 {"acknowledged":true,"shards_acknowledged":true,"index":"test"}
点开复合查询,添加一条数据,elesticsearch会根据数据结构自动扩充字段
至此elesticsearch已经完成安装
2.使用maven工程连接elesticsearch
在pom.xml中添加依赖
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>5.6.0</version> </dependency>
在我的测试中使用5.6.0版本不会有报错
main方法中执行
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY) .addTransportAddresses(new InetSocketTransportAddress(InetAddress.getByName("你的ip"),9300)); GetResponse response = client.prepareGet("test", "type1", "1").execute().actionGet(); System.out.println(response); client.close();