ES--Kibana相关操作创建索引和Mapping

ES–Kibana相关操作创建索引和Mapping

1.新建索引

PUT test_index/

PUT secisland?pretty
{}


PUT secisland4
{
  "settings": {
    "index":{
      "number_of_shards":1,
      "number_of_replicas":0
    }
  }
}

PUT secisland6
{
  "settings": {
    "number_of_shards":1,
    "number_of_replicas":0
  }
}
更新副分片数量:

PUT secisland6/_settings
{
  "number_of_replicas":0
}

查看索引列表

GET _cat/indices?pretty&v


PUT test_data
{
  "settings": {
    "index":{
        "number_of_shards":5,
        "number_of_replicas":1
    }
  }
}

#test_data索引名称,相当于数据库名称 
#product 类型,相当于数据库的表名
#1就是要存储数据的id,不写的话默认给一个长字符串
PUT test_data/product/1
{
  "name":"华为电脑",
  "address":"深圳",
  "price":9600,
  "creat_time":"2019-01-02"
}

 
#修改
POST test_data/product/3/_update
{
  "doc":{
    "price":5835
  }
}
#es7+ 这样更新
POST test_data/_update/2
{
  "doc":{
    "price":5836
  }
}


#删除
DELETE test_data/product/3

#删除索引
DELETE test_data

GET test_data/_settings

GET _all/_settings

#计算每种电脑有多少数量
GET test_data/product/_search
{
  "size": 0,
  "aggs": {
    "group_by_name": {
      "terms": {
        "field": "name.keyword"
      }
    }
  }
}
#size:只获取聚合结果,而不执行聚合原始数据
#aggs:固定语法,要对一份数据执行分组聚合操作
#group_by_name:就是对每个aggs,都要起一个名字,这个名字是随机的,你随便取什么都ok
#terms:根据字段的值进行分组
#field:根据指定的字段的值进行分组

#计算北京地区,统计地址的数量
GET test_data/product/_search
{
  "size": 0,
  "query": {
    "match": {
        "address": "深圳"
    }
  },
  "aggs": {
    "all_names": {
      "terms": {
        "field": "address.keyword"
      }
    }
  }
}

#计算北京地区,统计地址的数量
GET test_data/product/_search
{
  "size": 0,
  "aggs": {
    "group_by_names":{
      "terms": {"field": "name.keyword"},
      "aggs": {
        "avg_price":{"avg": {"field": "price"}},
        "min_price":{"min": {"field": "price"}},
        "max_price":{"max": {"field": "price"}},
        "sum_price":{"sum": {"field": "price"}}
      }
    }
  }
}
#count:buckets,terms,自动就会有一个doc_count,就相当于是count
#avg:avg aggs 求平均值
#max:求一个bucket内,指定field值最大的那个数据
#min:求一个bucket内,指定field值最小的那个数据
#sum:求一个bucket内,指定field值的总和先分组,再算每组的平均值


#计算商品下的平均价格,并且按照平均价格降序排列
GET test_data/product/_search
{
  "size": 0,
  "aggs": {
   "all_names":{
     "terms": { "field": "name.keyword","collect_mode": "breadth_first","order": {
       "avg_price": "desc"}},
       "aggs": {
         "avg_price": {
           "avg": {"field": "price"}
         }
       }
     }
  }
}

#按照指定的价格范围区间进行分组,然后在每组内再按照name进行分组,最后再计算每组的平均价格  ranges:[{}]
GET test_data/product/_search
{
  "size": 0,
  "aggs": {
   "group_by_price":{
      "range": {
        "field": "price",
        "ranges": [
          {
            "from": 50,
            "to": 4000
          }
        ]
      },
      "aggs": {
        "group_by_names": {
          "terms": {
            "field": "name.keyword"
          },
          "aggs": {
            "avg_price": {
              "avg": {
                "field": "price"
              }
            }
          }
        }
      }
     }
  }
}


#histogram
GET test_data/product/_search
{
  "size": 0,
  "aggs": {
   "price":
   {
     "histogram": {
       "field": "price",
       "interval": 6000
     },
     "aggs": {
       "revenue": {
         "sum": {
           "field": "price"
         }
       }
     }
   }
  }
}

