赞
踩
背景:科室要完成一个图文系统,其中用到elasticsearch搜索引擎,作者开始对elasticsearch进行学习,初步在eclipse上搭建了一个springboot与eclipse的整合,完成了对数据的简单增删改查,并在swagger上进行测试成功。
jdk安装:版本1.8.0_40
jdk环境配置:
classpath: .;C:\Program Files\Java\jdk1.8.0_40\lib;C:\Program Files\Java\jdk1.8.0_40\lib\tools.jar
path: ;C:\Program Files\Java\jdk1.8.0_40\bin;C:\Program Files\Java\jdk1.8.0_40\jre\bin
配置完成,可以使用dos命令检查jdk是否安装成功:javac
eclipse安装:版本eclipse-jee-mars-2-win32-x86_64
1.初次需要配置eclipse中的jre版本,window->Preference->Java->Installed JREs->add
为jre1.8.0_40版本
2.使用springboot框架需要用到maven,所以还需要安装和配置maven
版本为apache-maven-3.6.0
,需要进行环境配置
path:;E:\apache-maven-3.6.0-bin\apache-maven-3.6.0\bin\;
致此完成maven的安装,可以通过dos命令检查maven是否安装成功:mvn
3.配置eclipse的Maven环境
打开window->preferences->Maven->Installations
,右侧点击add
installstion type:选择external maven的安装目录
设置maven的安装目录,然后finish
配置settings.xml:
因为本项目是在内网下运行的,还需要安装本地仓库:
在 <settings> </settings>
中添加本地仓库:
<localRepository>E:\MvnRepository</localRepository>
获取公司的镜像仓库:
<mirrors>
<mirror>
<id>greeMavenMirror</id>
<mirrorOf>*</mirrorOf>
<name>gree maven mirror</name>
<url>http://mirror.gree.com/maven</url>
</mirrors>
打开window->preferences->maven->user settings
加载修改好的settings文件,并更新
安装Mysql和前端可视化工具 navicat
elasticsearch安装:版本5.2.0
下载安装包并解压
打开dos窗口,(在开始菜单栏输入cmd)
以我本机为例:
下载解压到E:\elasticsearch\elasticsearch-5.2.0
在dos窗口输入如下命令进入E盘:
C:\User\180440>e:
进入E盘bin目录:
E:\>cd E:\elasticsearch\elasticsearch-5.2.0\bin
运行elasticsearch.bat文件
E:\>cd E:\elasticsearch\elasticsearch-5.2.0\bin>elasticsearch.bat
elasticsearch启动
测试访问http://localhost:9200/
如下结果表明elasticsearch安装成功
{
"name" : "9KresSP",
"cluster_name" :elasticsearch",
"cluster_uuid":"PN-82qmsTQ6_LK2hGVjJHw",
"version" : {
"number" : "5.2.0",
"build_hash" : "24e05b9",
"build_timestamp" : "2017-01-24T19:52:800Z",
"build_snapshot" : false,
"lucene_version" : "6.4.0"
},
"tagline" : "You Know, for Search"
}
以后启动elasticsearch只要去bin目录下双击elasticsearch.bat即可启动elasticsearch
致此,环境准备完成
创建一个maven项目,再根据springBoot的源码目录进行文件的增删操作
配置pom.xml文件
注意:springframework.boot版本需要为2.0.2.RELEASE
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>ElasticSearch</name> <description>ElasticSearch project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> <version>2.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--为了进行swagger测试,加入依赖--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swaggerr-ui</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3.配置application.properties文件
# elasticsearch集群名称,默认的是elasticsearch
spring.data.elasticsearch.cluster-name=my-application
#节点的地址 注意api模式下端口号是9300,千万不要写成9200 192.168.174.1为本机ip地址
spring.data.elasticsearch.cluster-nodes=192.168.174.1:9300
#是否开启本地存储
spring.data.elasticsearch.repositories.enable=true
**注意:**此处将elasticsearch集群名称名称设置为my-application
,则要保证elasticsearch安装目录下conf文件中的elasticsearch.yml文件配置为下:
注意安装完成之后的yml文件下列属性是默认注释掉的,要记得打开。
cluster.name:my-application
network.host:0.0.0.0
network.host属性设置为0.0.0.0是为了让外网也可以访问。
4.配置实体类,这里与mysql创建实体类不一样,需要和elasticsearch中的索引进行对应
package com.elasticsearch.entity; import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Field; @Document(indexName = "company",type = "employee", shards = 1,replicas = 0, refreshInterval = "-1") public class Employee { @Id private String id; @Field private String firstName; @Field private String lastName; @Field private Integer age = 0; @Field private String about; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAbout() { return about; } public void setAbout(String about) { this.about = about; } }
5.实体类对应的dao接口:
注意继承的ElasticsearchRepository接口中的方法
package com.elasticsearch.dao; import com.elasticsearch.entity.Employee; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Component; @Component public interface EmployeeRepository extends ElasticsearchRepository<Employee,String>{ /** * 查询雇员信息 * @param id * @return */ Employee queryEmployeeById(String id); }
6.实体对应的控制类:
package com.elasticsearch.controller; import com.elasticsearch.dao.EmployeeRepository; import com.elasticsearch.entity.Employee; import com.google.gson.Gson; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("es") @Api(tags="elasticsearch",value="elasticsearch测试") public class EmployeeController { @Autowired private EmployeeRepository employeeRepository; /** * 添加 * @return */ @RequestMapping("add") public String add() { Employee employee = new Employee(); employee.setId("1"); employee.setFirstName("xuxu"); employee.setLastName("zh"); employee.setAge(26); employee.setAbout("i am in peking"); employeeRepository.save(employee); System.err.println("add a obj"); return "success"; } /** * 删除 * @return */ @RequestMapping("delete") public String delete() { Employee employee = employeeRepository.queryEmployeeById("1"); employeeRepository.delete(employee); return "success"; } /** * 局部更新 * @return */ @RequestMapping("update") public String update() { Employee employee = employeeRepository.queryEmployeeById("1"); employee.setFirstName("哈哈"); employeeRepository.save(employee); System.err.println("update a obj"); return "success"; } /** * 查询 * @return */ @RequestMapping("query") public Employee query() { Employee accountInfo = employeeRepository.queryEmployeeById("1"); System.err.println(new Gson().toJson(accountInfo)); return accountInfo; } }
7.Application.java启动类
8.运动Application.java文件,访问http://localhost:8090/swagger-ui.html#!/elasticsearch 进行测试
9.最后注意:
1.ES中API的端口号是9300而不是9200。
2.ES系统中Elasticsearch.yml配置文件中要加入network.host: 0.0.0.0,否则外网地址访问不了。
3.最新的资料一定要去官网上面查看,博客上面好多都是过时的。官网地址:https://www.elastic.co
4.注意JDK、ES、Springboot三者之间的版本,很多时候错误都是版本冲突引起的。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。