zoukankan      html  css  js  c++  java
  • elasticsearch安装配置

    1,安装es

      安装java环境

    # java --version
    java version "1.8.0_65" Java(TM) SE Runtime Environment (build 1.8.0_65-b17) Java HotSpot(TM) 64-Bit Server VM (build 25.65-b01, mixed mode)

      安装es  官网:https://www.elastic.co/guide/en/elasticsearch/reference/current/rpm.html

     # rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
    [elasticsearch-6.x]
    name=Elasticsearch repository for 6.x packages
    baseurl=https://artifacts.elastic.co/packages/6.x/yum
    gpgcheck=1
    gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
    enabled=1
    autorefresh=1
    type=rpm-md
    
    # sudo yum install elasticsearch

    2,es配置文件 

     elasticsearch.yml    配置Elasticsearch

    # cat /etc/elasticsearch/elasticsearch.yml  |grep -v ^#
    cluster.name: escluster  #集群名称
    node.name: ${HOSTNAME}     #节点名称
    path.data: /data/es-data/   #数据存储路径
    path.logs: /var/log/elasticsearch   #日志文件路径
    network.host: _eth0_    # 节点要绑定的地址 
    
    http.port: 9200     # 接受http请求的端口
    transport.tcp.port: 9300  # 集群节点之间通信的端口
    transport.tcp.compress: true
    discovery.zen.ping.unicast.hosts: ["127.0.0.1:9300"]  #要加入集群的节点,如果没指定端口默认为transport.tcp.port
    discovery.zen.minimum_master_nodes: 1 #默认1,必须要合理设置。避免网络故障时脑裂将集群拆分成独立集群(master_eligible_nodes/2)+1.假如有3个节点符合则设置为2.
    bootstrap.memory_lock: true  # 锁定进程的内存空间,防止es内存被交换出去。
    xpack.security.enabled: false  
    xpack.monitoring.enabled: true

    除此之外还有一些重要的配置。es调优的时候经常用到。

    文件描述符

    • .zip .tar包安装的。
      # ulimit -n 65536 或者
      # vim /etc/security/limits.conf
      elasticsearch  -  nofile  65536
    • rpm包的已经将文件描述符最大数量默认为65536无需更改。

      可以通过 GET _nodes/stats/process?filter_path=**.max_file_descriptors 来检查。

    禁用交换分区

    • # sudo swapoff -a
    • #  /etc/sysctl.conf  添加vm.swappiness=1
    • # 在elasticsearch.yml配置 bootstrap.memory_lock: true 锁定进程地址空间,防止es内存被交换出去。

           可以用curl -X GET "localhost:9200/_nodes?filter_path=**.mlockall" 来查看。如果"mlockall": false说明请求失败。日志里面还会有警告信息:memory locking requested for elasticsearch process but memory is not locked。可能是因为运行es的用户没有锁定内存的权限。

        解决方法:

      • 如果es是用tar或者zip包安装的
        #ulimit -l unlimited,或者
        # /etc/security/limits.conf添加
         elasticsearch - nofile 65536
      • rpm包安装的
        在/usr/lib/systemd/system/elasticsearch.service文件中,或者添加一个/etc/systemd/system/elasticsearch.service.d/override.conf文件
        [Service]
        LimitMEMLOCK=infinity
        # sudo systemctl daemon-reload

     虚拟内存

    • # sysctl -w vm.max_map_count = 262144
      或者是
      # vim /etc/sysctl.conf
         vm.max_map_count=262144 
      # sysctl vm.max_map_count
      # rpm包已经自动配置,无需配置。

     jvm.options  配置es的jvm

    -Xms256m
    -Xmx256m

    3,架构

    filebeat--> kafka --> logstash --> elasticsearch --> kibana

    filebeat收集日志传给kafka-->logstash input作为消费者消费kafka的数据 -->logstash output将input的内容传给es --> kibana上添加新索引

    4,Restful API

    Elasticsearch支持使用RESTful API,可以使用RESTful API来进行增加文档,删除文档等操作,也可以用于查询。

    可以直接使用 kibana的 DevTools来发送restful请求,进行增删改查操作。

    es 查询语句 

    URI Search

          参数有q, analyzer,_source,sort,from,size等

    • GET my_index/doc/_search?q=quick
      GET my_index*/_search?q=title:quick
      GET _search?q=user:ketty
      GET _all/_search?q=user:ketty

    Request body search

    • query
      GET test_index/_search
      {
        "query": {
          "term": {    
            "age": {
              "value": "22"
            }}}}
    • from/size
      GET test_index/_search
      {
        "from": 0,    #第一个结果的偏移量,第0页  from默认为0
        "size": 1,     # 最大命中个数 , 1条数据   size 默认为10
        "query": {
          "term": {
            "name": {
              "value": "ketty"
            }}}}
    • sort    
    • GET test_index/_search
      {
        "query": {         #先过滤出含有ketty的name字段
          "term": {
            "name": {
              "value": "ketty"            
            }
          }
        }, 
        "sort": [        #后对这些字段排序
          {
            "age": {
              "order": "desc"         #desc 倒序 asc 顺序
            }}}}
    • source filtering

              默认会返回_source 所有字段的内容。  

    • # "_source": false   关闭
      # "_source": "fieldname"  
      # "_source": "obj.*"  可接受通配符
      
      GET test_index/_search
      {
        "_source": {                
          "excludes": "content",     
          "includes": "name"
        },
        "query": {
          "term": {
            "content": {
              "value": "hello"
            }}}}

     query DSL

    • query and filter context

                  查询上下文:文档匹不匹配这个查询,相关度高吗。是在query进行查询是的执行环境

                  过滤器上下文:文档匹不匹配。 不考虑相关性算分,和返回的排序问题。使用filter参数时

    • GET /_search
      {
        "query": {           #查询上下文
          "bool": {          
            "must": [      #两个match在查询上下文中
              { "match": { "title":   "Search"        }},        
              { "match": { "content": "Elasticsearch" }}  
            ],
            "filter": [       # 过滤器上下文
              { "term":  { "status": "published" }},   
              { "range": { "publish_date": { "gte": "2015-01-01" }}} 
            ]   # term,range在过滤器上下文中,会过滤掉不匹配的,但不会影响算分
          }
        }
      }
      View Code
    • match_all query
      GET test_index/_search
      {
        "query": {
          "match_all": {}
        }
      }
    • full-context queries
      • match   query
      1. GET test_index/_search

        {
          "query": {
            "match": {
              "content": {
                "query": "hello ketty",  
                "operator": "and"     # 默认是or) 匹配出content字段中同时含有hello和ketty单词的文档
              }}}}
        #模糊查询 fuzziness
        GET test_index/_search
        {
          "query": {
            "match": {
              "content": {
                "query": "test tontent",
                "fuzziness": 1      # "fuzziness":"auto" 自动根据字段值长度编辑距离
              }}}}
      • match_phrase 匹配短语
      1. GET test_index/_search
        {
          "query": {
            "match_phrase": {
              "content": {
                "query": "test content"   # 如果使用match,会匹配带有test,content,test content的文档 
              }}}}
          # "slop":"2"  "content test" 这个顺序 也可以匹配
        # "analyzer": "my_analyzer" 可以指定分词器
      • match_phrase_prefix   匹配短语前缀
      1. PUT test_index/doc/6
         {  "name":"fox",
             "message":"quick brown fox." }
        GET test_index/doc/_search
        {
          "query": {
            "match_phrase_prefix": {
              "message": {
                "query": "quick brown f",
                "max_expansions": "2"  # max_expansions 控制将要扩展的后缀数量
              }
            }}}
      • multi_match   多字段    
      1. GET test_index/_search
        {
          "query": {
            "multi_match": {
              "query": "hello",
              "fields": ["message","conte*"]    #可以使用通配符
            }
          }
        }

      • query_string   
      • #### deafult_field  指定字段,如果不指定默认字段会查询索引中的所有字段

        GET test_index/_search

        {

          "profile":true,    #查看执行过程 
          "query": {
            "query_string": {
              "default_field": "message",
              "query": "fox AND (white OR brown)" #匹配到 "slow white fox" 和 "quick brown fox"
              }}}

        ### fields 多字段查询 
        GET test_index/_search { "query": { "query_string": { "fields": ["message","content"], "query": "brown AND fox" }}}
        等同于 -->

          {
            "query": {
              "query_string": {
              "query": "(message:brown OR content:brown) AND (message:fox OR content:fox)" 
              }}}

          #返回结果

          "_source": {"message": "quick brown fox."  }}]
        也可以在字段名中使用通配符,例如:

          GET test_index/_search
            {
              "query": {
              "query_string": {
              "fields": ["messa*"],  #使用通配符匹配字段名
              "query": "brown AND fox"
              }}}}     等同于----》

          GET test_index/_search
            {
              "query": {
              "query_string": {
              "query": "messag\*:(brown AND fox)"   #将字段名写在query string里面。转义* 
              }}}  

             # 由于是json字符串中的特殊字符,因此需要用进行转义,因此上面的两个反斜杠。

          # 返回结果:

          "_source": {"message": "quick brown fox."  }}]

        # query string syntax

          字段名 例如:
            status:active
            title:(quick OR brown)等同于 title:(quick brown) titile字段中含有quick或者brown
            author:"John Smith" author字段中包含John Smith短语的
            book.*:(quick brown) 字段名为book.title,book.cotent之类的字段名包含quick或者brown
            _exists_:title title字段中有非null值

          通配符 ? *
            qu?ck bro* 能匹配quick或者bro*

              GET test_index/_search
               {
                 "query": {
                 "query_string": {
                 "query": "name:ket*"   #匹配到ketty kety
                  }}}

          正则表达式  可以通过包装在/中嵌入query string中
            GET test_index/_search
              {
                "query": {
                "query_string": {
                "query": "name:/k.*y/" #可以匹配name中包含ketty,kety等文档
                 }}}
          模糊查询 ~1 ~2  默认编辑距离为2
            GET test_index/_search
              {
                "query": {
                "query_string": {
                "query": "mess\*:(quikc~1)"   #可以匹配quick 
                }}}

          接近查询  指定短语中最大编辑距离   "fox quick"~5  可以匹配"quick brown fox"

          范围查询 [] 包含范围  {} 排他范围  [} {]

           date:[2018-01-01 TO 2018-07-03] 
           count: [10 TO *] 大于等于10
           age:[1 TO 5} 大于等于1 小于5
           age: >10
           age: <=5
            GET test_index/_search
            {
              "query": {
              "query_string": {
              "query": "age:[18 TO 24}" #大于等于18  小于24
              }}}

         Boosting ^ 使一个术语比另一个术语更相关 默认值是1   quick^2 fox

          布尔运算符
           + 必须存在
           - 必须不存在
           && AND
           || OR
           !  NOT

            GET test_index/_search

             {
               "query": {
               "query_string": {
               "query": "name:(!ketty)"
                }}}

          分组  (quick OR brown) AND fox

          保留字符  某些特殊字符需要用反斜杠转义: + - = && || > < ! ( ) { } [ ] ^ " ~ * ? : /

                    <>  无法转义

                例如:要查询(1+1)=2  写成 (1+1)=2

        • lucene query syntax
          lucene查询语法
          自由文本搜索 safari 表示搜索所有字段的safari
          特定字段中搜索值 status:200
          搜索多个值 status:[400 TO 499]
          布尔 AND,OR,NOT。要查找4XX状态码并且扩展名php或html status:[400 TO 499] AND (extension:php OR extension:html)
          Lucene 支持转义特殊字符 + - && || ! ( ) { } [ ] ^ " ~ * ? : 
          这些需要在字符前使用转义。 例如搜索(1+1):2 需要写成(1+1):2
           
          terms  
               单个term: 搜索单个单词例如"test"或者"hello"
               短语查询:用双引号括起来一组单词 "hello world"
                多个术语可以和布尔运算符组合起来
          fields   可以指定字段 也可以使用默认字段
                字段名:术语   例如 title:Do it right  在title中搜索Do,在默认字段中搜索it和right
          通配符  
              支持?和*通配符 (不能用在短语查询中)
              ? 单个字符    te?t  匹配 test text等
              * 多字符     test*  匹配 test,tests,tester等
                注意:不能用?或*作为搜索的第一个字符 
          模糊匹配 roam~1 
          range范围查询  []包含首尾  {}不包含首尾
              mod_date:[20170101 TO 20180606] 字段值在>=20170101 <=20180606的文档
              title:{Aida TO Carmen}  文件标题介于Aida Carmen之间但不包含Aida,Carmen 
          布尔运算符 AND OR NOT 必须大写否则查不出来哦 
            AND &&可替代AND 
            OR || 可以替代OR 要搜索包含 apache或者nginx的文档,可用查询 apache nginx 或者是 apache OR nginx 
            + 必须包含该字段值 例如 +jakarta lucene 表示必须包含jakarta可能包含lucene的文档 
            NOT - 可代替NOT 必须不包含 
          分组  用括号将子句分组为子查询。
               (jakarta OR apache) AND website  有website 可能有jakarta或apache 
          Field 分组  使用括号将多个字句分组到单个字段
               title:(+return +"pink panther")  搜索title字段包含单词return和短语"pink panther"的
          转义  特殊字符需要转义
              + - && || ! ( ) { } [ ] ^ " ~ * ? :   
               搜索(1+1):2  应该使用 (1+1):2
          
           
          View Code
        • kuery 
          加引号是短语搜索。 message:"Quick brown fox"  将搜索短语 ,不加引号将字段先分词再查询,可以匹配到brown quick fox
          多个搜索项必须由布尔运算符分割。布尔运算符不区分大小写  and or not  
                response:200 extension:php 将写成  response:200 and extension:php
           默认情况下 and优先级高于or 可以用括号( )分组覆盖优先级
          单个字段搜索多个值 response:(200 or 400)
          范围。 变为:bytes>1000。在 lucene里面是 bytes:>100
                >,>=,<,<=
          存在的查询。response:* 表示存在字段
          通配符。machine: win*  通配符   可以匹配win7 win10等   
                             machine.os*: win*   。使用的是es里面的复合查询
                    在kibana搜索条里面使用kuery   source : /ubox/logs/test*
                    使用lucene    source : /ubox/logs/test*  
      • simple_query_string   

            与常规的query_string不同,simple_query_string永远不会抛异常,并丢弃查询的无效部分

                          simple_query_string支持的特殊字符有:+   |   -   "   *   (AND)  ~N在单词后  ~N在短语后  搜索这些特殊字符需转义


      1. GET test_index/_search { "query": { "simple_query_string": { "query": "(test content) | fox + white", # 匹配到一条 "message": "slow white fox." "fields": ["content","mess*"], "default_operator": "AND" }}}
        # +AND |OR -否定 *在末尾表示前缀查询
    •  Term level queries
      • term
        #区别于match。 term不会对查询语句做分词,而是将查询语句作为整个单词
        GET test_index/_search
        {
          "query": {
            "term": {
              "message": {
                "value": "test message" #无法匹配到"this is a test message"
              }}}}
        
        GET test_index/_search
        {
          "query": {
            "match": {
              "message": "test alkdfj"  #可以匹配"this is a test message"
            }}}
      • terms  查询多个单词
        GET test_index/_search
        {
          "query": {
            "terms": {
              "message": [
                "test",
                "message"
              ]}}}
      • range   范围查询 
        GET test_index/_search
        {
          "query": {
            "range": {
              "age": {
                "gte": 10,      # gte 大于等于 ,gt 大于
                "lte": 20        # lte 小于等于, lt 小于
              }}}}
        #按时间的range   
        GET /_search
        {
          "query": {
            "range": {
              "@timestamp": {
                "gte": "now-1h/d",  # -1h:减去1小时, /d:向下舍入到UTC 00:00
                "lte": "now"
              }}}}
      • exists 字段存在
        GET test_index/_search
        {
          "query": {
            "exists":{
              "field":"name"
            }}}
      • prefix   前缀查询
        GET test_index/_search
        {
          "query": {
            "prefix": {
              "name": {
                "value": "ket"
              }}}}
      • wildcard   通配符查询
        GET test_index/_search
        {
          "query": {
            "wildcard": {
              "name": {
                "value": "ke*y"   
              }}}}
      • regexp   正则表达式查询
        GET test_index/_search
        {
          "query": {
            "regexp":{
              "name":"k.*y"     
            }}}
      • fuzzy   模糊查询
        GET test_index/_search
        {
          "query": {
            "fuzzy": {
              "name":{
                "value": "ket",
                "fuzziness": 2       #fuzziness 最大编辑距离
              }}}}
      • type
      • GET _search
        {
          "query": {
            "type":{
              "value":"my_type"
            }}}
      • ids 
        GET _search
        {
          "query": {
            "ids": {
              "type": "my_type",
              "values": ["1","2"]
            }}}
    •   compoud queries 复合查询
      • constant_score 
        GET _search
        {
          "query": {
            "constant_score": {
              "filter": {      # filter语句在过滤器上下文中 得分被忽略
                "term": { 
                  "name": "ketty"
                }
              },
              "boost": 1.2  #设定boost的值
            }}}
      • bool 

           bool查询由一个或多个子句构建,每个子句都有一个类型. 类型有:must,must not,should,filter

      • GET nginx-access-log-2018.06.03/_search
        {
          "query": {
            "bool": {
              "must": [
                {"match":{
                  "source":"/ubox/logs/budd/access.log"
                }},
                {"range":{
                  "@timestamp":{
                    "gte":"2018-06-03T00:00:00.000",
                    "lte":"2018-06-03T02:00:00.000"
                  }
                }},
                {"regexp":{
                  "url_path":"/sync/vmSkin.*"
                }}
                ]
             }}}
    •  joining queries
      • nested  嵌套类型的查询
        PUT test_index
        {
          "mappings": {
            "doc": {
              "properties": {
                "man":{            #设置man字段为嵌套类型
                  "type": "nested",   
                  "properties": {  
                    "age":{
                      "type":"integer"
                     },
                    "name":{
                      "type":"text"
                    }}}}}}}}
        
        PUT test_index/doc/1
        {
          "man":[
            {
              "name":"alice white",
              "age":34
            },
            {
              "name":"peter brown",
              "age":26
            }
            ]
        }
        GET test_index/_search    #查询语句
        {
          "query": {
            "nested": {          #关键字
              "path": "man",    #嵌套字段
              "query": {
                "match": {
                  "man.name": "peter"    #子字段
                }
              }
            }
          }
        }
        View Code 
      • has_child
      • has_parent
    
    
    
  • 相关阅读:
    bbs树形打印(一)
    ORM
    kafka的分区
    Content-type"是"application/json的作用
    idea增强for循环
    rabbitmq的发布订阅
    搭建mqtt服务器apollo
    kafka生产者集群和分区,消费者轮询接收
    http实时推送技术
    Kafka:Configured broker.id 2 doesn't match stored broker.id 0 in meta.properties.
  • 原文地址:https://www.cnblogs.com/xiaobaozi-95/p/9182330.html
Copyright © 2011-2022 走看看