1
大数据 ELK(十二):Elasticsearch 编程(环境准备)
作者:Lansonli
- 2022 年 10 月 04 日 广东
本文字数:3022 字
阅读完需:约 10 分钟

一、环境准备
1、准备 IDEA 项目结构
创建 elasticsearch_example 项目
创建包结构如下所示
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; }}
复制代码
划线
评论
复制
发布于: 刚刚阅读数: 4
版权声明: 本文为 InfoQ 作者【Lansonli】的原创文章。
原文链接:【http://xie.infoq.cn/article/c1c67ecfd5270c08461bf2c49】。文章转载请联系作者。
Lansonli
关注
微信公众号:三帮大数据 2022.07.12 加入
CSDN大数据领域博客专家,华为云享专家、阿里云专家博主、腾云先锋(TDP)核心成员、51CTO专家博主,全网六万多粉丝,知名互联网公司大数据高级开发工程师










评论