Elasticsearch7入门基础语法练习

Elasticsearch7入门基础语法练习,第1张

Elasticsearch7入门基础语法练习

《Elasticsearch中文文档》 | Elasticsearch 技术论坛

elasticsearch-java: Elasticsearch Java API 手册

#创建索引,等于mysql的create table
PUT /df_coal_boilers_dispatch_2022
{
  "mappings" : {
      "dynamic_templates" : [
        {
          "datatime_pattern" : {
            "match" : ".+?time$|.+?Time$|.+?Date$|.+?date$",
            "match_pattern" : "regex",
            "mapping" : {
              "format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||yyyy/MM/dd HH:mm:ss||yyyy/MM/dd HH:mm||yyyy/MM/dd||epoch_millis",
              "type" : "date"
            }
          }
        }
      ],
      "properties" : {
        "baseUrl" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "boiler_type" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "cityArea" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "city_state" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "coal_type" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "comment" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "countryArea" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "county" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "create_time" : {
          "type" : "date",
          "format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||yyyy/MM/dd HH:mm:ss||yyyy/MM/dd HH:mm||yyyy/MM/dd||epoch_millis"
        },
        "discharge_standard" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "dyComment" : {
          "type" : "boolean"
        },
        "dyDisabled" : {
          "type" : "boolean"
        },
        "environmental_construction" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "evaluate_check" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "evaluate_manage" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "fileName" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "in_forbid_area" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "in_supply_head_area" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "in_urban_area" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "monitor_network" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "network_standard" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "operation_time" : {
          "type" : "date",
          "format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||yyyy/MM/dd HH:mm:ss||yyyy/MM/dd HH:mm||yyyy/MM/dd||epoch_millis"
        },
        "orgId" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "org_name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "provinceArea" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "register_code" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "register_time" : {
          "type" : "date",
          "format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||yyyy/MM/dd HH:mm:ss||yyyy/MM/dd HH:mm||yyyy/MM/dd||epoch_millis"
        },
        "remark" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "requestOption" : {
          "properties" : {
            "headers" : {
              "properties" : {
                "Authorization" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                },
                "Content-Type" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                }
              }
            },
            "responseType" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            }
          }
        },
        "smoke_monitor" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "state" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "steam_ton" : {
          "type" : "float"
        },
        "supervision_standard" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "taskEndTime" : {
          "type" : "date",
          "format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||yyyy/MM/dd HH:mm:ss||yyyy/MM/dd HH:mm||yyyy/MM/dd||epoch_millis"
        },
        "taskId" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "taskStartTime" : {
          "type" : "date",
          "format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||yyyy/MM/dd HH:mm:ss||yyyy/MM/dd HH:mm||yyyy/MM/dd||epoch_millis"
        },
        "taskYear" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "update_time" : {
          "type" : "date",
          "format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||yyyy/MM/dd HH:mm:ss||yyyy/MM/dd HH:mm||yyyy/MM/dd||epoch_millis"
        },
        "version" : {
          "type" : "integer"
        }
      }
    }
}
#修改索引,添加字段,等于mysql的修改字段
PUT /df_coal_boilers_dispatch_2022/_mapping
{
  "properties" : {
        "expand_filed_name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
  }    
}
#查看索引信息,desc table
GET /df_coal_boilers_dispatch_2022
#关闭索引,读写阻断
POST /df_coal_boilers_dispatch_2022/_close
#打开索引
POST /df_coal_boilers_dispatch_2022/_open
#冻结索引,只读不能写
POST /df_coal_boilers_dispatch_2022/_freeze
#解冻
POST /df_coal_boilers_dispatch_2022/_unfreeze
#删除索引等于mysql的delete table
DELETE /df_coal_boilers_dispatch_2022
#插入数据,等于 insert into table
POST /df_coal_boilers_dispatch_2022/_doc?pretty=true
{
  "dyComment" : false,
  "fileName" : "i like elasticsearch what about you",
  "city_state" : "622900000000",
  "provinceArea" : "620000000000",
  "county" : "622927000000",
  "orgId" : "f9f2d4ac46ad4f26a9f05d001d50e035",
  "smoke_monitor" : "未安装",
  "update_time" : "2020-09-25 16:51:07",
  "supervision_standard" : "未监测",
  "boiler_type" : "0",
  "state" : "在用",
  "org_name" : "成都小学",
  "create_time" : "2021-06-01 23:10:43",
  "cityArea" : "622900000000",
  "in_supply_head_area" : "是",
  "steam_ton" : "2.5",
  "version" : 1,
  "taskStartTime" : "2021-01-01",
  "in_forbid_area" : "1",
  "environmental_construction" : "无",
  "baseUrl" : "http://125.74.7.101:9006/pr-gs-arev-node-s",
  "operation_time" : "2021-06-01 23:10:43",
  "countryArea" : null,
  "dyDisabled" : false,
  "comment" : "",
  "taskId" : "F0A4021017274CF297AB1867371D5E62",
  "taskEndTime" : "2020-12-30"
}
#批量导入json数据
POST /df_coal_boilers_dispatch_2022/_bulk?pretty --data-binary "@D:datademo.json"

