elasticsearch2

Posted rbcd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了elasticsearch2相关的知识,希望对你有一定的参考价值。

聚合查询(aggs)
#用于进行聚合的字段必须是exact value,分词字段不可进行聚合,对于text字段如果需要使用聚合,需要开启fielddata,但是通常不建议;

#聚合查询分类
#1.分桶聚合(Bucket agregations):类比SQL中的group by的作用,主要用于统计不同类型数据的数量
#2.指标聚合(Metrics agregations):主要用于最大值、最小值、平均值、字段之和等指标的统计
#3.管道聚合(Pipeline agregations):用于对聚合的结果进行二次聚合,如要统计绑定数量最多的标签bucket,就是要先按照标签进行分桶,再在分桶的结果上计算最大值。

#语法
GET product/_search

  "aggs": 
    #聚合函数的名称
    "<aggs_name>": 
      #agg_type:聚合种类,比如是桶聚合(terms)或者是指标聚合(avg、sum、min、max等)
      "<agg_type>": 
        #字段名称或者叫域名
        "field": "<field_name>"
      
    
  

桶聚合测试数据
## 数据
PUT product

  "mappings" : 
      "properties" : 
        "createtime" : 
          "type" : "date"
        ,
        "date" : 
          "type" : "date"
        ,
        "desc" : 
          "type" : "text",
          "fields" : 
            "keyword" : 
              "type" : "keyword",
              "ignore_above" : 256
            
          ,
          "analyzer":"ik_max_word"
        ,
        "lv" : 
          "type" : "text",
          "fields" : 
            "keyword" : 
              "type" : "keyword",
              "ignore_above" : 256
            
          
        ,
        "name" : 
          "type" : "text",
          "analyzer":"ik_max_word",
          "fields" : 
            "keyword" : 
              "type" : "keyword",
              "ignore_above" : 256
            
          
        ,
        "price" : 
          "type" : "long"
        ,
        "tags" : 
          "type" : "text",
          "fields" : 
            "keyword" : 
              "type" : "keyword",
              "ignore_above" : 256
            
          
        ,
        "type" : 
          "type" : "text",
          "fields" : 
            "keyword" : 
              "type" : "keyword",
              "ignore_above" : 256
            
          
        
      
    

PUT /product/_doc/1

    "name" : "小米手机",
    "desc" :  "手机中的战斗机",
    "price" :  3999,
    "lv":"旗舰机",
    "type":"手机",
    "createtime":"2020-10-01T08:00:00Z",
    "tags": [ "性价比", "发烧", "不卡顿" ]

PUT /product/_doc/2

    "name" : "小米NFC手机",
    "desc" :  "支持全功能NFC,手机中的滑翔机",
    "price" :  4999,
        "lv":"旗舰机",
    "type":"手机",
    "createtime":"2020-05-21T08:00:00Z",
    "tags": [ "性价比", "发烧", "公交卡" ]

PUT /product/_doc/3

    "name" : "NFC手机",
    "desc" :  "手机中的轰炸机",
    "price" :  2999,
        "lv":"高端机",
    "type":"手机",
    "createtime":"2020-06-20",
    "tags": [ "性价比", "快充", "门禁卡" ]

PUT /product/_doc/4

    "name" : "小米耳机",
    "desc" :  "耳机中的黄焖鸡",
    "price" :  999,
        "lv":"百元机",
    "type":"耳机",
    "createtime":"2020-06-23",
    "tags": [ "降噪", "防水", "蓝牙" ]

PUT /product/_doc/5

    "name" : "红米耳机",
    "desc" :  "耳机中的肯德基",
    "price" :  399,
    "type":"耳机",
        "lv":"百元机",
    "createtime":"2020-07-20",
    "tags": [ "防火", "低音炮", "听声辨位" ]

PUT /product/_doc/6

    "name" : "小米手机10",
    "desc" :  "充电贼快掉电更快,超级无敌望远镜,高刷电竞屏",
    "price" :  "",
        "lv":"旗舰机",
    "type":"手机",
    "createtime":"2020-07-27",
    "tags": [ "120HZ刷新率", "120W快充", "120倍变焦" ]

PUT /product/_doc/7

    "name" : "挨炮 SE2",
    "desc" :  "除了CPU,一无是处",
    "price" :  "3299",
        "lv":"旗舰机",
    "type":"手机",
    "createtime":"2020-07-21",
    "tags": [ "割韭菜", "割韭菜", "割新韭菜" ]

PUT /product/_doc/8

    "name" : "XS Max",
    "desc" :  "听说要出新款12手机了,终于可以换掉手中的4S了",
    "price" :  4399,
        "lv":"旗舰机",
    "type":"手机",
    "createtime":"2020-08-19",
    "tags": [ "5V1A", "4G全网通", "大" ]

PUT /product/_doc/9

    "name" : "小米电视",
    "desc" :  "70寸性价比只选,不要一万八,要不要八千八,只要两千九百九十八",
    "price" :  2998,
        "lv":"高端机",
    "type":"耳机",
    "createtime":"2020-08-16",
    "tags": [ "巨馍", "家庭影院", "游戏" ]

PUT /product/_doc/10

    "name" : "红米电视",
    "desc" :  "我比上边那个更划算,我也2998,我也70寸,但是我更好看",
    "price" :  2999,
    "type":"电视",
        "lv":"高端机",
    "createtime":"2020-08-28",
    "tags": [ "大片", "蓝光8K", "超薄" ]

