docker 中安裝elasticsearch和ik安裝配置

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

注意:

  1. config 和plugins 做了映射以后會(huì)讀取映射以后的文件,如果想執(zhí)行以上的指令,需要把docker對(duì)應(yīng)文件夾下的文件copy到對(duì)應(yīng)的宿主機(jī)的映射文件夾里
  2. 盡量不要再root權(quán)限下執(zhí)行,否則可能會(huì)報(bào)錯(cuò),es中有些地方不允許再root用戶下操作
    關(guān)于ik分詞
    ik分詞器安裝比較簡單,找到對(duì)應(yīng)的ik打包,把包放入Plugins中,解壓就好了

esclient的創(chuàng)建

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;
    }
//關(guān)閉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中的數(shù)據(jù)
    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后,需要對(duì)可以分詞的字段做映射
    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進(jìn)行操作 而非rhlClient
            this.getRestClient().performRequest("put", "/" + index, Collections.<String, String> 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");
    }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容