一.ES简介
elasticsearch是一个高度可扩展的开源全文搜索和分析引擎,可以快速的、近实时地对大数据进行存储、搜索和分析,用来支撑复杂的数据搜索需求和企业级应用,主要用于海量系统日志的数据查询与分析。
二.ES安装
- 下载地址:https://www.elastic.co/downloads/elasticsearch。
- 下载ES压缩包解压后,进入bin目录,双击elasticsearch.bat即可启动es
- 启动es服务后,访问localhost:9200,浏览器出现如下信息说明启动成功.
{
"name" : "YNFljfo",
"cluster_name" : "my-application",
"cluster_uuid" : "iuLpWK05SJGSqp5evk7Gpg",
"version" : {
"number" : "6.2.4",
"build_hash" : "ccec39f",
"build_date" : "2018-04-12T20:37:28.497551Z",
"build_snapshot" : false,
"lucene_version" : "7.2.1",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
三.head插件的安装
head插件可以很方便的操作es,head插件与es的关系就像navicat与MySQL数据库的关系,不过head插件的界面是通过访问网址浏览的。
- 下载head插件:https://github.com/search?q=elasticsearch-head
- 因为head插件依赖node环境,所以需要安装node.js:https://nodejs.org/en/download/
- 运行head需要借助grunt命令,所以要安装。
在cmd窗口进入node.js安装的根目录,然后执行npm install -g grunt -cli
。 - 安装pathomjs:
在cmd命令窗口进入head插件解压后的根目录,然后执行npm install
- 连接es: 设置跨域访问,在es的安装根目录的config目录下有elasticsearch.yml,在此文件中添加如下内容:
http.cors.enabled: true
http.cors.allow-origin: "*"
将cluster.name: my-application的注释取消
- 运行head:
在cmd命令窗口进入head插件解压后的根目录,然后执行grunt server
,如下图就启动成功。
- 然后访问
localhost:9100
看到如下画面就安装成功
四.Springboot集成ES
- 添加依赖及配置application.properties文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
spring.data.elasticsearch.cluster-name=my-application
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
- 创建实体类及响应体
@Data
@Document(indexName = "person")
@AllArgsConstructor
@NoArgsConstructor
public class Person implements Serializable {
@Id
private String skuId;
private String name;
private String address;
private Integer age;