PUT /product/_doc/11

  "name": "红米电视",
  "desc": "我比上边那个更划算,我也2998,我也70寸,但是我更好看",
  "price": 2998,
  "type": "电视",
  "lv": "高端机",
  "createtime": "2020-08-28",
  "tags": [
    "大片",
    "蓝光8K",
    "超薄"
  ]

桶聚合查询
## 桶聚合 例:统计不同标签的商品数量
GET product/_search
 #聚合查询
  "aggs": 
    #桶聚合查询
    "tag_bucket": 
      "terms": 
        "field": "tags.keyword"
      
    
  


## 不显示hits数据:size:0
GET product/_search

  "size": 0,
  "aggs": 
    "tag_bucket": 
      "terms": 
        #查询不会被分词
        "field": "tags.keyword"
      
    
  

## 排序
GET product/_search

  "size": 0,
  "aggs": 
    "tag_bucket": 
      "terms": 
        "field": "tags.keyword",
        "size": 3,
        "order": 
          "_count": "desc"
        
      
    
  


## doc_values和field_data
GET product/_search

  "size": 0,
  "aggs": 
    "tag_bucket": 
      "terms": 
        "field": "name"
      
    
  

GET product/_search

  "size": 0,
  "aggs": 
    "tag_bucket": 
      "terms": 
        "field": "name.keyword"
      
    
  

POST product/_mapping

  "properties": 
    "name": 
      "type": "text",
      "analyzer": "ik_max_word",
      #通过修改fielddata为true来支持文本字段桶聚合查询
      "fielddata": true
    
  

GET product/_search

  "size": 0,
  "aggs": 
    "tag_bucket": 
      "terms": 
        "size": 20,
        "field": "name"
      
    
  

桶指标聚合查询
## 指标聚合
## 例:最贵、最便宜和平均价格三个指标
GET product/_search

  "size": 0,
  "aggs": 
    "max_price": 
      "max": 
        "field": "price"
      
    ,
    "min_price": 
      "min": 
        "field": "price"
      
    ,
    "avg_price": 
      "avg": 
        "field": "price"
      
    
  

## 单个聚合查询所有指标
GET product/_search

  "size": 0,
  "aggs": 
    "price_stats": 
      "stats": 
        "field": "price"
      
    
  

##按照name去重的数量
GET product/_search

  "size": 0,
  "aggs": 
    "type_count": 
      "cardinality": 
        "field": "name"
      
    
  

GET product/_search

  "size": 0, 
  "aggs": 
    "type_count": 
      "cardinality": 
        "field": "name.keyword"
      
    
  

##对type计算去重后数量
GET product/_search

  "size": 0, 
  "aggs": 
    "type_count": 
      "cardinality": 
        "field": "lv.keyword"
      
    
  

##*********************************************
## 管道聚合 二次聚合
## 例:统计平均价格最低的商品分类
GET product/_search

  "size": 0, 
  "aggs": 
    "type_bucket": 
      "terms": 
        "field": "type.keyword"
      ,
      "aggs": 
        "price_bucket": 
          "avg": 
            "field": "price"
          
        
      
    ,
    "min_bucket":
      "min_bucket": 
        "buckets_path": "type_bucket>price_bucket"
      
    
  


Elasticsearch 教程


章节


Elasticsearch是一个高度可伸缩的开源全文搜索引擎。Elasticsearch让你可以快速、实时地存储、搜索和分析大量数据,它通常作为互联网应用的内部搜索引擎,为需要复杂搜索功能的应用提供支持。

下面是Elasticsearch的常用场景:

  • 电商搜索引擎,使用Elasticsearch存储商品与品类信息,提供搜索和搜索建议功能。
  • 日志系统,收集、分析日志数据,可以使用Logstash (Elasticsearch/Logstash/Kibana栈的一部分)来收集、聚合和解析数据,然后让Logstash将这些数据提供给Elasticsearch,通过搜索和聚合计算挖掘有价值的信息。
  • 价格提醒平台,在价格变动时,让用户可以收到通知。抓取供应商的价格,推入Elasticsearch,并使用其反向搜索(Percolator)功能来匹配用户的价格通知设置,找到匹配后将提醒推送给用户。

  • BI(商业智能),分析业务大数据,挖掘有价值的商务信息。可以使用Elasticsearch来存储数据,然后使用Kibana (Elasticsearch/Logstash/Kibana堆栈的一部分)构建自定义仪表板,该仪表板可以可视化显示数据。此外,还可以使用Elasticsearch聚合功能对数据执行复杂的业务智能分析。

本教程将介绍启动和运行Elasticsearch的过程,引导读者学习基本操作,如索引、搜索和修改数据等。学习完本教程,应该对Elasticsearch及其工作原理有一个较好的了解,学会使用Elasticsearch。

以上是关于elasticsearch2的主要内容,如果未能解决你的问题,请参考以下文章

elasticsearch2.4.6安全

Elasticsearch2.2报错问题集锦

elasticsearch2.2 集群搭建各种坑

elasticsearch2

ElasticSearch2.3.1环境搭建哪些不为人知的坑

elasticsearch2.4升级elasticsearch6.2心得