?全网最详细的大数据ELK文章系列,强烈建议收藏加关注!?
新文章都已经列出历史文章目录,帮助大家回顾前面的知识重点。
目录
Elasticsearch编程
一、环境准备
1、准备IDEA项目结构
2、准备POM依赖
3、创建用于保存职位信息的实体类
4、编写接口和实现类
?5、创建实现类
Elasticsearch编程
要将搜索的功能与前端对接,我们必须要使用Java代码来实现对Elasticsearch的操作。我们要使用一个JobService类来实现之前我们用RESTFul完成的操作
?
一、环境准备
1、准备IDEA项目结构
- 创建elasticsearch_example项目
- 创建包结构如下所示
包 | 说明 | cn.it.elasticsearch.entity | 存放实体类 | cn.it.elasticsearch.service | 存放服务接口 | cn.it.elasticsearch.service.impl | 存放服务接口实现类 |
2、准备POM依赖
<repositories><!-- 代码库 -->
<repository>
<id>aliyun</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<target>1.8</target>
<source>1.8</source>
</configuration>
</plugin>
</plugins>
</build>
3、???????创建用于保存职位信息的实体类
注意:
在id字段上添加一个?@JSONField注解,并配置注解的serialize为false,表示该字段无需转换为JSON,因为它就是文档的唯一ID。
?参考代码:
public class JobDetail {
// 因为此处无需将id序列化为文档中
@JSONField(serialize = false)
private long id; // 唯一标识
private String area; // 职位所在区域
private String exp; // 岗位要求的工作经验
private String edu; // 学历要求
private String salary; // 薪资范围
private String job_type; // 职位类型(全职/兼职)
private String cmp; // 公司名
private String pv; // 浏览量
private String title; // 岗位名称
private String jd; // 职位描述
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
public String getExp() {
return exp;
}
public void setExp(String exp) {
this.exp = exp;
}
public String getEdu() {
return edu;
}
public void setEdu(String edu) {
this.edu = edu;
}
public String getSalary() {
return salary;
}
public void setSalary(String salary) {
this.salary = salary;
}
public String getJob_type() {
return job_type;
}
public void setJob_type(String job_type) {
this.job_type = job_type;
}
public String getCmp() {
return cmp;
}
public void setCmp(String cmp) {
this.cmp = cmp;
}
public String getPv() {
return pv;
}
public void setPv(String pv) {
this.pv = pv;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getJd() {
return jd;
}
public void setJd(String jd) {
this.jd = jd;
}
@Override
public String toString() {
return "JobDetail{" +
"id=" + id +
", area='" + area + '\'' +
", exp='" + exp + '\'' +
", edu='" + edu + '\'' +
", salary='" + salary + '\'' +
", job_type='" + job_type + '\'' +
", cmp='" + cmp + '\'' +
", pv='" + pv + '\'' +
", title='" + title + '\'' +
", jd='" + jd + '\'' +
'}';
}
}
4、???????编写接口和实现类
cn.it.elasticsearch.service包中创建JobFullTextService接口,该接口中定义了职位全文检索相关的Java?API接口。
参考代码:
/**
* 定义JobFullTextService
*/
public interface JobFullTextService {
// 添加一个职位数据
void add(JobDetail jobDetail);
// 根据ID检索指定职位数据
JobDetail findById(long id) throws IOException;
// 修改职位薪资
void update(JobDetail jobDetail) throws IOException;
// 根据ID删除指定位置数据
void deleteById(long id) throws IOException;
// 根据关键字检索数据
List<JobDetail> searchByKeywords(String keywords) throws IOException;
// 分页检索
Map<String, Object> searchByPage(String keywords, int pageNum, int pageSize) throws IOException;
// scroll分页解决深分页问题
Map<String, Object> searchByScrollPage(String keywords, String scrollId, int pageSize) throws IOException;
// 关闭ES连接
void close() throws IOException;
;
}
?5、???????创建实现类
在cn.it.elasticsearch.service.impl包下创建一个实现类:JobFullTextServiceImpl,并实现上面的接口。
参考代码:
public class JobFullTextServiceImpl implements JobFullTextService {
@Override
public void add(JobDetail jobDetail) {
}
@Override
public void update(JobDetail jobDetail) {
}
@Override
public JobDetail findById(long id) {
return null;
}
@Override
public boolean deleteById(long id) {
return false;
}
@Override
public List<JobDetail> searchByKeywords(String keywords) {
return null;
}
@Override
public Map<String, Object> searchByPage(String keywords, int pageNum, int pageSize) {
return null;
}
@Override
public Map<String, Object> searchByScrollPage(String keywords, String scrollId, int pageSize) {
return null;
}
}
|