栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Elasticsearch 常用语法

Elasticsearch 常用语法

#创建索引并设置分片 
#分片个数,在创建索引不指定时 默认为 5
#数据副本,一般设置为1

PUT  index_es
{
 "settings": {
      "number_of_shards": 5,
      "number_of_replicas": 1
    }
  
}

#创建mappings

PUT index_es/type_es/_mapping
{
  "type_es": {
    "properties": {
      "name": {
        "type": "text"
      },
       "age": {
        "type": "long"
      },
      "date_time": {
          "type": "date",
          "format":"yyyy-MM-dd HH:mm:ss"
        }
        
    }
  }
}

# mappings 是不允许修改,danshi但是是可以新增字段
#在已经建立的索引下 ,添加 字段mappings  

PUT index_es/type_es/_mapping
{
  "type_es": {
    "properties": {
      "add_info": {
        "type": "text"
      }
    }
  }
}

#插入数据,设置id

PUT index_es/type_es/1
{
  "name": "张三 ",
  "age": "22",
  "date_time":"2022-01-01 19:03:22"
}


#插入数据 ,自动生成id

POST index_es/type_es
{
  "name": "张三2",
  "age": "8",
  "date_time":"2021-02-01 19:03:22"
}


#查询对应索引下所有数据

GET index_es/type_es/_search
{
  "query": {
    "match_all": {}
  }
}

#如果你使用 match 查询一个全文本字段,它会在真正查询之前用分析器先分析match一下查询字符:

GET index_es/type_es/_search
{
  "query":{
    "match":{
      "name":"张三2"
    }
  }
}

#multi_match查询允许你做match查询的基础上同时搜索多个字段,在多个字段中同时查一个:

GET index_es/type_es/_search
{ 
   "query":{
      "multi_match": { 
          "query":    "1", 
          "fields":   [ "name", "age" ] 
      } 
     
   }
}

#分页查询 

GET index_es/type_es/_search
{
 "query":{
    "match":{
      "name":"张三"
    }
  },
  "from": 0,
  "size": 5
}


#分页查询 ,并排序 

GET index_es/type_es/_search
{
 "query":{
    "match":{
      "name":"张三"
    }
  },

  "from": 0,
  "size": 5
  ,
  "sort": {
        "date_time": {
            "order": "asc"
        }
    }
  
}

#range过滤允许我们按照指定范围查找一批数据

#范围操作符包含:
#gt :: 大于
#gte:: 大于等于
#lt :: 小于
#lte:: 小于等于
GET index_es/type_es/_search
GET index_es/type_es/_search
{
  "query":{
    "range": { 
        "age": { 
            "gt": 20, 
            "lt": 30 
        } 
    } 
    
  }
}

#时区范围查询
#范围操作符包含:
#gt :: 大于
#gte:: 大于等于
#lt :: 小于
#lte:: 小于等于

GET index_es/type_es/_search
{
    "query": {
        "range": {
            "date_time": {
                "gte": "2022-01-10 12:12:02", 
                "lte": "2022-10-01 12:12:02",
                "format": "yyyy-MM-dd HH:mm:ss"
            }
        }
    }
}

#查询所有记录中年龄的最大值

GET index_es/type_es/_search
{
  "size": 0, 
  "aggs": {
    "age": {
      "max": {
        "field": "age"
      }
    }
  }
}

# 统计文档数量

GET index_es/type_es/_count
{
    "query":{
        "match":{
            "age":8
        }
    }
}

#stats 统计 count max min avg sum 5个值 ,为统计别名age_count 

GET index_es/type_es/_search
{
  "size": 0, 
    "aggs":{
        "age_count":{
            "stats":{
                "field":"age"
            }
            
        }
    }
}

#数据迁移 

POST _reindex
{
  "source": {
    "index": "index_es"
  },
  "dest": {
    "index": "new_index_es"
  }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/761551.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号