#查看索引数据
GET /df_coal_boilers_dispatch_2022/_search
#查看刚插入的数据select *from table where id = 'xx'
GET /df_coal_boilers_dispatch_2022/_doc/TfZRbH4BadycR9_U1y0L

#单条局部修改,update table set column = 'xx' where id = 'xxx'
POST /df_coal_boilers_dispatch_2022/_update/{id}
{
  "doc":{"state" : "未用"}
}
#按条件批量修改数据,update table set filed = 'xx' where 1=1
POST /df_coal_boilers_dispatch_2022/_update_by_query
{
  "query": {
    "match_all": {}
  },
  "script": {
    "source": "ctx._source.state = 在用",
    "lang": "painless"
  }
}
#按条件批量删除,delete table where 1=1
POST /df_coal_boilers_dispatch_2022/_delete_by_query 
{
  "query": {
    "match_all": {}
  }
}
#查询所有数据
GET _search
{
  "query": {
    "match_all": {}
  }
}
#查询节点
GET /_cat/nodes
#查询节点健康状态
GET /_cat/health?v
#查询所有的索引
GET /_cat/indices/?pretty
#模糊查询索引名
#查询所有df开头的索引
GET /_cat/indices/df*?pretty
#查询某索引的信息
GET /df_coal_boilers_dispatch?pretty
#查询某索引的结构信息
GET /df_coal_boilers_dispatch/_mapping?pretty
#查询某索引的配置信息
GET /df_coal_boilers_dispatch/_settings?pretty
#select *from table
GET /df_coal_boilers_dispatch_2022/_search

#索引结构查询discribe table
GET /df_coal_boilers_dispatch/_mapping

