美文网首页Elasticsearch Java
Elasticsearch7.*版本 1.入门

Elasticsearch7.*版本 1.入门

作者: 第二套广播体操 | 来源:发表于2019-07-29 14:55 被阅读0次

    马桶🚽Java 上厕所就能看完的Java小知识! 欢迎关注、点赞 每周持续更新!
    es版本变化剧烈 国内关于7.*版本的教学很少 我是个初学者 经过自己的摸索 总结出一份基于7版本的入门教程
    帮助和我一样的同学从7入门 免于各种错误提示之苦 (本文只提供语法的正确性 如果觉得知识不全 可查看其他人前几代es的教程)
    本文结合黑马《Elasticsearch》笔记与JAVA-ElasticSearch入门到放弃(es6) 侵删
    大多数技术笔记在此处更新 欢迎您指出错误

    ES安装

    https://www.elastic.co/cn/downloads/elasticsearch
    1.下载好对应的文件压缩包
    2.解压以后 在bin目录下打开elasticsearch.bat 即可启动(注意:启动es会吃很多内存,内存不够需要修改就找到config文件目录下的jvm.options,打开找到(Xms:代表最小2G,Xmx代表最大2G),修改最小为200m,运行内存会变小。)
    3.验证:访问:http://localhost:9200 默认端口

    image
    如果出现如上图所示结果 即为成功

    elasticsearch.yml

    可配置信息

    属性名 说明
    cluster.name 配置elasticsearch的集群名称,默认是elasticsearch。建议修改成一个有意义的名称。
    node.name 节点名,es会默认随机指定一个名字,建议指定一个有意义的名称,方便管理
    path.conf 设置配置文件的存储路径,tar或zip包安装默认在es根目录下的config文件夹,rpm安装默认在/etc/ elasticsearch
    path.data 设置索引数据的存储路径,默认是es根目录下的data文件夹,可以设置多个存储路径,用逗号隔开
    path.logs 设置日志文件的存储路径,默认是es根目录下的logs文件夹
    path.plugins 设置插件的存放路径,默认是es根目录下的plugins文件夹
    bootstrap.memory_lock 设置为true可以锁住ES使用的内存,避免内存进行swap
    network.host 设置bind_host和publish_host,设置为0.0.0.0允许外网访问
    http.port 设置对外服务的http端口,默认为9200。
    transport.tcp.port 集群结点之间通信端口
    discovery.zen.ping.timeout 设置ES自动发现节点连接超时的时间,默认为3秒,如果网络延迟高可设置大些
    discovery.zen.minimum_master_nodes 主结点数量的最少值 ,此值的公式为:(master_eligible_nodes / 2) + 1 ,比如:有3个符合要求的主结点,那么这里要设置为2
    image

    9300:集群节点间通讯接口
    9200:客户端访问接口

    Kibana安装

    优秀的ES图形化管理工具 有包括控制台和图表显示等简易操作
    1.Kibana下载地址:https://www.elastic.co/downloads/kibana
    2.解压并启动Kibana bin\kibana.bat(注意:使用Kibana要联网,不然一直出现warning错误)
    3.默认访问地址:http://localhost:5601

    image
    如图所示界面 即为成功

    Discover:可视化查询分析器
    Visualize:统计分析图表
    Dashboard:自定义主面板(添加图表)
    Timelion:Timelion是一个kibana时间序列展示组件(暂时不用)
    Dev Tools:Console控制台(同CURL/POSTER,操作ES代码工具,代码提示,很方便)
    Management:管理索引库(index)、已保存的搜索和可视化结果(save objects)、设置 kibana 服务器属性。

    IK分词器

    中文分词器,同lucene一样,在使用中文全文检索前,需要集成IK分词器。
    https://github.com/medcl/elasticsearch-analysis-ik/releases 找到和你es对应版本 并下载zip文件
    解压后的文件放置在es/plugins
    重启es测试

    POST _analyze
    {
      "analyzer":"ik_smart",
      "text":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"
    }
    
    image

    产生如图结果即为成功

    使用入门

    本质上也是储存数据 所以许多概念与Mysql类似
    索引(indices)--------------------------------数据库

    文档(Document)----------------Row 行

    字段(Field)-------------------Columns 列

    概念 说明
    索引库(indices) indices是index的复数,代表许多的索引,
    文档(document) 存入索引库原始的数据。比如每一条商品信息,就是一个文档
    字段(field) 文档中的属性
    映射配置(mappings) 字段的数据类型、属性、是否索引、是否存储等特性
    • 索引集(Indices,index的复数):逻辑上的完整索引
    • 分片(shard):数据拆分后的各个部分
    • 副本(replica):每个分片的复制

    要注意的是:Elasticsearch本身就是分布式的,因此即便你只有一个节点,Elasticsearch默认也会对你的数据进行分片和副本操作,当你向集群添加新数据时,数据也会在新加入的节点中进行平衡。

    创建索引库(数据库)

    "number_of_shards": 1 分片数
    "number_of_replicas": 1 副本数

    PUT people
    {
      "settings": {
        "number_of_shards": 1,
        "number_of_replicas": 1
      }
    
    }
    

    返回结果

    {
      "acknowledged" : true,
      "shards_acknowledged" : true,
      "index" : "people"
    }
    

    删索引库

    DELETE people
    

    查询索引库

    GET  people
    
    结果:
    
    {
      "people" : {
        "aliases" : { },
        "mappings" : { },
        "settings" : {
          "index" : {
            "creation_date" : "1564308107803",
            "number_of_shards" : "1",
            "number_of_replicas" : "1",
            "uuid" : "zQYt9-BXR9yuQOLNugAabg",
            "version" : {
              "created" : "7020099"
            },
            "provided_name" : "item"
          }
        }
      }
    }
    
    

    查询索引库是否存在

    HEAD people
    
    返回
    
    200 - OK
    

    创建映射关系(数据库列)

    • type:类型,可以是text、long、short、date、integer、object等
    • index:是否索引,默认为true
    • store:是否存储,默认为false
    • analyzer:分词器,这里的ik_max_word即使用ik分词器
    PUT people/_mapping
    {
      "properties": {
        "name": {
          "type": "text",
          "analyzer": "ik_max_word"
        },
        "age": {
          "type": "integer",
          "index": "false"
        },
        "sex": {
          "type": "keyword"
        }
      }
    }
    

    如果再次创建不通话映射则为新增映射

    查询映射关系

    GET people/_mapping
    

    返回结果

    {
      "people" : {
        "mappings" : {
          "properties" : {
            "age" : {
              "type" : "integer",
              "index" : false
            },
            "name" : {
              "type" : "text",
              "analyzer" : "ik_max_word"
            },
            "sex" : {
              "type" : "keyword"
            }
          }
        }
      }
    }
    

    添加 更新数据

    我们也可以直接添加数据 这样系统会给我们默认生成一套映射关系
    如果_doc 后面不指定id数 会默认生成随机id

    POST people/_doc/1
    {
      "name" : "小明",
      "age" : 18,
      "sex": "男"
    }
    

    使用相同的代码更改部分 即可更改

    POST people/_doc/1
    {
      "name" : "小红",
      "age" : 18,
      "sex": "女"
    }
    

    通过查询获取id 然后更新即可

    获取数据

    GET people/_doc/1
    
    或者查询所有
    
    GET people/_search 
    
    {
      "_index" : "people",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 3,
      "_seq_no" : 2,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "name" : "小红",
        "age" : 18,
        "sex" : "女"
      }
    }
    

    删除数据

    通过查询id进行删除即可

    DELETE people/_doc/1
    

    查询功能

    我们添加小王2 和小男

    POST people/_doc/2
    {
      "name" : "小王2",
      "age" : 22,
      "sex": "男"
    }
    
    POST people/_doc/3
    {
      "name" : "小男",
      "age" : 22,
      "sex": "男"
    }
    
    

    全部查询

    基本语法

    GET /索引库名/_search
    {
        "query":{
            "查询类型":{
                "查询条件":"查询条件值"
            }
        }
    }
    

    返回结果

    time_out:是否超时
    _shards:分片信息
    hits:搜索结果总览对象
    total:搜索到的总条数
    max_score:所有结果中文档得分的最高分
    hits:搜索结果的文档对象数组,每个元素是一条搜索到的文档信息
    _index:索引库
    _type:文档类型
    _id:文档id
    _score:文档得分
    _source:文档的源数据
    

    默认查询所有的people下的数据

    GET people/_search
    

    返回结果

    {
      "took" : 0,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 2,
          "relation" : "eq"
        },
        "max_score" : 1.0,
        "hits" : [
          {
            "_index" : "people",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 1.0,
            "_source" : {
              "name" : "小红",
              "age" : 18,
              "sex" : "女"
            }
          },
          {
            "_index" : "people",
            "_type" : "_doc",
            "_id" : "-idVPGwBetwbYuu2n6UB",
            "_score" : 1.0,
            "_source" : {
              "name" : "小王",
              "age" : 22,
              "sex" : "男"
            }
          }
        ]
      }
    }
    

    查询所有的people下的数据

    GET people/_search
    {
      "query": {
        "match_all": {}
      }
    }
    

    匹配查询

    查询name小王2的字段 默认情况为or (即包含小王字段的也会查询到)

    GET people/_search
    {
      "query": {
        "match": {
          "name": "小王2"
        }
      }
    }
    

    返回结果(部分)

      "hits" : [
          {
            "_index" : "people",
            "_type" : "_doc",
            "_id" : "-idVPGwBetwbYuu2n6UB",
            "_score" : 0.52354836,
            "_source" : {
              "name" : "小王",
              "age" : 22,
              "sex" : "男"
            }
          },
          {
            "_index" : "people",
            "_type" : "_doc",
            "_id" : "2",
            "_score" : 0.39019167,
            "_source" : {
              "name" : "小王2",
              "age" : 22,
              "sex" : "男"
            }
          }
        ]
    

    如果我们只想要小王2 则通过更改关联关系(此时我们只能查询到小王2)

    GET people/_search
    {
      "query": {
        "match": {
          "name":{"query": "小王2","operator": "and"}
        }
      }
    }
    

    设置最小匹配度查询

    如果满足其中的50% 岂可视为查询正确 小王和小王2都会查询出来
    如果设置为100% 则只会查出小王2

    GET people/_search
    {
      "query": {
        "match": {
          "name":{"query": "小王2","minimum_should_match": "50%"}
        }
      }
    }
    

    多字段查询

    我们在sexname中查询带 “男”字 的信息

    GET people/_search
    {
      "query": {
        "multi_match": {
          "query": "男"
          , "fields": ["name","sex"]
        }
      }
    }
    

    返回结果

    "hits" : [
          {
            "_index" : "people",
            "_type" : "_doc",
            "_id" : "-idVPGwBetwbYuu2n6UB",
            "_score" : 0.2876821,
            "_source" : {
              "name" : "小王",
              "age" : 22,
              "sex" : "男"
            }
          },
          {
            "_index" : "people",
            "_type" : "_doc",
            "_id" : "2",
            "_score" : 0.2876821,
            "_source" : {
              "name" : "小王2",
              "age" : 22,
              "sex" : "男"
            }
          },
          {
            "_index" : "people",
            "_type" : "_doc",
            "_id" : "3",
            "_score" : 0.2876821,
            "_source" : {
              "name" : "小男",
              "age" : 22,
              "sex" : "男"
            }
          }
        ]
    

    词条匹配(精确查找)

    term 查询被用于精确值 匹配,这些精确值可能是数字、时间、布尔或者那些未分词的字符串
    terms 就是查询多个

    GET people/_search
    {
     "query": {
       "terms": {
         "sex": [
           "男","女"
         ]
       }
     }
    

    结果(部分)

    "hits" : [
        {
          "_index" : "people",
          "_type" : "_doc",
          "_id" : "1",
          "_score" : 1.0,
          "_source" : {
            "name" : "小红",
            "age" : 18,
            "sex" : "女"
          }
        },
        {
          "_index" : "people",
          "_type" : "_doc",
          "_id" : "-idVPGwBetwbYuu2n6UB",
          "_score" : 1.0,
          "_source" : {
            "name" : "小王",
            "age" : 22,
            "sex" : "男"
          }
        },
        {
          "_index" : "people",
          "_type" : "_doc",
          "_id" : "2",
          "_score" : 1.0,
          "_source" : {
            "name" : "小王2",
            "age" : 22,
            "sex" : "男"
          }
        },
        {
          "_index" : "people",
          "_type" : "_doc",
          "_id" : "3",
          "_score" : 1.0,
          "_source" : {
            "name" : "小男",
            "age" : 22,
            "sex" : "男"
          }
        }
      ]
    

    结果过滤

    我们只需要name字段

    GET people/_search
     {
       
       "query": {
         "terms": {
           "sex": [
             "男","女"
           ]
         }
       },"_source": "name"
     }
    

    我们不需要带有age 的信息

    GET people/_search
     {
       
       "query": {
         "terms": {
           "sex": [
             "男","女"
           ]
         }
       },"_source": {
         "excludes": "age"
       }
     }
    

    我们只需要性别和名字

      GET people/_search
      {
        
        "query": {
          "terms": {
            "sex": [
              "男","女"
            ]
          }
        },"_source": {
          "includes": ["name","sex"]
        }
      }
    

    返回结果(部分)

     "hits" : [
          {
            "_index" : "people",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 1.0,
            "_source" : {
              "sex" : "女",
              "name" : "小红"
            }
          },
          {
            "_index" : "people",
            "_type" : "_doc",
            "_id" : "-idVPGwBetwbYuu2n6UB",
            "_score" : 1.0,
            "_source" : {
              "sex" : "男",
              "name" : "小王"
            }
          },
          {
            "_index" : "people",
            "_type" : "_doc",
            "_id" : "2",
            "_score" : 1.0,
            "_source" : {
              "sex" : "男",
              "name" : "小王2"
            }
          },
          {
            "_index" : "people",
            "_type" : "_doc",
            "_id" : "3",
            "_score" : 1.0,
            "_source" : {
              "sex" : "男",
              "name" : "小男"
            }
          }
        ]
    

    相关文章

      网友评论

        本文标题:Elasticsearch7.*版本 1.入门

        本文链接:https://www.haomeiwen.com/subject/brybrctx.html