docker搭建es

docker搭建es官网:https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html

1.拉取镜像

docker pull docker.elastic.co/elasticsearch/elasticsearch:8.2.3

2.创建网络

docker network create elastic

3.设置虚拟内存

To set this value permanently, update the vm.max_map_count setting in /etc/sysctl.conf. To verify after rebooting, run sysctl vm.max_map_count.

sysctl -w vm.max_map_count=262144
#永久设置:
sudo vim /etc/sysctl.conf 
vm.max_map_count=262144

4.运行es

docker run --name es --net elastic -p 9200:9200 -p 9300:9300 -itd docker.elastic.co/elasticsearch/elasticsearch:8.2.3

5.复制es的配置信息

mkdir esdatadir;
docker cp es:/usr/share/elasticsearch/config  /root/data/esdatadir;
docker cp es:/usr/share/elasticsearch/data /root/data/esdatadir;
docker cp es:/usr/share/elasticsearch/plugins /root/data/esdatadir;

6.删除es初始运行的实例

docker stop es;docker rm es; 

docker rm -f es

7.重新运行es实例,映射es的数据配置

 docker run --name es --net elastic -p 9200:9200 -p 9300:9300  \
	-v /root/data/esdatadir/config:/usr/share/elasticsearch/config \
	-v /root/data/esdatadir/data:/usr/share/elasticsearch/data  \
	-v /root/data/esdatadir/plugins:/usr/share/elasticsearch/plugins \
	-itd docker.elastic.co/elasticsearch/elasticsearch:8.2.3;

#测试es 访问
curl http://127.0.0.1:9200

8.关闭ssl和密码验证,用于本地测试是否成功

sudo vim ~/esdatadir/config/elasticsearch.yml
#将xpack.security.enabled: true ==> xpack.security.enabled: false
#将xpack.security.enrollment.enabled: true ==> xpack.security.enrollment.enabled: false
#添加 http.cors.enabled: true
#添加 http.cors.allow-origin: "*"

9.为了安全限制,需要打开ssl和密码配置

禁止关闭ssl的安全配置,由于安全问题

附加:

1.一些命令生成:

#重置密码
docker exec -it es /usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic
#设置密码
docker exec -it es /usr/share/elasticsearch/bin/elasticsearch-setup-passwords interactive
#生成token
docker exec -it es /usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token --scope kibana

2.分词器安装

#复制安装
#先下载ik分词器插件,然后添加进docker
docker cp ik es:/usr/share/elasticsearch/plugins/
docker restart es;

#进入容器安装
docker exec -it es /bin/bash
cd plugins #(进入elasticsearch插件目录)

#执行安装命令,需要对应es版本号
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v8.2.3/elasticsearch-analysis-ik-8.2.3.zip

#重启es
#如果es启动报错,修改~/plugins/ik/plugin-descriptor.properties,尾部添加elasticsearch.version=8.2.3(注意版本号)


3.java操作es

由于现在是8.2.3的版本,为了安全打开了ssl的验证

客户端连接需要处理ssl

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
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.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;
import org.apache.http.ssl.SSLContextBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.net.ssl.SSLContext;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

/**
 * es 链接客户端
 *
 * @author lt
 * @createDate 2022年06月29日15:46:52
 */
@Configuration
public class EsRestConfig {
    @Value("${elasticsearch.ip}")
    private String ip;
    @Value("${elasticsearch.port}")
    private String port;
    @Value("${elasticsearch.scheme}")
    private String scheme;
    @Value("${elasticsearch.username}")
    private String username;
    @Value("${elasticsearch.password}")
    private String password;

    @Bean("elasticsearchClient")
    public ElasticsearchClient elasticsearchClient() throws Exception {
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
            // 信任所有
            public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                return true;
            }
        }).build();
        SSLIOSessionStrategy sessionStrategy = new SSLIOSessionStrategy(sslContext, NoopHostnameVerifier.INSTANCE);

        RestClient restClient = RestClient.builder(
                new HttpHost(ip, Integer.parseInt(port), scheme)).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                httpClientBuilder.disableAuthCaching();
                httpClientBuilder.setSSLStrategy(sessionStrategy);
                httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                return httpClientBuilder;
            }
        }).build();
        // Create the transport with a Jackson mapper
        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());
        // And create the API client
        ElasticsearchClient client = new ElasticsearchClient(transport);
        return client;
    }
}

4.es的一些java操作