#模糊查询,中文的会分词需要加个keyword,字段设置为keyword则可以不需要,match只能匹配相关度,而分词之间的位置关系却无法保
#fuzziness 允许容错数的,用于短句匹配的时候(莱文斯坦距离),中文可以加keyword,这样可以近似匹配出成都小学出来,成都第七中学不能匹配
GET /df_coal_boilers_dispatch_2022/_search
{
  "version": true, 
  "query": {
    "match": {
      "org_name.keyword": {
        "query": "成都中学",
        "fuzziness": 1
      }
    }   
  }
}
#这样子只要包含成都中学任意一个字的都会匹配出来
GET /df_coal_boilers_dispatch_2022/_search
{
  "version": true, 
  "query": {
    "match": {
      "org_name": {
        "query": "成都中学"
      }
    }   
  }
}
#通配符检索——扫描所有倒排索引, 性能较差
GET /df_coal_boilers_dispatch/_search
{
  "query": {
    "wildcard": {
      "county": {
        "value": "6201*"
      }
    }   
  }
}
#中文通配符检索——扫描所有倒排索引, 性能较差
GET /df_coal_boilers_dispatch/_search
{
  "query": {
    "wildcard": {
      "org_name.keyword": {
        "value": "甘肃*"
      }
    }   
  }
}
#查询*第?中学 性能较差
GET /df_coal_boilers_dispatch/_search
{
  "query": {
    "wildcard": {
      "org_name.keyword": {
        "value": "*第?中学"
      }
    }   
  }
}
#正则匹配——扫描所有倒排索引, 性能较差
GET /df_coal_boilers_dispatch/_search
{
  "query": {
    "regexp": {
      "org_name.keyword": ".*甘肃.*"
    }   
  }
}
#前缀检索,查询"甘肃"开头的--扫描所有倒排索引, 性能较差
GET /df_coal_boilers_dispatch/_search
{
  "query": {
    "prefix": {
      "org_name.keyword": "甘肃"
    }   
  }
}
#fuzzy 模糊查询,单词条容错查询
#value :查询的关键字
#boost: 设置查询的权值,默认是1.0
#fuzziness :设置匹配的最小相似度
#默认值为0.5:对于字符串,取值为0-1(包括0和1);对于数值,取值可能大于1;对于日期型,取值为1d,2d,1m这样,1d就代表1天
#prefix_length:指明区分词项的共同匹配长度,默认是0
#用于处理输入词条拼写错误的简单解决方法,但是有很高的CUP开销精度很低,和match里的fuzziness一样,不常用
GET /df_coal_boilers_dispatch/_search
{
  "query": {
    "fuzzy": {
      "org_name.keyword": {
        "value": "中学",
        "fuzziness": "2"
      }
    }
  }
}
#常用次方式可以简单处理容错词条查询
GET /df_coal_boilers_dispatch/_search
{
  "query": {
    "match": {
      "org_name": {
        "query": "成都中学",
        "fuzziness": "2",
        "operator": "and"
      }
    }
  }
}

#查询索引某字段分词情况URL: http://localhost:9200/{index}/{type}/{id}/_termvectors?fields={fieldName} 
GET /df_coal_boilers_dispatch/_doc/MJGzynkB8PIb1LFSm_8u/_termvectors?fields=org_name

#查询倒排索引存储的值
GET /df_coal_boilers_dispatch/_analyze
{
  "field":"org_name",
  "text":"肖金中学"
}

#精准匹配term是完全匹配检索, 要用在不分词的字段上, 如果某个field在映射中被分词了, term检索将不起作用.所以,不分词的field, 要在mapping中设置为不分词.类似in
GET /df_coal_boilers_dispatch/_search
{
  "query": {
    "terms": {
      "org_name.keyword":["永靖县杨塔乡卫生院","永靖县徐顶乡卫生院"]
    }   
  }
}
#传入词会进行分析并解析为数组,且必须同时满足数组所有的词才会返回结果。slop 则是传入词与词直接的距离,like的一种替代方式,默认分词standard下“中小学”(中移动一位),"中心小学(中移动两位)”也能被匹配出来
#由于match_phrase是在搜索阶段进行的计算,会影响搜索效率,据说比term查询慢20倍,所以不要进行大文本量的字段搜索,尽量进行标题,名字这种不是太长的类型的搜索才使用这个
GET /df_coal_boilers_dispatch_2022/_search
{
  "query": {
    "match_phrase": {
      "org_name": {
        "query": "中学",
        "slop": 2
      }
    }   
  }
}

