• docker创建elasticsearch、elasticsearch-head部署及简单操作


    elasticsearch部署

    1  拉取elasticsearch镜像


         docker pull elasticsearch:7.7.0

    2   创建文件映射路径

         mkdir /mydata/elasticsearch/data
         mkdir /mydata/elasticsearch/plugins
         mkdir /mydata/elasticsearch/config

    3  文件夹授权
       


        chmod 777 /mydata/elasticsearch/data

    4  修改配置文件


        cd /mydata/elasticsearch/config
        vi elasticsearch.yml
        填入如下内容:
        #集群名称
        cluster.name: "elasticsearch"
        network.host: 0.0.0.0
        #跨域设置
        http.cors.enabled: true
        http.cors.allow-origin: "*"
        #http端口
        http.port: 9200
       #java端口
       transport.tcp.port: 9300

    5  运行镜像:


       docker run --name elasticsearch -p 9200:9200 -p 9300:9300 -d  -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms512m -Xmx512m"  \
    -v /mydata/elasticsearch/data:/usr/share/elasticsearch/data -v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins  \
    -v /data/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml elasticsearch:7.7.0

    6  访问页面:

       http://192.168.56.102:9200 查看是否部署成功

    如出现如上内容,表示elasticsearch部署成功,就可以进行搜索操作了

    elasticsearch-head部署

        为了更好的使用elasticsearch,需要部署elasticsearch-head插件

    1  #拉取镜像


          docker pull mobz/elasticsearch-head:5

      2  启动容器


        docker run -d --name elasticsearch-head -p 9100:9100 mobz/elasticsearch-head:5

       3 访问elasticsearch-head页面内容

            

    elasticsearch-head的页面操作

        1  查询所有数据

                    

       出现如上所示报错。解决方案如下:

      1  docker exec -it {elasticearch-head}容器名称 /bin/bash

       2  修改elasticsearch-head的js文件

           vi  /usr/src/app/_site/vendor.js

          a  第6886行 :/contentType: "application/x-www-form-urlencoded
              改为 :contentType: "application/json;charset=UTF-8"

           b  第7574行 var inspectData = s.contentType === "application/x-www-form-urlencoded" &&
               改为 var inspectData = s.contentType === "application/json;charset=UTF-8" &&

       3  重启elasticsearch-head容器

    其他页面操作参见博客:

    ElasticSearch-Head操作Elasticsearch进行查询数据(查询所有数据,查询单个索引所有数据,查询单个索引指定类型所有数据,根据指定条件查询数据)_elasticsearch-head 查询-CSDN博客

    使用java操作elasticsearch

     1 引入依赖:

      

    1. org.elasticsearch.client
    2. elasticsearch-rest-high-level-client
    3. 7.7.0
    4. org.elasticsearch
    5. elasticsearch
    6. org.elasticsearch
    7. elasticsearch
    8. 7.7.0

    2  相关crud操作

    1. public class EsHandler {
    2. private static final String ES_SERVER_ADDRESS = "192.168.56.102";
    3. private static final String IDX_NAME = "employee";
    4. private static RestHighLevelClient CLIENT = null;
    5. public static void main(String[] args) throws IOException {
    6. // 初始化
    7. init();
    8. // 创建索引数据
    9. createIndex();
    10. // 修改数据
    11. // updateDoc();
    12. // 删除数据
    13. // deleteDoc();
    14. // 查询数据
    15. // searchDoc();
    16. // 关闭连接
    17. close();
    18. }
    19. private static void init() {
    20. CLIENT = new RestHighLevelClient(RestClient.builder(
    21. new HttpHost(ES_SERVER_ADDRESS,9200,"http")));
    22. }
    23. private static void close() throws IOException {
    24. CLIENT.close();
    25. }
    26. private static void createIndex() throws IOException {
    27. IndexRequest indexRequest = new IndexRequest(IDX_NAME);
    28. Map insertInfo = new HashMap<>();
    29. insertInfo.put("name","wangwu");
    30. indexRequest.source(insertInfo);
    31. IndexResponse response = CLIENT.index(indexRequest, RequestOptions.DEFAULT);
    32. System.out.println("ID:" + response.getId() + "/t" + "RESULT:" + response.getResult());
    33. }
    34. private static void updateDoc() throws IOException {
    35. UpdateRequest updateRequest = new UpdateRequest(IDX_NAME,"8");
    36. // 注意此处的泛型类型:,如果是其他的泛型类型,es的api会认为是另一套api调用
    37. Map sourceInfo = new HashMap<>();
    38. sourceInfo.put("name","骡子摊");
    39. // updateRequest.doc("name","隆昌羊肉汤");
    40. updateRequest.doc(sourceInfo);
    41. updateRequest.timeout("1s");
    42. updateRequest.retryOnConflict(3);
    43. UpdateResponse response = CLIENT.update(updateRequest, RequestOptions.DEFAULT);
    44. System.out.println("ID:" + response.getId() + "/t" + "RESULT:" + response.getResult());
    45. }
    46. private static void deleteDoc() throws IOException {
    47. DeleteRequest deleteRequest = new DeleteRequest(IDX_NAME,"9");
    48. DeleteResponse response = CLIENT.delete(deleteRequest, RequestOptions.DEFAULT);
    49. System.out.println("ID:" + response.getId() + "/t" + "RESULT:" + response.getResult());
    50. }
    51. /**
    52. *
    53. * @throws IOException
    54. */
    55. private static void searchDoc() throws IOException {
    56. SearchSourceBuilder builder = new SearchSourceBuilder()
    57. .query(QueryBuilders.matchQuery("message", "execute"));
    58. SearchRequest searchRequest = new SearchRequest();
    59. searchRequest.indices("rizhi-log-*");
    60. searchRequest.source(builder);
    61. // 执行请求
    62. SearchResponse response = CLIENT.search(searchRequest, RequestOptions.DEFAULT);
    63. // 解析查询结果
    64. System.out.println(response.toString());
    65. }

  • 相关阅读:
    1141 PAT Ranking of Institutions
    弹性响应蒸馏 | 用弹性响应蒸馏克服增量目标检测中的灾难性遗忘
    基于springboot和ftp实现的网盘文件系统
    课题学习(七)----粘滑运动的动态算法
    leetCode 70.爬楼梯 动态规划
    软件项目管理--软件工作量估算
    html页面仿word文档样式(vue页面也适用)
    多数据源切换
    protobuf 插件(option)使用
    手把手开发Admin 系列五(规划api篇)
  • 原文地址:https://blog.csdn.net/qq_39203337/article/details/133811312