使用本文档请首先需要有使用ElasticSearch的知识,本文档建立在有使用过的基础之上
为什么不使用内部:spring-boot-starter-data-elasticsearch
想必能找到这里来的朋友也知道,spring-boot-starter-data-elasticsearch 集成的版本都比较低,最新的快照版刚刚升级zhic支持到5.x,但是快照版什么的压根不敢用,而且还是个 Springboot 2.0,哥们现在还一直在用1.5.10好吧,我直接升2.0做啥嘞。
好了,不再瞎说了,直接开始上代码了:
全部是代码,GitHub地址:https://github.com/itliuxing/springboot-elasticsearch
使用前提:Springboot 1.5.10 ,Elasticsearch 5.1.1
pom.xml
<?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.wisestar</groupId>
<artifactId>wisestar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>elasticsearch</name>
<description>爬虫数据自动封装至elasticsearch数据库,自动创建索引</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.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>
<!-- elasticsearch版本控制 ,我这里使用的是5.1.1 -->
<log4j-api.version>2.8.2</log4j-api.version>
<log4j-core.version>2.8.2</log4j-core.version>
<elasticsearch.version>5.1.1</elasticsearch.version>
<transport.version>5.1.1</transport.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring boot elasticsearch开发 支持
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- elasticsearch 5.x 依赖 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j-api.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j-core.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${transport.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我们新建一个自定义配置文件:elasticsearch.properties ,路径在 resources
#elasticsearch config
# mult node conf , mult is , split
es.hosts=127.0.0.1
# cluster name
cluster.name=wisestar
# elasticsearch index,type
es.main.index=wisestar
es.main.type=material
application.yml 里面自己peih配好一个端口号
##Tomcat端口号
server:
port: 8008
ElasticSearchConfig.java 自定义参数配置
package com.wisestar.elastic.conf;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
/**
* @Class ElasticSearchConfig.java
* @Author 作者姓名:刘兴
* @Version 1.0
* @Date 创建时间:2018年8月9日 下午9:16:02
* @Copyright Copyright by 刘兴
* @Direction 类说明 这个主要是封装生成:TransportClient 这个是elasticsearch的客户端调用接口
*/
@Configuration
@PropertySource("classpath:elasticsearch.properties")
public class ElasticSearchConfig {
@Value("${es.hosts}")
private String esHosts;
@Value("${cluster.name}")
private String clusterName;
@Bean
public TransportClient geTransportClient() throws UnknownHostException{
Settings settings=Settings.builder().put("cluster.name",clusterName).build();
TransportClient client=new PreBuiltTransportClient(settings);
String[] hosts=esHosts.split(",");
for(String host:hosts){
// 如果我们配置多个host,自动识别配置多个节点
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host),9300));
}
return client;
}
}
ElasticSearchProperties.java
package com.wisestar.elastic.conf;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* @Class ElasticSearchProperties.java
* @Author 作者姓名:刘兴
* @Version 1.0
* @Date 创建时间:2018年8月10日 上午10:04:34
* @Copyright Copyright by 刘兴
* @Direction 类说明 多个索引建立等信息到时候全部读取配置文件
*/
@Component
@Configuration
@PropertySource("classpath:elasticsearch.properties")
public class ElasticSearchProperties {
@Value("${es.main.index}")
private String esMainIndex; // 主索引【可能我们会建立多张索引库】
@Value("${es.main.type}")
private String esMainType; // 主索引的type
public String getEsMainIndex() {
return esMainIndex;
}
public void setEsMainIndex(String esMainIndex) {
this.esMainIndex = esMainIndex;
}
public String getEsMainType() {
return esMainType;
}
public void setEsMainType(String esMainType) {
this.esMainType = esMainType;
}
}
MaterialQuotedService.java
package com.wisestar.elastic.service;
import java.util.Map;
/***
*
* @Class MaterialQuotedService.java
* @Author 作者姓名:Liuxing
* @Version 1.0
* @Date 创建时间:2018年8月9日 下午9:42:34
* @Copyright Copyright by Liuxing
* @Direction 类说明 提供调用elasticsearch的接口服务类
*/
public interface MaterialQuotedService {
public String getIndexTypeById( String indexname ,String type , String id);
public String saveIndex( String indexname ,String type , String json ) ;
Map<String,Object> getInfoByIndex( String indexname ,String type , String id ) ;
boolean deleteIndexById( String indexname ,String type , String id ) ;
boolean UpdateIndexById( String indexname ,String type , String id , String doc ) throws Exception ;
}
MaterialQuotedServiceImpl.java
package com.wisestar.elastic.service.impl;
import java.util.Map;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wisestar.elastic.service.MaterialQuotedService;
/**
* @Class MaterialQuotedServiceImpl.java
* @Author 作者姓名:刘兴
* @Version 1.0
* @Date 创建时间:2018年8月9日 下午9:45:13
* @Copyright Copyright by 刘兴
* @Direction 类说明 实际操作,这里只做了简单的操作,后面比如说复杂查询还需要封装
*/
@Service
public class MaterialQuotedServiceImpl implements MaterialQuotedService {
@Autowired
private TransportClient client;
/****
* 查询返回String
*/
public String getIndexTypeById( String indexname ,String type , String id){
GetResponse response=client.prepareGet( indexname, type , id ).get();
return response.getSourceAsString();
}
/***
* 保存文档
*/
public String saveIndex( String indexname ,String type , String json ) {
try {
IndexResponse response=client.prepareIndex( indexname, type ).setSource(json).get();
//创建成功 反会的状态码是201
if(response.status().getStatus()==201){
return response.getId();
}
} catch (Exception e) {
System.out.println( "不符合索引的数据存储,数据源为:" + json );
}
return null ;
}
/****
* 查询返回Map
*/
public Map<String,Object> getInfoByIndex( String indexname ,String type , String id ) {
GetResponse response=client.prepareGet( indexname , type , id ).get();
return response.getSource();
}
/***
* 删除文档
*/
public boolean deleteIndexById( String indexname ,String type , String id ) {
DeleteResponse response=client.prepareDelete( indexname , type , id ).get();
if(response.status().getStatus()==200){
return true ;
}
return false ;
}
/***
* 跟新文档
*/
public boolean UpdateIndexById( String indexname ,String type , String id , String doc ) throws Exception{
UpdateRequest updateRequest = new UpdateRequest(indexname, type, id).doc(doc);
UpdateResponse response = client.update(updateRequest).get();
if (response.status().getStatus() == 200) {
return true;
}
return false;
}
}
MaterialController.java
package com.wisestar.elastic.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wisestar.elastic.conf.ElasticSearchProperties;
import com.wisestar.elastic.entity.CompanyQuotedPrice;
import com.wisestar.elastic.service.MaterialQuotedService;
/**
* @Class MaterialController.java
* @Author 作者姓名:刘兴
* @Version 1.0
* @Date 创建时间:2018年8月9日 下午4:57:10
* @Copyright Copyright by 刘兴
* @Direction 类说明 一个简单的使用版本,后期的复杂查询要建立在数据量大的基础上
*/
@RestController
public class MaterialController {
@Autowired
private ElasticSearchProperties conf ; // 配置文件,引用配置参数使用
@Autowired
private MaterialQuotedService materialRepository; // 调用我们的接口与elasticsearch通讯
// 本接口的数据来源是爬虫爬出来的数据写入搜索引擎
//http://localhost:8008/save
@PostMapping("save")
public String save(CompanyQuotedPrice material ){
try {
ObjectMapper mapper =new ObjectMapper();
String json =mapper.writeValueAsString( material );
String result = materialRepository.saveIndex(conf.getEsMainIndex(), conf.getEsMainType(), json);
if( result == null ) {
System.out.println( "存储失败 ......" );
}
return "success";
} catch (JsonProcessingException e) {
return "false";
}
}
//http://localhost:8008/delete?id=AWUfPMRENSdSGyKuqpga
@GetMapping("delete")
public String delete(String id){
boolean result = materialRepository.deleteIndexById( conf.getEsMainIndex(), conf.getEsMainType(), id);
return result + "" ;
}
//http://localhost:8008/update?id=1525417362754&name=修改&description=修改
@GetMapping("update")
public String update( CompanyQuotedPrice material ){
try {
ObjectMapper mapper =new ObjectMapper();
String json =mapper.writeValueAsString( material );
materialRepository.UpdateIndexById( conf.getEsMainIndex(), conf.getEsMainType(), material.getId() , json);
return "success";
} catch ( Exception e) {
return "false";
}
}
//http://localhost:8008/getOne?id=1525417362754
@GetMapping("getOne")
public String getOne(String id){
return materialRepository.getIndexTypeById(conf.getEsMainIndex(), conf.getEsMainType(), id) ;
}
}
这时候会发现我们缺少了一个最重要的问题,载体 一份数据到elasticsearch可以是json,但是可能我们需要对数据的处理,当然我这里是定位成一个爬虫爬取数据后存储的过程,可以不需要,但是还是扩展出来比较好,这个对象就要跟我们创建的索引对应起来的。
CompanyQuotedPrice.java
package com.wisestar.elastic.entity;
import java.io.Serializable;
/**
* @Class CompanyQuotedPrice.java
* @Author 作者姓名:刘兴
* @Version 1.0
* @Date 创建时间:2018年8月1日 下午4:42:53
* @Copyright Copyright by 刘兴
* @Direction 类说明 材料数据承载对象
*/
public class CompanyQuotedPrice implements Serializable {
private static final long serialVersionUID = 1L;
private String id; // 材料索引ID
private String bswName; // 材料
private String bswSpecify; // 材料
private String bswBrand; // 材料
private String bswUnit; // 材料
private String bswIncluPrice; // 材料
private String bswTariffPrice; // 材料
private String bswTaxRate; // 材料
private String bswQuoTime; // 材料
private String bswQuoArea; // 材料
private String bswSupplier; // 材料
private String bswContact; // 材料
private String bswContactPhone; // 材料
private String bswAddr; // 材料
private String bswDuty; // 材料
省略 get 、set
}
再加送一份索引创建的脚本吧
#建立索引并创建mapping
PUT /wisestar
{
“settings” : {
“index”: {
“number_of_shards”: “5”,
“number_of_replicas”: “1”
}
},
“mappings” : {
“material” : {
“properties” : {
“id” : {
“type” : “keyword”
},
“bswName” : {
“type” : “text”,
“analyzer”: “ik_max_word”,
“search_analyzer”: “ik_max_word”
},
“bswSpecify” : {
“type” : “text”,
“analyzer”: “ik_max_word”
},
“bswBrand” : {
“type” : “text”,
“analyzer”: “ik_max_word”
},
“bswUnit” : {
“type” : “keyword”
},
“bswIncluPrice” : {
“type” : “double”
},
“bswTariffPrice” : {
“type” : “double”
},
“bswTaxRate” : {
“type” : “keyword”
},
“bswQuoTime” : {
“type”: “date”,
“format” : “yyyy/MM/dd HH:mm:ss||yyyy/MM/dd”
},
“bswQuoArea” : {
“type” : “keyword”
},
“bswSupplier” : {
“type” : “text”,
“analyzer”: “ik_max_word”,
“search_analyzer”: “ik_max_word”
},
“bswContact” : {
“type” : “keyword”
},
“bswContactPhone” : {
“type” : “keyword”
},
“bswAddr” : {
“type” : “keyword”
},
“bswDuty” : {
“type” : “keyword”
}
}
}
}
}
全部是代码,我就不多写了,GitHub地址:https://github.com/itliuxing/springboot-elasticsearch
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/160949.html