IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> kibana中DevTool工具下的新增修改查询 -> 正文阅读

[大数据]kibana中DevTool工具下的新增修改查询

最近稍微看了下elasticsearch的简单使用,虽然可能在近期的工作中不经常用到,但是还是随笔记录一下


一、新增

1、创建索引

PUT product_db
{
  "mappings": {
    "properties": {
      "id": {
        "type": "long"
      },
      "name": {
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "keywords": {
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "subTitle": {
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "salecount": {
        "type": "long"
      },
      "putawayDate": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      },
      "price": {
        "type": "double"
      },
      "promotionPrice": {
        "type": "keyword"
      },
      "originalPrice": {
        "type": "keyword"
      },
      "pic": {
        "type": "keyword"
      },
      "sale": {
        "type": "long"
      },
      "hasStock": {
        "type": "boolean"
      },
      "brandId": {
        "type": "long"
      },
      "brandName": {
        "type": "keyword"
      },
      "brandImg": {
        "type": "keyword"
      },
      "categoryId": {
        "type": "long"
      },
      "categoryName": {
        "type": "keyword"
      },
      "attrs": {
        "type": "nested",
        "properties": {
          "attrId": {
            "type": "long"
          },
          "attrName": {
            "type": "keyword"
          },
          "attrValue": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

上方语句表示新建一个名称为product_db的索引

可能碰到的问题:

org.elasticsearch.index.mapper.MapperParsingException: analyzer [ik_max_word] not found for field [name]

原因是:当前安装的elasticsearch没有安装ik分词器
解决办法:

  • 进入 elasticsearch 安装目录下的 bin\ 目录,在此目录打开命令行窗口,输入下面代码安装IK分词器插件
    elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/vX.X.X/elasticsearch-analysis-ik-X.X.X.zip

注意:上方指令中的X.X.X表示的是当前安装的elasticsearch的版本号

2、新增数据

PUT /product_db/_doc/1
{
  "id": "26",
  "name": "小米 11 手机",
  "keywords": "小米手机",
  "subTitle": "AI智慧全面屏 6GB +64GB 亮黑色 全网通版 移动联通电信4G手机 双卡双待双卡双待",
  "price": "3999",
  "promotionPrice": "2999",
  "originalPrice": "5999",
  "pic": "http://macro‐oss.oss‐cn‐shenzhen.aliyuncs.com/mall/images/20180615/xiaomi.jpg",
  "sale": 999,
  "hasStock": true,
  "salecount": 999,
  "putawayDate": "2021-04-01",
  "brandId": 6,
  "brandName": "小米",
  "brandImg": "http://macro‐oss.oss‐cn‐shenzhen.aliyuncs.com/mall/images/20190129/1e34aef2a409119018a4c6258e39ecfb_222_222.png",
  "categoryId": 19,
  "categoryName": "手机通讯",
  "attrs": [
    {
      "attrId": 1,
      "attrName": "cpu",
      "attrValue": "2核"
    },
    {
      "attrId": 2,
      "attrName": "颜色",
      "attrValue": "黑色"
    }
  ]
}

上方语句表明在product_db索引下新增一个序号为1的数据文档

二、修改

POST /product_db/_update/1
{
  "doc":{
    "name": "小米12pro hahha"
  }
}

上方的指令表示:将序号为1的文档中属性名为name的属性修改成对应的字符串,相当于:
update product_db set name="小米12pro hahha" where _id=1

三、查询

1、获取索引属性

GET product_db

上方指令表示查询product_db索引的具体属性

2、查询所有数据

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

上方指令表示查询product_db索引下的所有数据,相当于:
select * from product_db

3、分页查询数据

GET product_db/_search
{
  "query": { "match_all": {} },
  "from": 10,
  "size": 10
}

上方指令表示从序号10的文档开始向后查询10条数据,相当于:
select * from product_db where _id >=10 limit 10

  • 注意:如果size没有设置的话,默认是10

4、排序查询数据

GET product_db/_search
{
  "query": { "match_all": {} },
  "sort": { "price": { "order": "desc" } }
}

上方指令表示查询出的数据以price字段值倒序,相当于
select * from product_db order by price desc limit 10

5、查询指定字段数据

GET product_db/_search
{
  "query": { "match_all": {} },
  "_source": ["id", "name", "price"]
}

上方指令表示查询所有数据中的id、name、price三个字段,相当于:
select id, name, price from product_db

6、条件查询

GET product_db/_search
{
  "query": { "match": { "price": 3999 } }
}

上方指令表示只查询price是3999的数据,相当于:
select * from product_db where price=3999

7、模糊查询

只要不是数字类型,使用match都是模糊查询

GET product_db/_search
{
  "query": { "match": { "name": "小米" } }
}

GET product_db/_search
{
  "query": { "match": { "name": "小米 华为" } }
}

上方两条指令分别相当于:
select * from product_db where name like '%小米%'
select * from product_db where name like '%小米' or name like '%华为%'

  • 注意:后面一个指令的小米只是以小米为后缀的模糊查询,而不是任意包含小米字段的模糊查询

8、与、或、非查询

GET product_db/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "name": "小米" } },
        { "match": { "name": "华为" } }
      ],
      "should": [
        { "match": { "name": "苹果" } },
        { "match": { "name": "三星" } }
      ],
      "must_not": [
        { "match": { "price": 2999 } }
      ]
    }
  }
}

上方指令是一个与、或、非组合查询语句,相当于:
select * from product_db where name like '%小米%华为%' or name like '%苹果%三星%' and price != 2999

9、范围查询

GET product_db/_search
{
  "query": {
    "bool": {
      "must": { "match_all": {} },
      "filter": {
        "range": {
          "price": {
            "gte": 2000,
            "lte": 5000
          }
        }
      }
    }
  }
}

上方指令相当于:
select * from product_db where price between 2000 and 5000

10、聚集查询

相当于SQL中的聚集函数,比如分组、求和、求平均数之类的

GET product_db/_search
{
  "size": 0,
  "aggs": {
    "group_by_brandId": {
      "terms": {
        "field": "brandId.keyword"
      }
    }
  }
}

上方指令相当于:
SELECT brandId, COUNT(*) FROM product_db GROUP BY brandId ORDER BY COUNT(*) DESC LIMIT 10;

GET product_db/_search
{
  "size": 0,
  "aggs": {
    "group_by_brandId": {
      "terms": {
        "field": "brandId.keyword"
      },
      "aggs": {
        "average_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}

上方指令相当于:
SELECT brandId, COUNT(*), AVG(price) FROM product_db GROUP BY brandId ORDER BY COUNT(*) DESC LIMIT 10;

GET product_db/_search
{
  "size": 0,
  "aggs": {
    "group_by_salecount": {
      "range": {
        "field": "salecount",
        "ranges": [
          {
            "from": 2000,
            "to": 3000
          },
          {
            "from": 3000,
            "to": 4000
          },
          {
            "from": 4000,
            "to": 5000
          }
        ]
      },
      "aggs": {
        "group_by_brandId": {
          "terms": {
            "field": "brandId.keyword"
          },
          "aggs": {
            "average_balance": {
              "avg": {
                "field": "price"
              }
            }
          }
        }
      }
    }
  }
}

上方指令表示统计销售数量在三个销售数量区间内,各个品牌的平均价格

  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2021-07-14 23:05:55  更:2021-07-14 23:06:43 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/21 0:06:04-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码