zoukankan      html  css  js  c++  java
  • ElasticSearch的安装和使用

    一、传统版安装

    1.下载es: https://www.elastic.co/downloads/past-releases/elasticsearch-5-6-8

    2.运行:下载后解压,进入到bin目录,执行:elasticsearch。浏览器输入http://localhost:9200/测试是否启动

    3.安装head插件(用于可视化操作es)

    1)下载head插件:https://github.com/mobz/elasticsearch-head

    2)解压到任意目录,但是要和elasticsearch的安装目录区别开

    3)安装node js ,安装cnpm

    4)全局安装grunt 。Grunt是基于Node.js的项目构建工具。它可以自动运行你所 设定的任务

    npm install -g grunt-cli

    5)安装依赖

    cnpm install

    6)进入elasticsearch-head目录启动head

    grunt server

    7)浏览器输入http://localhost:9100/测试head插件是否成功安装

    8)在页面上方文本框输入es的地址(端口9200),点击连接按钮出现跨域问题

    4.在es目录下的config/elasticsearch.yml添加两行使es能被head等插件跨域访问。改配置后重启es:

    http.cors.enabled: true
    http.cors.allow-origin: "*"

    5.安装IK分词器 

    1)下载ielasticsearch-analysis-ik-5.6.8.zip。下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases

    2)解压zip,重命名解压后的目录为ik,拷贝到elasticsearch/plugins目录

    3)重启es

    4)浏览器测试ik分词器:

    http://localhost:9200/_analyze?analyzer=ik_max_word&pretty=true&text=我是程序员 //其中ik_max_word为最多切分,ik_smart为最少切分

    5)自定义词库

    a.进入elasticsearch/plugins/ik/config目录

    b.新建一个my.dic文件,编辑内容:

    c.修改elasticsearch/plugins/ik/config/IKAnalyzer.cfg.xml

    <properties>
        <comment>IK Analyzer 扩展配置</comment>
        <!--用户可以在这里配置自己的扩展字典 -->
        <entry key="ext_dict">my.dic</entry>
         <!--用户可以在这里配置自己的扩展停止词字典-->
        <entry key="ext_stopwords"></entry>
        <!--用户可以在这里配置远程扩展字典 -->
        <!-- <entry key="remote_ext_dict">words_location</entry> -->
        <!--用户可以在这里配置远程扩展停止词字典-->
        <!-- <entry key="remote_ext_stopwords">words_location</entry> -->
    </properties>

    d.重启es。测试

    http://localhost:9200/_analyze?analyzer=ik_max_word&pretty=true&text=人艰不拆

    二、使用

    1.以Restful方式操作ES

    ● 新建一个名为articleindex索引。PUT请求

    http://127.0.0.1:9200/articleindex/

    ● 查询全部文档。GET请求

    http://127.0.0.1:9200/articleindex/article/_search

    ● 创建/修改文档(如果不存在_id为1的文档则会创建,反之则会修改)。PUT请求

    http://192.168.184.134:9200/articleindex/article/1

    ● 按I_id查询文档。GET请求

    http://192.168.184.134:9200/articleindex/article/1

    ● 基本匹配查询(查title为“好给力”的文档)。GET请求

    http://192.168.184.134:9200/articleindex/article/_search?q=title:好给力

    ● 模糊查询。GET请求

    http://192.168.184.134:9200/articleindex/article/_search?q=title:*s*

    ● 删除文档。DELETE请求

    http://192.168.184.134:9200/articleindex/article/1

     2.SpringDataElasticSearch

    0)因为elasticsearch从5版本以后默认不开启远程连接,需要修改es目录下config/elasticsearch.yml,添加一行

    transport.host: 0.0.0.0 

    如果不加上会出现NoNodeAvailableExceptionn[None of the configured nodes are available

    其中0.0.0.0表示允许任何ip远程连接es。如果项目上线需要指定ip有权限访问

    1)添加依赖

            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-elasticsearch</artifactId>
                <version>3.0.6.RELEASE</version>
            </dependency>

    2)application.yml文件配置

    spring:
      data:
        elasticsearch:
          cluster-nodes: 127.0.0.1:9300 #java操作es用的是9300端口

    3)实体类

    @Document(indexName = "tenpower", type = "article")
    public class Article implements Serializable {
        @Id
        private String id;
    
        @Field(analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
        private String title; //文章标题
    
        @Field(analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
        private String content;//文章正文
    
        private String status; //审核状态
    
        //Getter and Setter ...
    }

    4)Dao。也是SpringData风格

    public interface ArticleSearchDao extends ElasticsearchRepository<Article,String> {
    }

     3.logstash使用

    1)解压logstash,然后在解压目录下创建mysqletc目录(名称随意)

    2)在该目录下创建mysql.conf(名称随意),内容如下(需要修改配置):

    input {
      jdbc {
          # mysql jdbc connection string to our backup database
          jdbc_connection_string => "jdbc:mysql://192.168.25.129:3306/tensquare_article?characterEncoding=UTF8"
          # the user we wish to excute our statement as
          jdbc_user => "root"
          jdbc_password => "123456"
          # the path to our downloaded jdbc driver  
          jdbc_driver_library => "E:/logstash-6.7.0/mysqletc/mysql-connector-java-5.1.46.jar"
          # the name of the driver class for mysql
          jdbc_driver_class => "com.mysql.jdbc.Driver"
          jdbc_paging_enabled => "true"
          jdbc_page_size => "50000"
          #以下对应着要执行的sql的绝对路径。
          #statement_filepath => ""
          statement => "select id,title,content from tb_article"
          #定时字段 各字段含义(由左至右)分、时、天、月、年,全部为*默认含义为每分钟都更新(测试结果,不同的话请留言指出)
          schedule => "* * * * *"
      }
    }
    
    output {
      elasticsearch {
          #ESIP地址与端口
          hosts => "localhost:9200" 
          #ES索引名称(自己定义的)
          index => "tenpower"
          #自增ID编号
          document_id => "%{id}"
          document_type => "article"
      }
      stdout {
          #以JSON格式输出
          codec => json_lines
      }
    }

    3)将mysql驱动包mysql-connector-java-5.1.46.jar拷贝到mysqletc目录下

    4)在logstash的bin目录下执行

    logstash -f ../mysqletc/mysql.conf

    过一会可以看到执行成功log

    三、docker下安装es

    1.安装es

    #下载镜像
    docker pull elasticsearch:5.6.8
    #运行容器 docker run -di --name=es -p 9200:9200 -p 9300:9300 elasticsearch:5.6.8

    2.修改application.yml将ip地址改为宿主机的ip地址

    3.进入容器修改/usr/share/elasticsearch/config下的elasticsearch.yml,追加

    #防止出现NoNodeAvailableException异常
    transport.host: 0.0.0.0 
    #允许head插件跨域访问
    http.cors.enabled: true
    http.cors.allow-origin: "*"

    4.修改宿主机系统配置

    1)vi /etc/security/limits.conf,追加

    * soft nofile 65536
    * hard nofile 65536

    其中nofile是单个进程允许打开的最大文件个数,soft nofile是软限制,hard nofile是硬限制

    2)vi /etc/sysctl.conf,追加

    vm.max_map_count=655360

    表示限制一个进程可以拥有的VMA(虚拟内存区域)的数量

    3)执行下面命令 修改内核参数马上生效

    sysctl -p

    5.ik分词器安装和win下安装一样

    6.安装head插件

    1)下载镜像

    docker pull mobz/elasticsearch-head:5

    2)运行head容器

    docker run -di --name=es-head -p 9100:9100 mobz/elasticsearch-head:5

     7.logstash同win

    四、完整版安装

    1.添加一个用户(viuman)。因为elasticsearch默认不允许以root账号运行

    2.改es配置文件

    1)vim elasticsearch-7.4.0/config/jvm.options。把内存调小若512还是大了继续减少

    -Xms512m
    -Xmx512m

    2)vim elasticsearch-7.4.0/config/elasticsearch.yml

    path.data: /home/viuman/elasticsearch/data # 数据目录位置
    path.logs: /home/viuman/elasticsearch/logs # 日志目录位置
    
    network.host: 0.0.0.0 # 绑定到0.0.0.0,允许任何ip来访问
    
    node.name: node-1 #打开一个注释节点 cluster.initial_master_nodes: ["node-1"] # 取消注释保留一个节点

    如果centos内核版本低于3.5,需要禁用es的插件

    bootstrap.system_call_filter: false

    3)在es目录下建上边指定的data和logs两个目录

    注:elasticsearch.yml的其他配置信息

    3.修改系统配置文件

    1)修改es可运行最大线程数。vim /etc/security/limits.conf

    * soft nofile 65536
    * hard nofile 131072
    * soft nproc 4096
    * hard nproc 4096

    2)解决线程数不够。vim /etc/security/limits.d/20-nproc.conf 

    * soft nproc 4096

    3)限制进程可以拥有的VMA(虚拟内存区域)的数量。vim /etc/sysctl.conf  

    vm.max_map_count=655360

    4.刷新配置,然后重启终端。刷新命令

    sysctl -p

    5.安装ik分词器

    1)ik版本和es保持一致,下载地址:

    https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.4.2/elasticsearch-analysis-ik-7.4.2.zip

    2)解压到elasticsearch的plugins/ik目录中

  • 相关阅读:
    Capture CIS:Capture was not found错误
    Memcached FAQ
    MTK META工具的使用和注意事项(MT6252)
    关于maps.google.com和ditu.google.cn地图偏差的说明
    VSS2005的二次开发
    同学PB经历的面试题
    给定一个字符串,包含中文字符和英文字符,取给定大小字节的子串。
    一些笔试题目和整理的答案 腾讯(Tencent)
    redhat面试题目
    Ubuntu远程链接Ubuntu之ssh
  • 原文地址:https://www.cnblogs.com/naixin007/p/10564848.html
Copyright © 2011-2022 走看看