GET /df_coal_boilers_dispatch/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "org_name": "中学"
          }
        }
      ],
      "should": [
        {
          "match_phrase": {
            "org_name": {
              "query": "中学",
              "slop": 0
            }
          }
        }
      ]
    } 
  }
}
#效率稍高点的做法是先match过滤,在用resource对结果加分
#window_size 是每一分片进行重新评分的顶部文档数量这个只要大于你可能分页的总数*每页的数量即可(pageNumber*pageSize)实际上不需要这么多因为翻页不可能很深,这个根据业务调整即可。
GET /df_coal_boilers_dispatch_2022/_search
{
  "query": {
    "match": {
      "org_name": "中学"
    }  
  },
  "rescore": {
    "query": {
      "rescore_query":{
        "match_phrase": {
          "org_name": {
            "query": "中学",
            "slop": 0
          }
        } 
      }
    },
    "window_size": 30
  }
}
#multi_match查询–可以指定多个字段,实际业务中全文检索用到最多的查询方式就是multi_match语法,该语法参数比较多,用的不好不但效果出不来而且还会影响性能,org_name比evaluate_manage大3倍的权重值,中文会分词,会匹配“第”和“九”
#query 来自用户输入的查询短语
#fields数组,默认支持最大长度1024,可以单独为任意字段设置相关度权重,支持通配符;fields可以为空,为空时会取mapping阶段配置的所有支持term查询的filed组合在一起进行查询
#type 定义内部查询方式和打分方式
#1、best_fields    按照match检索,所有字段单独计算得分并取最高分的field为最终_score,虽然是默认值,但不建议使用,数据量上来后查询性能会下降
#2、most_fields    按照match检索,融合所有field得分为最终_score
#3、cross_fields    将fields中的所有字段融合成一个大字段进行match检索,此时要求所有字段使用相同分析器
#4、phrase    按照match_phrase检索,默认slop为0,执行短语精确匹配,所以即便设置 minimum_should_match 也无效; 取最高字段得分
#5、phrase_prefix    按照match_phrase_prefix检索,滑动步长slop默认为0;取最高字段得分
#6、bool_prefix    按照match_bool_prefix检索
GET /df_coal_boilers_dispatch/_search
{
  "query": {
    "multi_match": {
      "query": "中学",
      "type": "most_fields", 
      "fields": ["org_name^3", "evaluate_manage"]
    }
  },
  "sort":[
     {
       "operation_time": {
         "order":"desc"
       }
     }
    ]
}
#查询多字段中包含“中学”的,like查询的一种替代方案s
GET /df_coal_boilers_dispatch/_search
{
  "query": {
    "multi_match": {
      "query": "中学",
      "fields": ["org_name", "evaluate_manage"],
      "operator": "and"
    }
  }
}
#查询字段中包含“第九”的,like查询的一种替代方案select * from table where org_name like '%第九%'
GET /df_coal_boilers_dispatch/_search
{
  "query": {
    "match": {
      "org_name": {
        "query": "第九",
        "operator": "and"
      }
      
    }
  }
}
#查询字段中匹配第九中学匹配其中50%以上
GET /df_coal_boilers_dispatch/_search
{
  "query": {
    "match": {
      "org_name": {
        "query": "金水中学",
        "operator": "and",
        "minimum_should_match": "50%"
      }
      
    }
  }
}

#通用的范围查询–range gte gt lte lt  查找兰州市下的州县
GET /df_coal_boilers_dispatch/_search
{
  "query": {
     "range": {
       "county": {
         "gte": "620100000000",
         "lt": "620200000000"
       }
     }
  }
}
#通用的范围查询–range gte gt lte lt  查找兰州市下的州县且注册时间在某时间范围内,打分
GET /df_coal_boilers_dispatch/_search
{
  "query": {
     "bool": {
       "must": [
         {
           "range": {
             "county": {
               "gt": "620100000000",
               "lt": "620200000000"
             }
           }
         },
         {
           "range": {
             "register_time": {
               "gt": "2009-01-01",
               "lt": "2009-12-30"
             }
           }
         }
       ]
     }
  }
}
#通用的范围查询–range gte gt lte lt  查找兰州市下的州县且注册时间在某时间范围内,不打分
GET /df_coal_boilers_dispatch/_search
{
  "query": {
     "bool": {
       "filter": [
         {
           "range": {
             "county": {
               "gt": "620100000000",
               "lt": "620200000000"
             }
           }
         },
         {
           "range": {
             "register_time": {
               "gt": "2009-01-01",
               "lt": "2009-12-30"
             }
           }
         }
       ],
       "must": [
         {
           "terms": {
             "state.keyword": ["在用", "使用"]
           }
         }
       ]
     }
  }
}
#混用嵌套select * from table where (taskYear="2020" and state ='在用')  or (taskYear = "2021" and state='弃用')
GET /df_coal_boilers_dispatch/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "taskYear": {
                    "value": "2020"
                  }
                }
              },
              {
                "term": {
                  "state.keyword": {
                    "value": "在用"
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                  "taskYear": {
                    "value": "2021"
                  }
                }
              },
              {
                "term": {
                  "state.keyword": {
                    "value": "弃用"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}
#查询非空,存在该字段,空字符串值的会检索出来
#等于sql: selct * from table where filed_name is not null or filed_name = ''
GET /df_coal_boilers_dispatch_2022/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "exists": {
            "field": "taskYear"
          }
        }
      ]
    }
  }
}
#查询无字段或者字段值为null selct * from table where filed_name is null
GET /df_coal_boilers_dispatch_2022/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "exists": {
            "field": "taskYear"
          }
        }
      ]
    }
  }
}
#ES查询字段为空字符串,不加exists也会查询出不存该字段和该字段为null的数据 selct * from table where filed_name = '' 
GET /df_coal_boilers_dispatch_2022/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "exists": {
            "field": "taskYear"
          }
        }
      ], 
      "must_not": [
        {
          "wildcard": {
            "taskYear": {
              "value": "*"
            }
          }
        }
      ]
    }
  }
}