import bandex.R;
import bandex.bean.RemoteResult;
import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import rosetta.community.entity.canal.PageEsData;
import rosetta.community.entity.dto.CtTrends;
import rosetta.community.entity.dto.CtTrendsComments;
import rosetta.community.util.es.CustomEsClient;

/**
 * @program: bppserver
 * @description
 * @author: liutian
 * @create: 2020-09-15 17:23
 **/
@Service("esSearchService")
public class EsSearchService {

    @Value("${elasticsearch.profiles}")
    private String profiles;

    @Value("${spring.application.name}")
    private String applicationName;

    /**
     * databaseName
     */
    @Value("${elasticsearch.databaseName}")
    private String databaseName;

    /**
     * 帖子搜索
     *
     * @param queryText
     * @param page
     * @param size
     * @return
     */
    public RemoteResult<PageEsData> trendsSearch(String queryText, int page, int size) {

        BoolQuery boolQuery = null;
        if (StringUtils.isNotBlank(queryText)) {
            boolQuery = BoolQuery.of(b -> b
                    .must(m -> m
                            .multiMatch(v -> v
                                    .query(queryText)
                                    .fields("content", "title")
                            )
                    )
                    .filter(f -> f
                            .term(t -> t
                                    .field("status")
                                    .value(1)
                            )
                    ));
        } else {
            boolQuery = BoolQuery.of(b -> b
                    .filter(f -> f
                            .term(t -> t
                                    .field("status")
                                    .value(1)
                            )
                    ));
        }

        // 设置boolQuery条件
        BoolQuery finalBoolQuery = boolQuery;
        Query query = Query.of(q -> q
                .bool(finalBoolQuery)
        );
        // 查询条件--->生成DSL查询语句
        String index = applicationName + "_" + profiles + "_" + databaseName + "_" + "ct_trends";
        PageEsData pageEsData = CustomEsClient.searchPage(query, page, size, index, CtTrends.class);
        return R.success(pageEsData, "查询成功");
    }


    public RemoteResult<PageEsData> trendsSearchByAuthor(String queryText, int page, int size) {
        // 设置boolQuery条件
        Query query = Query.of(q -> q
                .bool(b -> b
                        .must(m -> m
                                .multiMatch(v -> v
                                        .query(queryText)
                                        .fields("author")
                                )
                        )
                        .filter(f -> f
                                .term(t -> t
                                        .field("status")
                                        .value(1)
                                )
                        )
                )
        );
        // 查询条件--->生成DSL查询语句
        String index = applicationName + "_" + profiles + "_" + databaseName + "_" + "ct_trends";
        PageEsData pageEsData = CustomEsClient.searchPage(query, page, size, index, CtTrends.class);
        return R.success(pageEsData, "查询成功");
    }


    /**
     * 根据帖子id 返回帖子评论数据
     *
     * @param trendsId
     * @param page
     * @param size
     * @return
     */
    public PageEsData trendsCommentsSearchByTrendsId(Long trendsId, int page, int size) {
        // 设置boolQuery条件
        Query statusFilter = Query.of(q -> q
                .term(t -> t.field("status").value(1))
        );
        Query trendsFilter = Query.of(q -> q
                .term(t -> t.field("trendsId").value(trendsId))
        );
        Query query = Query.of(q -> q
                .bool(b -> b
                        .filter(statusFilter,
                                trendsFilter
                        )
                )
        );
        // 查询条件--->生成DSL查询语句
        String index = applicationName + "_" + profiles + "_" + databaseName + "_" + "ct_trends_comments";
        PageEsData pageEsData = CustomEsClient.searchPage(query, page, size, index, CtTrendsComments.class);
        return pageEsData;
    }

    /**
     * 根据用户id 返回用户评论数据
     *
     * @param userId
     * @param page
     * @param size
     * @return
     */
    public PageEsData trendsCommentsSearchByUserId(Long userId, int page, int size) {
        // 设置boolQuery条件
        Query statusFilter = Query.of(q -> q
                .term(t -> t.field("status").value(1))
        );
        Query userFilter = Query.of(q -> q
                .term(t -> t.field("userId").value(userId))
        );
        Query query = Query.of(q -> q
                .bool(b -> b
                        .filter(statusFilter,
                                userFilter
                        )
                )
        );
        // 查询条件--->生成DSL查询语句
        String index = applicationName + "_" + profiles + "_" + databaseName + "_" + "ct_trends_comments";
        PageEsData pageEsData = CustomEsClient.searchPage(query, page, size, index, CtTrendsComments.class);
        return pageEsData;
    }


}

上次更新:
贡献者: liutian