目录
一.索引库操作
1.创建索引库
格式:PUT /索引库名称
PUT /my_index
{
"mappings": {
"properties": {
"first_name": {
"type": "keyword",
"copy_to": "full_name"
},
"last_name": {
"type": "keyword",
"copy_to": "full_name"
},
"full_name": {
"type": "text",
"fielddata": true
}
}
}
}
2.删除索引库
格式:DLETE /索引库
DELETE /my_index
3.开启-关闭索引库
格式:POST /索引库名/标识 _open:开启 _close:关闭
POST /my_index/_open
POST /my_index/_close
4.新增索引库字段
格式:PUT 索引库名/标识 _mapping:表结构
PUT my_index/_mapping
{
"properties": {
"testfield": {
"type": "text"
}
}
}
二.数据操作
1.添加数据
格式:POST /索引库名/标识 _doc:文档操作
//不确定ID的添加
POST /my_index/_doc
{
"first_name":"zhang",
"last_name":"san"
}
//确定ID的添加
PUT /my_index/_doc/1{
"first_name":"zhang",
"last_name":"san"
}
2.修改数据
格式:PUT /索引库/标识/主键
PUT /my_index/_doc/1{
"first_name":"zhang",
"last_name":"san1"
}
3.删除数据
格式:DELETE /索引库/标识/主键
DELETE /my_index/_doc/1
三.数据查询
1.精确查询
格式:GET /索引库/标识 _search:查询
match:精确查询
multi_match:多字段查询
range:模糊查询
filter:过滤
//单字段查询
GET /my_index/_search
{
"query": {
"match": {
"first_name": "zhang"
}
}
}
//多字段查询
GET /my_index/_search
{
"query": {
"multi_match": {
"query": "zhang",
"fields": ["first_name","last_name"]
}
}
}
2.范围查询
GET /my_index/_search
{
"query": {
"match": {
//full_name为copy_to设置的字段数据组合
"full_name":{
"query": "six hand"
}
}
}
}
3.模糊查询
GET /my_index/_search
{
"query": {
"range": {
"age": { //范围查询字段
"gte": 16, //大于16
"lte": 20 //小于20
}
}
}
}
4.统计查询
格式:GET /索引库/标识 _count:数量
//索引数据总条数
GET /my_index/_count
//条件匹配条数
GET /my_index/_count
{
"query": {
"range": {
"age": {
"gte": 10,
"lte": 20
}
}
}
}
GET /my_index*,my_index2*/_count //获取满足多个通配符条件的索引数据总条数
GET /my_index*/_count //获取满足通配符条件的数据总条数,同样适用于条件统计
5.地理位置查询
//查询距离经纬度坐标点,直线距离小于600KM的全部数据
GET /my_index/_search
{
"query": {
"bool": {
"must": [
{
"match_all": {}
}
],
"filter": [
{
"geo_distance": {
"distance": "100km",
"location": {
"lat": 36.227,
"lon": 117.2857
}
}
}
]
}
}
}
四.IK分词器
1.测试分词器
POST _analyze
{
"text": "中华人民共和国是一个伟大的国家",
"analyzer": "ik_max_word"
//ik_smart 为粗粒度分词
//ik_max_word 为细粒度分词
}
2.使用分词器
//在创建索引库结构时引入分词器
PUT /my_index
{
"mappings": {
"properties": {
"full_name": {
"type": "text",
"store": true,
"analyzer": "ik_max_word"
}
}
}
//在添加索引库结构时引入分词器
PUT /my_index/_mapping
{
"properties": {
"title": {
"type": "text",
"analyzer":"ik_max_word"
}
}
3.分词查询
GET /my_index/_search
{
"query": {
"match": {
"title":{
"query": "中华"
}
}
}
}
五.聚合查询
1.分组统计
//分组统计数量
GET /my_index/_search{
"size": 0,
"aggs": {
"group_by_title": {
"terms": {
//根据first_name聚合分组统计
"field": "first_name",
"size": 10
}
}
}
}
2.分组计算
GET /my_index/_search
{
"size": 0, //size用做排序后显示size条相关信息,默认不排序
"aggs": {
"group_by_title": {
"terms": {
"field": "first_name",
"size": 10
}
, "aggs": {
"avg_by_score": {
//avg为平均分
//sum为和值
//min为最小值
//max为最大值
"avg": {
"field": "score"
}
}
}
}
}
}
3.区段分组统计
GET /my_index/_search
{
"size": 2,
"sort": [
{
"score": {
"order": "asc"
//默认是desc,降序由大到小
//asc:升序由小到大
}
}
],
"aggs": {
"histogram_by_price":{
"histogram": {
"field": "score", //分段字段
"interval": 2 //即从能查询到的最小分数算起,分段单位为2
}
}
}
}
4.时间区段分组统计
GET /cars/_search
{
"aggs": {
"histogram_by_date": { //名称自定义
"date_histogram": { //时间区间
"field": "sold_date", //划分字段
"interval": "month", //时间区间单位月
"format": "yyyy-MM-dd", //时间格式化
"min_doc_count": 1, //每个区间最少的数量,如果2,区域只有1个数据,则该月不展示
"extended_bounds": { //指定总体时间区域
"min": "2021-01-01",
"max": "2022-12-31"
}
},
"aggs": {
"sum_by_price": {
"sum": {
"field": "price"
}
}
}
}
}
}