docker run -d --restart=always -p 9200:9200 -p 9300:9300 --name=es \
-e ES_JAVA_OPTS="-Xms1024m -Xmx1024m" \
-v /data/docker/elasticsearch/data:/usr/share/elasticsearch/data \
-v /data/docker/elasticsearch/logs:/usr/share/elasticsearch/logs \
-v /data/docker/elasticsearch/config:/usr/share/elasticsearch/config \
-v /data/docker/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
-v /etc/localtime:/etc/localtime \
-v /etc/timezone:/etc/timezone \
docker.elastic.co/elasticsearch/elasticsearch:6.0.0
docker network connect localhost_connection es
注意:
- config 和plugins 做了映射以后会读取映射以后的文件,如果想执行以上的指令,需要把docker对应文件夹下的文件copy到对应的宿主机的映射文件夹里
- 尽量不要再root权限下执行,否则可能会报错,es中有些地方不允许再root用户下操作
关于ik分词
ik分词器安装比较简单,找到对应的ik打包,把包放入Plugins中,解压就好了
esclient的创建
package com.huntor.bs.connector;
import java.io.IOException;
import java.util.Collections;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHits;
import org.springframework.stereotype.Service;
import com.huntor.bs.exception.BSSysException;
import com.huntor.bs.pojo.SystemError;
import com.huntor.bs.util.ConfigUtil;
@Service
public class ElasticsearchClient {
private static final String ES_HOST = ConfigUtil.getValue("es_host");
private static final String ES_PORT = ConfigUtil.getValue("es_port");
private RestHighLevelClient client = null;
private RestClient restClient = null;
private RestHighLevelClient getClient() {
if (StringUtils.isEmpty(ES_HOST) || StringUtils.isEmpty(ES_PORT)) {
throw new BSSysException(SystemError.SEARCH_ENGINE_CONNECTION_ERROR, "Missing ES configuration.");
}
int port;
try {
port = Integer.parseInt(ES_PORT);
} catch (NumberFormatException e) {
throw new BSSysException(SystemError.SEARCH_ENGINE_CONNECTION_ERROR,
"Wrong ES configuration. The \"es_port\" is not a number.");
}
if (client == null) {
client = new RestHighLevelClient(RestClient.builder(new HttpHost(ES_HOST, port, "http")));
}
return client;
}
//获取client
private RestClient getRestClient() {
if (StringUtils.isEmpty(ES_HOST) || StringUtils.isEmpty(ES_PORT)) {
throw new BSSysException(SystemError.SEARCH_ENGINE_CONNECTION_ERROR, "Missing ES configuration.");
}
int port;
try {
port = Integer.parseInt(ES_PORT);
} catch (NumberFormatException e) {
throw new BSSysException(SystemError.SEARCH_ENGINE_CONNECTION_ERROR,
"Wrong ES configuration. The \"es_port\" is not a number.");
}
if (restClient == null) {
restClient = RestClient.builder(new HttpHost(ES_HOST, port, "http")).build();
}
return restClient;
}
//关闭client
private void closeRestClient() {
if (restClient == null) {
return;
}
try {
restClient.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
restClient = null;
}
}
private void close() {
if (client == null) {
return;
}
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
client = null;
}
}
//批量更新es中的数据
public BulkResponse bulk(BulkRequest bulkRequest) {
if (bulkRequest.requests().isEmpty()) {
return null;
}
try {
return getClient().bulk(bulkRequest);
} catch (IOException e) {
throw new BSSysException(SystemError.UPDATE_SEARCH_ENGINE_ERROR, e);
} catch (BSSysException e) {
throw e;
} catch (Exception e) {
throw new BSSysException(SystemError.SEARCH_ENGINE_CONNECTION_ERROR,
"Unable to connect ES server. Please check the configuration.", e);
} finally {
close();
}
}
//查询
public SearchHits search(SearchRequest searchRequest) {
SearchResponse searchResponse;
try {
try {
searchResponse = getClient().search(searchRequest);
} catch (IOException e) {
throw new BSSysException(SystemError.SEARCH_ENGINE_CONNECTION_ERROR, e);
} catch (BSSysException e) {
throw e;
} catch (Exception e) {
throw new BSSysException(SystemError.SEARCH_ENGINE_CONNECTION_ERROR,
"Unable to connect ES server. Please check the configuration.", e);
}
RestStatus status = searchResponse.status();
if (status.getStatus() >= 400) {
throw new BSSysException(SystemError.SEARCH_ENGINE_CONNECTION_ERROR,
"ES server response an error http status: " + status.getStatus());
}
// TimeValue took = searchResponse.getTook();
if (searchResponse.isTerminatedEarly() != null && searchResponse.isTerminatedEarly()) {
throw new BSSysException(SystemError.SEARCH_ENGINE_CONNECTION_ERROR,
"The request to es server is terminated early.");
}
if (searchResponse.isTimedOut()) {
throw new BSSysException(SystemError.SEARCH_ENGINE_CONNECTION_ERROR,
"The request to es server is time out.");
}
} finally {
close();
}
return searchResponse.getHits();
}
//安装好ik后,需要对可以分词的字段做映射
public void createIndex(String index, String name) {
try {
// 借助indexRequest的json拼接工具
IndexRequest indexRequest = new IndexRequest();
XContentBuilder builder = JsonXContent.contentBuilder().startObject().startObject("mappings")
.startObject(name).field("dynamic", true).startObject("properties").startObject("name").field("type", "text")
.field("analyzer", "ik_max_word").endObject().startObject("branch_name").field("type", "text")
.field("analyzer", "ik_max_word").endObject().startObject("id").field("type", "integer")
.endObject().startObject("publish_status").field("type", "integer").endObject()
.startObject("tenant_id").field("type", "integer").endObject().endObject().endObject().endObject()
.startObject("settings").startObject("analysis").startObject("analyzer").startObject("ik")
.field("tokenizer", "ik_max_word").endObject().endObject().endObject().endObject().endObject();
indexRequest.source(builder);
// 生成json字符串
String source = indexRequest.source().utf8ToString();
HttpEntity entity = new NStringEntity(source, ContentType.APPLICATION_JSON);
// 使用RestClient进行操作 而非rhlClient
this.getRestClient().performRequest("put", "/" + index, Collections. emptyMap(), entity);
} catch (IOException e) {
throw new BSSysException(SystemError.UPDATE_SEARCH_ENGINE_ERROR, e);
}finally {
closeRestClient();
}
}
/**
* 判断索引是否存在
*/
public boolean checkIndexExist(String index) {
Response response;
try {
response = this.getRestClient().performRequest("HEAD", index);
} catch (IOException e) {
throw new BSSysException(SystemError.SEARCH_ENGINE_CONNECTION_ERROR, e);
}
return response.getStatusLine().getReasonPhrase().equals("OK");
}
}