#按区域统计查找到的总条数
#sql:select city_state,count(city_state) from table where 条件 group by city_count union
# select provinceArea,count(provinceArea) from table where 条件 group by provinceArea
GET /df_coal_boilers_dispatch/_search
{
  "size": 0,
  "aggs": {
    "city_count": {
      "terms": {
        "field": "city_state.keyword"
      }
    },
    "province_count": {
      "terms": {
        "field": "provinceArea.keyword"
      }
    }
  },
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
             "register_time": {
               "gte": "2019-01-01",
               "lte": "2019-12-30"
             }
           }
        }
      ]
    }
  }
}
#平均聚合,没有值按0计算,size=0不返回具体命中数据(返回hits为空),missing为字段缺失或者无值时候给定缺省值,默认情况是直接忽略
POST /df_coal_boilers_dispatch/_search
{
  "query": {
    "match_all": {}
  }, 
  "size": 0, 
  "aggs": {
    "steam_ton_avg": {
      "avg": {
        "field": "steam_ton",
        "missing": 0
      }
    }
  }
}
#需要对平均数进行更新再计算 比如平均数*2,最大最小值
POST /df_coal_boilers_dispatch/_search
{
  "query": {
    "match_all": {}
  }, 
  "size": 0, 
  "aggs": {
    "steam_ton_avg": {
      "avg": {
        "field": "steam_ton",
        "missing": 0,
        "script": {
          "lang": "painless",
          "source": "_value*params.param1",
          "params": {
            "param1": 2
          }
        }
      }
    },
    "steam_ton_max": {
      "max": {
        "field": "steam_ton",
        "missing": 0
      }
    },
    "steam_ton_min": {
      "min": {
        "field": "steam_ton", 
        "script": {
          "source": "_value"
        }
      }
    }
  }
}
#统计聚合stats,返回min,max,sum,count,avg
POST /df_coal_boilers_dispatch/_search
{
  "query": {
    "match_all": {}
  }, 
  "size": 0, 
  "aggs": {
    "steam_ton_count": {
      "stats": {
        "field": "steam_ton"
      }
    }
  }
}
#计算百分比分布percenties
POST /df_coal_boilers_dispatch/_search
{
  "query": {
    "match_all": {}
  }, 
  "size": 0, 
  "aggs": {
    "percent_range_test": {
      "percentiles": {
        "field": "steam_ton",
        "percents": [
          1,
          5,
          25,
          50,
          75,
          95,
          99
        ]
      }
    }
  }
}
#热门聚合,top_hits
#选项:from - 与要获取的第一个结果的偏移量。
#size - 每个存储区要返回的最匹配匹配项的最大数目。默认情况下,返回前三个匹配项。
#sort - 应该如何对最匹配的匹配进行排序。默认情况下,命中按主要查询的分数排序。
GET _search
{
  "query": {
    "match_all": {}
  }, 
  "aggs": {
    "top_hots": {
      "terms": {
        "field": "pageViews",
        "order": {
          "top_hit": "desc"
        }
      },
      "aggs": {
        "top_tags_hits": {
          "top_hits": {
          }
        },
        "top_hit": {
          "max": {
            "script": {
              "source": "_score"
            }
          }
        }
      }
    }
  }
}
#搜索匹配黄河的数据search_type类型query then fetch(默认的搜索方式)DFS query and fetch这种方式比第一种方式多了一个初始化散发(initial scatter)计算全局词频(term frequencies)步骤但性能是最差的,和下面的查询的结果是一样的
GET /smart_search_all/_search?search_type=dfs_query_then_fetch
{
  "query": {
    "multi_match": {
            "query": "黄河",
            "fields": ["title^3","summary"]
          }
  }
}
#搜索匹配黄河的数据,完全匹配的结果在第一个位置select * from table where title = '黄河' or (title like '%黄河%' or summary like '%黄河%')
GET /smart_search_all/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "title": {
              "value": "黄河"
            }
          }
        },
        {
          "multi_match": {
            "query": "黄河",
            "fields": ["title^3","summary"]
          }
        }
      ]
    }
  }
}
#过滤器聚合,查询兰州下的平均吨数,先统计在过滤select a from ( select avg(1) a from table group by field) where field = 'xx'
GET /df_coal_boilers_dispatch/_search
{
  "aggs": {
    "avg_county_ton": {
      "filter": {
        "term": {
          "city_state": "620100000000"
        }
      },
      "aggs": {
        "avg_steam_ton": {
          "avg": {
            "field": "steam_ton"
          }
        }
      }
    }
  },
  "size": 0
}
#查询兰州下的平均吨数,先过滤在统计select avg(1) from table where filed = 'xx'
GET /df_coal_boilers_dispatch/_search
{
  "query": {
    "term": {
      "city_state": {
        "value": "620100000000"
      }
    }
  },
  "size": 0, 
  "aggs": {
        "avg_steam_ton": {
          "avg": {
            "field": "steam_ton"
          }
        }
      }
}
#多过滤器,匿名过滤器类似aggs filter  类似sql case when,select case when steam_ton >=10 then 1 else 0 end as big_ton,case when steam_ton >=5 and steam_ton <10 then 1 else 0 end as middle_ton,case when steam_ton >=1 and steam_ton <5 then 1 else 0 end as small_ton,case when steam_ton is null or steam_ton < 1 then 1 else 0 end as other_ton from table 
GET /df_coal_boilers_dispatch/_search
{
  "size": 0,
  "aggs": {
    "my_cases": {
      "filters": {
        "other_bucket_key": "other_ton", 
        "filters": {
          "big_ton": {
            "range": {
              "steam_ton": {
                "gte": 10
              }
            }
          },
          "middle_ton": {
            "range": {
              "steam_ton": {
                "gte": 5,
                "lt": 10
              }
            }
          },
          "small_ton": {
            "range": {
              "steam_ton": {
                "gte": 1,
                "lt": 5
              }
            }
          }
        }
      }
    }
  }
}
#地理距离聚合,查找以某个点范围内的数据
POST /eemp_search_source_geo/_search
{
  "query": {
    "match": {
      "parentPostCode": "620100000000"
    }
  }
}
#origin取的原点
#distance_type有两种距离计算模式:arc(默认)和 plane。arc 计算模式是最准确的,plane计算模式是最快的,却是最不准确的。当你搜索的上下文比较小,地理区域跨越较小(~5km),可以考虑使用 plane。当搜索跨越很大区域时(如跨大陆搜索),plane 返回结果的误差幅度会更大
#unit在默认情况下,距离单位是m(米),其它单位也可以,如:mi(英里),in(英寸),yd(码),km(公里),cm(厘米),mm(毫米)
#将 keyed 标志设置为 true会给每个桶关联一个唯一的字符串键,并将范围作为哈希而不是数组返回
POST /smart_search_geo/_search
{
  "size": 0, 
  "aggs": {
    "my_geo_test": {
      "geo_distance": {
        "field": "location",
        "origin": {
          "lat": 103.114722,
          "lon": 36.233333
        },
        "unit": "km", 
        "distance_type": "arc", 
        "keyed":true,
        "ranges": [
          {"to": 3000,"key":"0-3000"},
          {"from": 3000, "to": 5000,"key":"3000-5000"},
          {"from": 5000}
        ]
      }
    }
  }
}


 


 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5716897.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存