什么是ElasticSearch
Elasticsearch(ES) 是一个基于Lucene构建的开源、分布式、RESTful接口全文搜索引擎。ElasticSearch还是一个分布式文档数据库,其中每个字段均被索引且可被搜索,它能够扩展至数以百计的服务器存储以及处理PB级的数据。他可以在很短的时间内存储、搜索和分析大量的数据。
demo地址 : https://github.com/BKTiger/elasticsearch-demo.git
java与ES整合配置
- 引入依赖
本项目使用的Springboot版本为2.0.7.RELEASE,注意必须指定elasticsearch版本,否则依赖会报错。
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<elasticsearch.version>7.1.0</elasticsearch.version> <!-- 这里须指定ElasticSearch版本-->
</properties>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${
elasticsearch.version}</version>
</dependency>
- 引入配置
2.1 简版配置
2.2 终板配置@Configuration public class ElasticSearchRestConfig { @Bean public RestHighLevelClient restHighLevelClient(){ RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("localhost", 9200, "http"))); return client; } }
@Configuration public class ElasticSearchRestConfig { @Value("${data.elasticsearch.url}") private String[] ipAddress; private static final int ADDRESS_LENGTH = 2; private static final String HTTP_SCHEME = "http"; @Bean public RestClientBuilder restClientBuilder() { HttpHost[] hosts = Arrays.stream(ipAddress) .map(this::makeHttpHost) .filter(Objects::nonNull) .toArray(HttpHost[]::new); return RestClient.builder(hosts); } @Bean @Primary public RestHighLevelClient highLevelClient(@Autowired RestClientBuilder restClientBuilder) { //TODO 此处可以进行其它操作 return new RestHighLevelClient(restClientBuilder); } private HttpHost makeHttpHost(String s) { String[] address = s.split(":"); if (address.length == ADDRESS_LENGTH) { String ip = address[0]; int port = Integer.parseInt(address[1]); return new HttpHost(ip, port, HTTP_SCHEME); } else { return null; } } }
索引的构建
- 索引的创建
创建代码参考demo的test方法中的 com.zhangtai.demo.DemoBootApplicationTests#createIndex,该方法中引入了ik分词器和dynamic-synonym同义词插件,下文中有使用和配置的方法,需要先加入ik分词器和dynamic-synonym同义词插件后才可使用该方法创建索引,如果不使用可以不进行索引的人工创建,es会自行创建,分词器使用默认分词器,每个汉字为一个分词。 - 索引的删除
// DELETE http://127.0.0.1:9200/goods
private void deleteAll(String index){
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(index);
try {
restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
}
- 判断索引是否存在
public void indexExist(){
// goods索引是否存在
GetIndexRequest goods = new GetIndexRequest("goods");
try {
boolean exists = restHighLevelClient.indices().exists(goods, RequestOptions.DEFAULT);
System.out.println("goods索引存在与否:"+exists);
} catch (IOException e) {
e.printStackTrace();
}
}
数据操作
-
新增数据
1.1 实体类@Data public class Goods { private String id; private String name; private BigDecimal price; private Long skuId; private Long spuId; private Long colorId; private String colorName; public Goods() { } public Goods(String id, String name, BigDecimal price, Long skuId, Long spuId, Long colorId, String colorName) { this.id = id; this.name = name; this.price = price; this.skuId = skuId; this.spuId = spuId; this.colorId = colorId; this.colorName = colorName; } public Goods(String name, BigDecimal price, Long skuId, Long spuId, Long colorId, String colorName) { this.name = name; this.price = price; this.skuId = skuId; this.spuId = spuId; this.colorId = colorId; this.colorName = colorName; } }
2.2 新增测试数据
private List<Map<String,Object>> getDate(){ List<Map<String,Object>> result = new ArrayList<>(); Goods goods = new Goods("00001","袜子",new BigDecimal(21.3D),1L,1L,1L,"红色"); String s = JSON.toJSONString(goods); Map map = JSON.parseObject(s, Map.class); result.add(map); goods = new Goods("00002","袜子",new BigDecimal