《第2章 Java关键特性》节略部分
在一次实际的产品开发中,由于业务需求的缘故,需要使用Elasticsearch搜索引擎。搜索引擎是通过索引和文档检索数据的,索引类似于MySQL的数据库,而文档类似于MySQL的表。要想使用搜索引擎,就必须事先创建索引和文档。
有两种解决方案可以实现。
第一种方案是把创建索引和文档的语句直接集成在代码里,每次启动时都检查相应的索引、文档是否存在,不存在就创建。
第二种方案是通过脚本的形式,把每个索引和文档的创建语句都保存下来,如果有字段改动则删除,再重新创建。 考虑到开发时字段可能会经常变动,此时就必然会导致修改代码,所以采取第二种方案时既要修改代码,又要同时修改脚本,否则会报错,比较费事。而采用第一种方案,只需要删掉索引和文档再重新启动应用就可以了,不必再单独执行脚本,非常方便,也不容易忘记。综合开发进度及其他现实因素,决定采用第一种方案来解决创建索引和文档的问题。
笔者在这里不打算创建一个完整的项目,只需要演示用Java创建Elasticsearch索引相关部分的核心内容就行了。即使是这么一点内容,代码量也不少。
事先声明:这里的代码都是应用于本地Elasticsearch服务的,而不是云原生服务,否则代码和配置等内容会有很大不同。
首先,引入所需要的Elasticsearch相关依赖。
<!-- Elasticsearch相关依赖 -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
<!-- apache commons -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
然后修改application.properties
属性文件。
## ELASTICSEARCH
spring.elastic.rhlc.schema=http
spring.elastic.rhlc.hosts=127.0.0.1:9200
spring.elastic.rhlc.username=elastic
spring.elastic.rhlc.password=123456
spring.elastic.rhlc.connectTimeOut=5000
spring.elastic.rhlc.socketTimeOut=5000
spring.elastic.rhlc.connectionRequestTimeOut=10000
spring.elastic.rhlc.maxConnectNumber=10000
spring.elastic.rhlc.maxConnectPerRoute=8
接着,创建Elasticsearch配置类。
package com.java.book.chapter02.anno.es;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Elasticsearch配置类
*
*/
@Configuration
public class ElasticConfiguration {
@Value("${spring.elastic.rhlc.schema}")
private String schema;
@Value("${spring.elastic.rhlc.hosts}")
private String hosts;
@Value("${spring.elastic.rhlc.username}")
private String username;
@Value("${spring.elastic.rhlc.password}")
private String password;
@Value("${spring.elastic.rhlc.connectTimeOut}")
private int connectTimeOut;
@Value("${spring.elastic.rhlc.socketTimeOut}")
private int socketTimeOut;
@Value("${spring.elastic.rhlc.connectionRequestTimeOut}")
private int connectionRequestTimeOut;
@Bean
public RestHighLevelClient client() {
String[] hosts = this.hosts.split(",");
HttpHost[] httpHosts = new HttpHost[hosts.length];
for (int i = 0; i < hosts.length; i++) {
httpHosts[i] = new HttpHost(hosts[i].split(":")[0], Integer.parseInt(hosts[i].split(":")[1]), schema);
}
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
RestClientBuilder builder = RestClient.builder(httpHosts).setRequestConfigCallback(requestConfigBuilder -> {
requestConfigBuilder.setConnectTimeout(connectTimeOut);
requestConfigBuilder.setSocketTimeout(socketTimeOut);
requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeOut);
return requestConfigBuilder;
}).setHttpClientConfigCallback(httpClientBuilder -> {
httpClientBuilder.disableAuthCaching();
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
});
return new RestHighLevelClient(builder);
}
}
上面这些都属于常规动作。接下来,还是按照套路进行:先创建Elasticsearch字段类型枚举。
package com.java.book.chapter02.anno.es;
/**
* elastic字段类型枚举
*
*/
public enum FieldType {
Auto("auto"),
Text("text"),
Keyword("keyword"),
Long("long");
public String value;
private FieldType(final String value) {
this.value = value;
}
public static String getValue(final String value) {
for (FieldType field : FieldType.values()) {
if (field.getValue().equalsIgnoreCase(value)) {
return field.value;
}
}
return null;
}
public String getValue() {
return value;
}
public void setValue(final String value) {
this.value = value;
}
}
然后,创建Elasticsearch的相关字段注解。
package com.java.book.chapter02.anno.es;
import org.springframework.core.annotation.AliasFor;
import java.lang.annotation.*;
/**
* elastic字段注解,定义每个elasticsearch字段上的属性
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Documented
@Inherited
public @interface DocField {
@AliasFor("name")
String value() default "";
@AliasFor("value")
String name() default "";
FieldType type() default FieldType.Auto;
boolean index() default false;
String format() default "";
String pattern() default "";
boolean store() default false;
boolean fielddata() default false;
String searchAnalyzer() default "";
String analyzer() default "";
String normalizer() default "";
}
接着,创建Elasticsearch文档注解(相当于数据表的注解)。
package com.java.book.chapter02.anno.es;
import java.lang.annotation.*;
/**
* elastic文档注解,定义每个elasticsearch文档上的属性
*
*/
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
public @interface Document {
String index();
String type() default "_doc";
boolean useServerConfiguration() default false;
short shards() default 1;
short replicas() default 0;
String refreshInterval() default "1s";
String indexStoreType() default "fs";
}
再创建Elasticsearch文档(相当于数据表)。
package com.java.book.chapter02.anno.es;
/**
* elastic文档对象
*
*/
@Document(index = "document", type = "_doc", shards = 1, replicas = 0)
public class ElasticDocument {
private static final long serialVersionUID = 2879048112350101009L;
// 文档编码
@DocField(name = "guid", type = FieldType.Keyword)
protected String guid = "";
// 标题
@DocField(name = "title", type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
protected String title = "";
// 文档创建时间(资源实际创建时间)
@DocField(name = "createtime", type = FieldType.Long)
protected long createtime;
// 文档更新时间(资源实际更新时间)
@DocField(name = "updatetime", type = FieldType.Long)
protected long updatetime;
public ElasticDocument() {
}
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public long getCreatetime() {
return createtime;
}
public void setCreatetime(long createtime) {
this.createtime = createtime;
}
public long getUpdatetime() {
return updatetime;
}
public void setUpdatetime(long updatetime) {
this.updatetime = updatetime;
}
@Override
public String toString() {
return String.format("{\"guid\":\"%s\", \"title\":\"%s\", \"createtime\":%d, " +
"\"updatetime\":%d}", guid, title, createtime, updatetime);
}
}
这里面的@Document
就是刚才创建的文档注解。
还需要一个真正的执行者,由它来完成所有材料的拼装,组成我们的缝合怪
。
package com.java.book.chapter02.anno.es;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
/**
* ElasticDao
*
*/
@Component
public class ElasticDao {
@Autowired
private RestHighLevelClient client;
/**
* 索引是否存在
*
*/
public boolean indexExist(final String index) {
try {
return client.indices().exists(new GetIndexRequest(index), RequestOptions.DEFAULT);
} catch (IOException e) {
System.out.println("index exist exception");
}
return false;
}
/**
* 解析类注解,获取包括父类字段在内的所有字段
* 因为解析的时候,会把父类及自身的一些额外字段给解析进去
* 如logger、serialVersionUID等
* 所以需要把这些无用的字段排除掉
* 这里不存在继承,所以直接调用clazz.getDeclaredFields()
* 另外,如果存在继承关系,该怎么处理呢?(可以作为思考练习)
*
*/
public static List<Field> getAllDeclaredFields(Class<?> clazz) {
return new ArrayList<>(Arrays.asList(clazz.getDeclaredFields()));
}
/**
* 创建索引,前面都是为了实现它作准备
* 这里会通过注解,一路解析文档的字段,拼接成可执行的脚本交给elasticsearch的api去执行
*
*/
public boolean createIndex(final String index, final Class<?> clazz) {
try {
Document document = (Document) clazz.getAnnotation(Document.class);
int shards = document.shards();
int replicas = document.replicas();
if (indexExist(index)) {
return false;
}
CreateIndexRequest request = new CreateIndexRequest(index);
request.settings(Settings.builder()
.put("index.number_of_shards", shards)
.put("index.number_of_replicas", replicas)
);
StringBuilder builder = new StringBuilder();
builder.append("{\n");
builder.append(" \"properties\": {\n");
List<Field> list = getAllDeclaredFields(clazz);
int length = list.size();
for (int i = 0; i < length; i++) {
DocField docField = list.get(i).getAnnotation(DocField.class);
if (null == docField) {
continue;
}
builder.append(" \"").append(docField.name()).append("\" : {\n");
builder.append(" \"type\" : \"").append(docField.type().value).append("\"");
if (docField.index()) {
builder.append(", \n");
builder.append(" \"index\" : ").append(docField.index());
}
if (docField.fielddata()) {
builder.append(", \n");
builder.append(" \"fielddata\" : ").append(docField.fielddata());
}
if (docField.store()) {
builder.append(", \n");
builder.append(" \"store\" : ").append(docField.store());
}
if (StringUtils.isNotBlank(docField.analyzer())) {
builder.append(", \n");
builder.append(" \"analyzer\" : \"").append(docField.analyzer()).append("\"");
}
if (StringUtils.isNotBlank(docField.format())) {
builder.append(", \n");
builder.append(" \"format\" : \"").append(docField.format()).append("\"");
}
if (StringUtils.isNotBlank(docField.searchAnalyzer())) {
builder.append(", \n");
builder.append(" \"search_analyzer\" : \"").append(docField.searchAnalyzer()).append("\"");
}
if (StringUtils.isNotBlank(docField.pattern())) {
builder.append(", \n");
builder.append(" \"pattern\" : \"").append(docField.pattern()).append("\"");
}
if (StringUtils.isNotBlank(docField.normalizer())) {
builder.append(", \n");
builder.append(" \"normalizer\" : \"").append(docField.normalizer()).append("\"");
}
if (i == length -1) {
builder.append("\n }\n");
} else {
builder.append("\n }, \n");
}
}
builder.append(" }\n");
builder.append("}\n");
request.mapping(JSON.parseObject(builder.toString()).toJSONString(), XContentType.JSON);
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
boolean acknowledged = response.isAcknowledged();
return acknowledged;
} catch (IOException e) {
System.out.println("create index exception");
}
return false;
}
}
拼接完成,该表演了。
package com.java.book.chapter02.anno.es;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
/**
* 索引Service实现
*
*/
@Service
public class IndexService {
@Resource
private ElasticDao elasticDao;
/**
* 索引初始化
*
*/
@PostConstruct
private void initIndex() {
boolean flag = false;
// 创建一个名为Test的索引
if (!elasticDao.indexExist("ExtractMethod")) {
flag = elasticDao.createIndex("ExtractMethod", ElasticDocument.class);
if (flag) {
System.out.println("create ExtractMethod index success");
} else {
System.out.println("create ExtractMethod index failure");
}
} else {
System.out.println("ExtractMethod index exist");
}
}
}
这就是整个注解结合Elasticsearch的真实案例。其实这个一开始只是作为代码里面的小工具出现的,但随着需求越来越多,以致它后来竟演化成为了一个小的内部系统,可以通过它实现动态创建、修改、删除Elasticsearch的索引和文档,以及导出、导入数据等功能,非常方便。目前那些主流的开源框架大概也是像这样一点点发展起来的吧。
感谢支持
更多内容,请移步《超级个体》。