• Elasticsearch全文搜索技术之二kibana的简介和使用


    1,课程回顾
    2,本章重点
    3,具体内容

    3.1 kibana的简介和使用
    简介:
    Kibana 是通向 Elastic 产品集的窗口。 它可以在 Elasticsearch 中对数据进行视觉探索和实时分析。 Kibana是一个针对Elasticsearch的开源分析及可视化平台,用来搜索,查看,用来交互存储在Elasticsearch索引中的数据。使用Kibana,可以通过各种图表进行高级数据分析及展示。
    解压配置:
    下载上传文件到虚拟机
    解压:
    tar -xzvf kibana-6.4.0-linux-x86_64.tar.gz -C /usr/
    改名:
    mv /usr/kibana-6.4.0-linux-x86_64/ /usr/kibana
    配置环境变量:
    vim /etc/profile
    source /etc/profile
    修改配置:
    cd /usr/kibana
    vim config/kibana.yml
    :2 端口号 :7 主机IP :28 es url地址

      启动访问(不兼容360浏览器):  
                 kibana  (配置过环境变量,非守护运行)   ctrl+c 直接关闭
                 kibana  &  (守护方式运行)  关闭麻烦点
                 ps -ef |grep kibana    查找进程  
                 kill -9   xxxx  杀死进程
    
    • 1
    • 2
    • 3
    • 4
    • 5

    启动完毕,显示:
    server running at http://192.168.23.211:5601
    3.2 创建,修改和删除索引及数据的增删改查:
    数据类型
    核心类型(Core datatype)
    字符串:string,string类型包含 text 和 keyword。

    text:该类型被用来索引长文本,在创建索引前会将这些文本进行分词,转化为词的组合,建立 索引;允许es来检索这些词,text类型不能用来排序和聚合。例如电子邮件主体部分或者一款产品的介绍

    keyword:该类型不需要进行分词,可以被用来检索过滤、排序和聚合, 可以满足电子邮箱地址、主机名、状态码、邮政编码和标签等数据的要求。

    数值型:long、integer、short、byte、double、float

    日期型:date

    布尔型:boolean

    二进制型:binary
    https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html
    索引的管理:
    创建索引的语法
    PUT /my_index
    {
      “settings”: { … any settings … },
      “mappings”: {
        “type_one”: { … any mappings … },
        “type_two”: { … any mappings … },//6.0之前的版本可以
        …
      }
    }
    mappings: 映射(Mapping) 相当于数据表的表结构

    number_of_shards:每个索引的主分片数,这个配置在索引创建后不能修改。默认值为5
    number_of_replicas:每个主分片的副本数,默认值是 1 。对于活动的索引库,这个配置可以随时修改。
    创建索引的示例
     
    PUT /index_hr
    {    
        "settings":{
         "number_of_shards":3,
         "number_of_replicas":1
        },
        "mappings":{
         "emp_type":{
         "properties":{
         "empno":{
         "type":"integer"
         },
         "ename":{
         "type":"keyword"
         },
         "job":{
         "type":"keyword"
         },
         "salary":{
         "type":"double"
         },
         "deptno":{
         "type":"integer"
         },
         "hiredate":{
         "type":"date"
         }
         }
         }
        }
    }
     
    成功返回:
    {"acknowledged":true,"shards_acknowledged":true,"index":"my_index"}
    
     GET _all/   查看所有索引
     GET _all/_settings   查看索引配置
    
    创建索引时如果503(master_not_discovered_exception)
    删除集群各节点下的data下的数据
    rm -rf  /usr/elasticsearch/data/*
    重启集群
        
    2、修改索引
    PUT /my_index/_settings
    {
      "number_of_replicas": 1
    }
    {"acknowledged":true}
    
    3、删除索引
    
    DELETE /my_index    //删除单个索引
    DELETE /index_one,index_two  //删除多个索引
    DELETE /index_*   //删除以index_开头的索引
    DELETE /_all    //删除所有索引
        {"acknowledged":true}
    可以设置下面的属性,使DELETE /_all 失效,必须指定索引名称,才可以删除。
    elasticsearch.yml
    action.destructive_requires_name: true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    数据管理:
    1,插入数据
    指定ID插入:
    post 192.168.23.30:9200/my_index/my_doc/1
    {
    “my_field”:“aaa”
    }

    返回:
    {"_index":"my_index","_type":"my_type","_id":"1","_version":1,"result":"created","_shards":{"total":1,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}
         不指定ID插入:
    
     
    返回:
    {"_index":"my_index","_type":"my_type","_id":"s40HiGwBg5JLnGO8RVwg","_version":1,"result":"created","_shards":{"total":1,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1}
    2,查询数据:
       查询所有
         /index_hr/_search   注意传递的参数{}	
    
    带参数查询:
      get  192.168.23.30:9200/index_hr/_search?q=ename:lisi
    
      指定id查询:
         get  192.168.23.30:9200/index_hr/emp_type/2
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    3,更新数据
    根据ID更新

    post 192.168.182.66:9200/index_hr/emp_type/1

       自动生成ID更新
    post   192.168.23.30:9200/my_index/my_type/s40HiGwBg5JLnGO8RVwg
    
    • 1
    • 2

    4 ,删除数据
    根据id删除

    delete 192.168.182.66:9200/index_hr/emp_type/1
    再次查询
    {“_index”:“my_index”,“_type”:“my_type”,“_id”:“s40HiGwBg5JLnGO8RVwg”,“found”:false}

    3.7 springboot整合es
    pom.xml(springboot版本不使用最新的 建议:2.1.6.RELEASE)


    org.springframework.boot
    spring-boot-starter-data-elasticsearch

      application.properties 配置:
    
    • 1

    spring.data.elasticsearch.cluster-name=my-application
    #9200 http请求端口
    #9300 java 链接es的端口
    spring.data.elasticsearch.cluster-nodes=192.168.23.81:9300

      entity:
    
    • 1

    package com.aaa.sbm.entity;

    import org.springframework.data.annotation.Id;
    import org.springframework.data.elasticsearch.annotations.Document;

    import java.util.Date;

    /**

    • fileName:Order

    • description:

    • author:zz

    • createTime:2019/8/16 9:39

    • version:1.0.0
      */
      @Document(indexName =“index_order”,type = “order_type”,shards = 3,replicas = 1)
      public class Order {

      @Id
      private long id;
      private String orderNo;
      private String orderName;
      private String orderTime;
      private Integer customId;

      public long getId() {
      return id;
      }

      public void setId(long id) {
      this.id = id;
      }

      public String getOrderNo() {
      return orderNo;
      }

      public void setOrderNo(String orderNo) {
      this.orderNo = orderNo;
      }

      public String getOrderName() {
      return orderName;
      }

      public void setOrderName(String orderName) {
      this.orderName = orderName;
      }

      public String getOrderTime() {
      return orderTime;
      }

      public void setOrderTime(String orderTime) {
      this.orderTime = orderTime;
      }

      public Integer getCustomId() {
      return customId;
      }

      public void setCustomId(Integer customId) {
      this.customId = customId;
      }

      @Override
      public String toString() {
      return “Order{” +
      “id=” + id +
      “, orderNo='” + orderNo + ‘’’ +
      “, orderName='” + orderName + ‘’’ +
      “, orderTime=” + orderTime +
      “, customId=” + customId +
      ‘}’;
      }
      }

    dao:
    package com.aaa.sbm.esdao;

    import com.aaa.sbm.entity.Order;
    import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

    /**

    • fileName:OrderEsDao
    • description:
    • author:zz
    • createTime:2019/8/16 9:53
    • version:1.0.0
      */
      public interface OrderEsDao extends ElasticsearchRepository {
      }

    service:

    package com.aaa.sbm.service;

    import com.aaa.sbm.entity.Order;

    import java.util.List;

    /**

    • fileName:OrderService

    • description:

    • author:zz

    • createTime:2019/8/16 9:59

    • version:1.0.0
      */
      public interface OrderService {

      /**

      • 带参查询order列表
      • @param param
      • @return
        */
        List getList(String param,int type);

      /**

      • 向索引中添加数据
      • @param order
      • @return
        /
        int save(Order order);
        /
        *
      • 向索引中修改数据
      • @param order
      • @return
        /
        int update(Order order);
        /
        *
      • 向索引中删除数据
      • @id
      • @return
        */
        int delete(long id);
        }

    package com.aaa.sbm.service;

    import com.aaa.sbm.entity.Order;
    import com.aaa.sbm.esdao.OrderEsDao;
    import org.elasticsearch.index.query.QueryBuilder;
    import org.elasticsearch.index.query.QueryBuilders;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.util.StringUtils;

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;

    /**

    • fileName:OrderServiceImpl

    • description:

    • author:zz

    • createTime:2019/8/16 10:29

    • version:1.0.0
      */
      @Service
      public class OrderServiceImpl implements OrderService {

      @Autowired
      private OrderEsDao orderEsDao;

      @Override
      public List getList(String param,int type) {

       QueryBuilder queryBuilder = null;
       if(type==0){
           //查询所有
           queryBuilder = QueryBuilders.matchAllQuery();
       }else if(type==1&&!StringUtils.isEmpty(param)){ //查询匹配的单个列的值
           //类似于mybatis中   orderName =#{param}
           queryBuilder = QueryBuilders.matchQuery("orderName",param);
       }else if(type==2&&!StringUtils.isEmpty(param)){//查询order中所有为字符串的匹配值
           // 类似于mybatis中   (orderNo =#{param}  or orderName =#{param})
           queryBuilder = QueryBuilders.multiMatchQuery(param,"orderNo","orderName",);
       }else if(type==3&&!StringUtils.isEmpty(param)){//单列模糊查询
           //类似于mybatis中   orderName like '%${param}%'
           queryBuilder = QueryBuilders.wildcardQuery("orderName","*"+param+"*");
       }
       //获取order对象迭代集合
       Iterable search = orderEsDao.search(queryBuilder);
       //返回迭代器
       Iterator iterator =   search.iterator();
       //定义返回对象
       List  orders = new ArrayList<>();
       //迭代对象
       while(iterator.hasNext()){
           orders.add(iterator.next());
       }
       return orders;
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25

      }

      @Override
      public int save(Order order) {
      //向es中存储对象
      Order rorder = orderEsDao.save(order);
      if(rorder!=null)
      return 1;
      return 0;
      }

      @Override
      public int update(Order order) {
      //向es中存储对象
      Order rorder = orderEsDao.save(order);
      if(rorder!=null)
      return 1;
      return 0;
      }

      @Override
      public int delete(long id) {
      try {
      orderEsDao.deleteById(id);
      return 1;
      }catch (Exception e){
      e.printStackTrace();
      }
      return 0;
      }
      }

    controller:
    package com.aaa.sbm.controller;

    import com.aaa.sbm.entity.Order;
    import com.aaa.sbm.service.OrderService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;

    /**

    • fileName:OrderController

    • description:

    • author:zz

    • createTime:2019/8/16 10:59

    • version:1.0.0
      */
      @RestController
      public class OrderController {

      @Autowired
      private OrderService orderService;

      /**

      • es的order 索引查询
      • @param name
      • @param type
      • @return
        */
        @RequestMapping(“orderList”)
        public Object list(@RequestParam(required = true,defaultValue =“”) String name,@RequestParam(required = true,defaultValue =“0”) Integer type){
        return orderService.getList(name,type);
        }

      /**

      • order索引添加
      • @param order
      • @return
        */
        @RequestMapping(“orderAdd”)
        public Object add(Order order){
        return orderService.save(order);
        }

      /**

      • order索引更新
      • @param order
      • @return
        */
        @RequestMapping(“orderUpdate”)
        public Object update(Order order){
        return orderService.save(order);
        }

      /**

      • order索引值的删除
      • @param id
      • @return
        */
        @RequestMapping(“orderDelete”)
        public Object delete(Long id){
        return orderService.delete(id);
        }

    }

    启动类:
    package com.aaa.sbm;

    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

    @SpringBootApplication
    @MapperScan(“com.aaa.sbm.dao”)
    @EnableElasticsearchRepositories(“com.aaa.sbm.esdao”)
    public class SpringbootMabatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMabatisApplication.class, args);
    }
    
    • 1
    • 2
    • 3

    }

    测试
    http://localhost:8888/orderList
    //查询时,参数为小写
    http://localhost:8888/orderList?name=orderb&type=1
    http://localhost:8888/orderList?name=ord10002&type=2
    http://localhost:8888/orderList?name=orderb&type=3

    http://localhost:8888/orderAdd?id=4&orderNo=ord100022&orderName=orderaa&orderTime=2019-08-16&customId=2

    http://localhost:8888/orderUpdate?id=4&orderNo=ord100022&orderName=orderaa&orderTime=2019-08-16&customId=2

    http://localhost:8888/orderDelete?id=4

    4,知识点总结
    5,本章面试题

  • 相关阅读:
    神经网络(十)激活函数DLC
    四只股票的收盘价可视化
    减少乘法次数的优化算法(Gauss、Strassen、Winograd)
    期末复习 C语言再学习
    OpenShift 4 - 用 Percona XtraDB Cluster 在 OpenShift 部署运行 MySQL 多副本集群
    【完整解题】2023年第四届MathorCup高校数学建模挑战赛——大数据竞赛B题 思路代码文章电商零售商家需求预测及库存优化问题
    Elasticsearch索引yellow修复
    flink1.13.2 Streaming File Sink产生大量orc小文件的问题解决方案
    周期性配送管理系统源码-带多端小程序(自研产品)
    【PAT甲级】1027 Colors in Mars
  • 原文地址:https://blog.csdn.net/zhangchen124/article/details/126557422