GET test_data/product/_search
{
  "size": 0,
  "aggs": {
    "time":{
      "date_histogram": {
        "field": "creat_time",
        "interval": "month",
        "format": "yyyy-MM-dd",
        "min_doc_count": 0,
        "extended_bounds":{
          "min":"2019-01-01",
          "max":"2019-01-02"
        }
      }
    }
  }
}

 
GET test_data/product/_search
{
  "size": 0,
  "query": {
    "term": {
      "brand": {
        "value": "华为电脑"
      }
    }
  },
  "aggs": {
    "single_brand_avg_price": {
      "avg": {
        "field": "price"
      }
    },
    "all":{
      "global": {},
      "aggs": {
        "all_brand_avg_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}
#single_brand_avg_price:就是针对query搜索结果,执行的,拿到的,就是华为电脑的平均价格
#all.all_brand_avg_price:拿到所有品牌的平均价格







2.创建别名

POST _aliases
{
  "actions": [
    {
      "add": {
        "index": "test_index",
        "alias": "test_index_name"
      }
    }
  ]
}

3.创建mapping

POST test_index/test_type/_mapping
{

"test_type": {
  "dynamic": false,
  "_all": {
    "enabled": false
  },
  "properties": {
    "wbbh": {
      "type": "keyword"
    },
    "jyxkzbh": {
      "type": "keyword"
    },
    "wbmc": {
      "type": "text",
      "analyzer": "smartcn",
      "fields": {
        "raw": {
          "type": "keyword"
        },
        "standard": {
          "type": "text",
          "analyzer": "standard"
        }
      }
    },
    "zbx": {
      "type": "keyword"
    },
    "zby": {
      "type": "keyword"
    },
    "zby_zbx": {
      "type": "keyword"
    },
    "lksj": {
      "type": "keyword"
    },
    "wbdz": {
      "type": "text",
      "analyzer": "smartcn",
      "fields": {
        "raw": {
          "type": "keyword"
        },
        "standard": {
          "type": "text",
          "analyzer": "standard"
        }
      }
    },
    "cjsj": {
      "type": "date"
    },
    "rksj": {
      "type": "date"
    },
    "gxdwmc": {
      "type": "text",
      "analyzer": "smartcn",
      "fields": {
        "raw": {
          "type": "keyword"
        },
        "standard": {
          "type": "text",
          "analyzer": "standard"
        }
      }
    },
    "wbfzr": {
      "type": "text",
      "analyzer": "smartcn",
      "fields": {
        "raw": {
          "type": "keyword"
        },
        "standard": {
          "type": "text",
          "analyzer": "standard"
        }
      }
    },
    "dt": {
      "type": "keyword"
    },
    "type": {
      "type": "keyword"
    }
  }
}

}

4.创建一个新的 people 索引,注意,将IP替换为你们自己的主机地址

###  创建一个新的 people 索引,注意,将IP替换为你们自己的主机地址
PUT http://10.247.63.97:9200/people
Content-Type: application/json
 
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1
  },
  "mappings": {
    "properties": {
      "type": {"type": "keyword"},
      "name": {"type": "text"},
      "country": {"type": "keyword"},
      "age": {"type": "integer"},
      "date": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"
      }
    }
  }
}

5.关于ElasticSearch的9200和9300端口区别

9200作为Http协议,主要用于外部通讯

9300作为Tcp协议,jar之间就是通过tcp协议通讯

ES集群之间是通过9300进行通讯

6.查询索引

GET /_cat/indices?v

获取索引

请求:

GET secisland
GET secisland/_settings


可以使用通配符获取多个索引,或者使用_all或者*号获取全部索引。
请求:

GET secisland*

或者:

GET */_settings

参数含义
参数大致解释:

  • took: 执行搜索耗时,毫秒为单位

  • time_out: 搜索是否超时

  • _shards: 多少分片被搜索,成功多少,失败多少

  • hits: 搜索结果展示
    hits.total: 匹配条件的文档总数
    hits.hits: 返回结果展示,默认返回十个
    hits.max_score:最大匹配得分
    hits._score: 返回文档的匹配得分(得分越高,匹配程度越高,越靠前)

  • _index _type _id 作为剥层定位到特定的文档

  • _source 文档源


7.删除索引

删除索引

删除索引secisland4,请求:

DELETE secisland4

删除索引可以使用逗号分隔符或者_all或者*号删除全部索引

DELETE secisland2,secisland3

DELETE _all

DELETE *

在这里插入图片描述

8.关闭索引

关闭/打开索引

关闭:POST secisland/_close

打开:POST secisland/_open

关闭的索引只能显示索引的元数据信息,不能够进行读写操作。

可以同时打开或者关闭多个索引。如果指向不存在的索引则会抛出错误,可以使用配置ignore_unavailable=true,不显示异常。

全部索引可以用_all或者*打开或者关闭。因为关闭的索引会继续占用磁盘空间而不能使用,所以在一定程度上造成磁盘空间的浪费。
禁止使用关闭索引功能:
在这里插入图片描述

9.重建索引

重建索引

POST _reindex
{
  "source": {"index": "secisland"},
  "dest": {"index": "secisland4"}
}
{
  "took" : 13,
  "timed_out" : false,
  "total" : 0,
  "updated" : 0,
  "created" : 0,
  "deleted" : 0,
  "batches" : 0,
  "version_conflicts" : 0,
  "noops" : 0,
  "retries" : {
    "bulk" : 0,
    "search" : 0
  },
  "throttled_millis" : 0,
  "requests_per_second" : -1.0,
  "throttled_until_millis" : 0,
  "failures" : [ ]
}

在这里插入图片描述

10.

  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值