提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
本文对 ElasticSearch 内置分词器 未说明,有说明ik中文分词器的用法提示:以下是本篇文章正文内容,下面案例可供参考
一、ES介绍
ES是一个基于RESTful web接口并且构建在Apache Lucene之上的开源分布式搜索引擎
二、ES基本命令介绍
1.命令介绍
代码如下(示例):
/_search #搜索所有的index和type的内容
/index【s】/_search #【s】多个用,隔开
#指查询多个index下的所有type的内容
/index【s】/type【s】/_search #【s】多个用,隔开
#指查询多个index下,多个type的内容
#index可以使用通配符
*x #指查询末尾为x的index 下的数据
_all #指查询所有的index 下的数据
例如:/*x/type【s】/_search
-
term组:精准查询
term terms
-
match组:match 不经过属性设置是模糊查询
match match_all multi_match match_phrase
2.ES表结构
2.0 数据类型
String
text 和 keyword
数值数据类型
long,integer,short,byte,double,float,half_float,scaled_float
日期数据类型
date
布尔数据类型
boolean
二进制数据类型
binary
范围数据类型
integer_range,float_range,long_range,double_range,date_range,ip_range
2.1 ES表结构创建
#先创建索引【index】 默认创建5个分片1个副本 默认是_id为routing
#分片数据保存公式:shard=hash(routing) % number_of_primary_shards
PUT http://127.0.0.1:9200/indexname【名字随便起】
#入参 可以自定义分片和副本
{
"settings":{
"number_of_shards": 5,
"number_of_replicas": 1
}
}
#CURL 命令
curl -X PUT "localhost:9200/indexname?pretty"
{
"settings": {
"number_of_shards": 4,
"number_of_replicas": 2
},
#mapping 映射,用来添加字段的
"mapping": {
#type_1是 type屬性
"type_1": {
"properties": {
"id": {
"type": "long"
},
"name": {
#text 类型默认分词
"type": "text",
#analyzer ik分词:ik_smart大粒度
#ik_max_word 小粒度
"analyzer": "ik_smart"
},
"adder": {
"type": "keyword"
},
"money": {
"type": "double"
}
}
}
}
}
2.2 ES表结构新增
PUT /test_1/test/_mapping
{
"test":{
"properties": {
"number":{
"type": "double"
}
}
}
}
2.3 ES修改表结构(es不能修改类型)
提醒:::es不能修改数据类型,只有通过创建新的index复制
curl -X PUT "localhost:9200/indexname_1?pretty"
{
"settings": {
"number_of_shards": 4,
"number_of_replicas": 2
},
"mapping": {
"type_1": {
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "keyword"
},
"adder": {
"type": "ik_smart"
}
}
}
}
}
## 替换
POST _reindex
{
"source": {
"index": "indexname"
},
"dest": {
"index": "indexname_1"
}
}
##删除原来的
DELETE indexname
##设置别名
POST /_aliases
{
"actions": [
{"add": {"index": "indexname_1", "alias": "my_index"}}
]
}
3.ES查询里层级关系
4.ES命令使用
#创建索引
PUT /customer
#删除索引
DELETE /customer
#插入内容
PUT /customer/doc/1
{
#内容插入到_source id为1
"name": "xxxx"
}
#修改
POST /customer/doc/1/_update?pretty
{
"doc": { "name": "zhangsan", "age": 20 }
}
#查看所有索引的信息
GET /_cat/indices?v
#批量操作 _bulk
POST /customer/doc/_bulk?pretty
{"update":{"_id":"1"}}
{"doc": { "name": "lisi" } }
{"delete":{"_id":"2"}}
4.1简单查询命令
#q=* 指查询所有文档 sort 指时间排序 pretty指返回json格式结果
GET /indexname/_search?q=*&sort=time:asc&pretty
#该查询和上面查询结果相同
GET /indexname/_search
{
#query 查询
#match_all查询全部
#match 指定查询 zhagnsan 或者 lisi ||| operator=and 就是都存在
#match_phrase 替换 【match变体】 查询zhangsan lisi 字符串的数据
"query": {
"match_all": {} /
"match": {
"name": "zhangsan lisi",
"operator":"and"
}
},
"sort": [
{ "account_number": "asc" }
],
#指定返回参数的个数 具体字段
"_source": ["name", "money"],
#不指定 默认0 分页开始
"from": 10,
#不指定 默认为10 查询多少个
"size": 10
}
4.2组合查询布尔逻辑
# bool must 查询全部都符合条件的是结果
# bool must_not 全部不符合的就是结果
# bool should 查询的条件有一个符合就是结果
# minimum_should_match 或的精度控制
# filter range 范围查询
GET /indexname/_search
{
"query": {
"bool": {
"must": [
{ "match": { "name": "zhangsan" } },
{ "match": { "name": "lisi" } }
],
"must_not": [
{ "match": { "age": "25" } }
],
"minimum_should_match",2,
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}
4.3 聚合查询
# 根据状态分组 获取默认前10个数据 统计计数倒倒序
#size = 0 不显示查询结果 就能明了看出汇总的结果数据
GET /inexname/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword"
}
}
}
}
# 多种聚合 都要在group_by_state里
# 这是聚合的通用模式
# 以上面的聚合数据为准 再聚合
# 上层聚合进行逻辑操作 比如 下层聚合的排序
GET /inedxname/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword",
"order": {
"average_balance": "desc"
}
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
# 复杂分组 聚合演示
# 1.按照年龄分3组
# 2.依据年龄组在性别进行分组
# 3.依据以上分组 进行账户平均值
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_age": {
"range": {
"field": "age",
"ranges": [
{
"from": 20,
"to": 30
},
{
"from": 30,
"to": 40
},
{
"from": 40,
"to": 50
}
]
},
"aggs": {
"group_by_gender": {
"terms": {
"field": "gender.keyword"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
}
}