es常用基本查询

es查询

1. 分页查询

GET test*/_search
{
  "size": 10, 
  "from": 0 ,
  "query": {
    "term": {
      "member_age" : 62
    }
  }
  
}

2. 根据id更新数据

POST {index}/_update/{id}
{
  "doc":{
  "key":"value"
  }
}

3. 根据查询语句修改文档 POST {index}/_update_by_query

例句:查询member_gender为男的数据修改为女
POST {index}/_update_by_query
{
  "script":{
  "inline":"ctx._source.member_gender = params.member_gender",
  "params": {
              "member_gender": "女"
            }
  },
  "query":{
    "term":{
      "member_gender" : "男"
    }
  }
}

4. #聚合查询 查询最大最小值加条件

POST wipro-headpic/_search?pretty
{
	# 查询条件
  "query":{
     "term":{
         "member_age":3
     }    
  },
  # 根据字段排序
   "sort": [
    {
      "member_age": {
        "order": "asc"
      }
    }
  ],
  "size": 2, # 显示条数
  # 聚合函数的聚合方法
  "aggs": {
    "maxage": {
      "max": {
        "field": "member_age"
      }
    }
  }
}

5 # 去重计数 这个field字段只能去重int类型的

POST  wipro-headpic/_search?size=0
{
   
    "aggs":{
        "age_count":{
            "value_count":{
               "field" : "ordernum"
            }
            
        },
        "name_count":{
            "cardinality":{
                "field" : "ordernum"
            }
        }
    }
}

去重计数 可以添加.keyword后缀解决报错illegal_argument_exception 异常

POST  wipro-headpic/_search?size=1
{
    "aggs":{
       "age_count":{
            "value_count":{
               "field" : "ordernum"
            }
            
        },
        "ordernum_count":{
            "cardinality":{
            
                "field" : "ordernum.keyword"
            }
        }
    }
}

查看内存占用情况

GET /_cat/segments/wipro-headpic?v&h=shard,segments,size,size.memory

所有segment占用的memory总和:

GET /_cat/nodes?v&h=name,port,sm

根据id查询文档

POST wipro-headpic/_doc/dbmykm4Bz_XBJXPx0VE4
{
“member_gender” : “ee”,
“type” : “wipro-headpic”,
“customer_shop_id” : “uuuuuuuuuuuuuu”,
“member_age” : 68

}

#根据条件更新文档
POST wipro-headpic/_update_by_query
{
“script”:{
“inline”:“ctx._source.member_gender = params.member_gender”,
“params”: {
“member_gender”: “女”
}
},
“query”:{
“term”:{
“member_gender” : “男”
}
}
}

#聚合查询 查询最大最小值
POST wipro-headpic/_search?pretty
{
“size”: 1,
“aggs”: {
“maxage”: {
“max”: {
“field”: “member_age”
}
}
}
}

#聚合查询 查询最大最小值加条件
POST wipro-headpic/_search?pretty
{
“query”:{
“term”:{
“member_age”:3
}
},
“sort”: [
{
“member_age”: {
“order”: “asc”
}
}
],
“size”: 2,
“aggs”: {
“maxage”: {
“max”: {
“field”: “member_age”
}
}
}
}

PUT wipro-headpic/_mapping
{
“properties”: {
“interests”: {
“type”: “text”,
“fielddata”: true
}
}
}

去重计数

POST wipro-headpic/_search?size=1
{

"aggs":{
   "count":{
        "value_count":{
           "field" : "member_id.keyword"
        }
        
    },
    "crad_count":{
     
        "cardinality":{
           "precision_threshold" : 12000,
            "field" : "member_id.keyword"
           
        }
    }
}

}

GET /wipro-headpic/_doc/_search?q=member_age:44
{
“size”:1000
}

查询指定字段

GET /wipro-headpic/_doc/_search?_source=member_age
{
“size”:1000
}

获取top(size)

GET /wipro-headpic/_doc/_search
{
“size” :0,
“aggs” : {
“popular_age” : {
“terms” : {
“field” : “member_gender.keyword”,
“size”: 3
}
}
}
}

获取top(size) shard_size 指定分片上返回的数据数量

GET /wipro-headpic/_doc/_search
{
“size” :0,
“aggs” : {
“popular_age” : {
“terms” : {
“shard_size”:10,
“field” : “member_age”,
“size”: 3

        }
    }
}

}

使用 terms aggs 查询top(默认10) 全局搜索

GET /wipro-headpic/_doc/_search
{
“size” :0,
“aggs” : {
“popular_age” : {
“terms” : {
“size”:3,
“field” : “member_age”
}
}
}
}

查询member_age最多的三个,并且member_id数量最多的5个–>广度查询 默认是深度

GET /wipro-headpic/_doc/_search?size=0
{
“aggs” : {
“一级actors” : {
“terms” : {
“field” : “member_age”,
“size” : 3,
“shard_size” : 10,
“collect_mode” : “breadth_first”
},
“aggs” : {
“二级costars” : {
“terms” : {
“field” : “member_id.keyword”,
“size” : 5
}
}
}
}
}
}

查看内存占用情况

GET /_cat/segments/wipro-headpic?v&h=shard,segments,size,size.memory

所有segment占用的memory总和:

GET /_cat/nodes?v&h=name,port,sm

查看内存占用情况

GET /_cat/segments/wipro-headpic?v&h=shard,name,port,segments,size,size.memory

GET /_stats/fielddata?fields=*

先查询再去重计数

POST wipro-headpic/_search?size=1
{

"aggs":{
   "count":{
        "value_count":{
           "field" : "member_id.keyword"
        }
        
    },
    "crad_count":{
     
        "cardinality":{
          
           "precision_threshold" : 500,
            "field" : "member_age"
           
        }
    }
}

}

排序查询

GET /_search
{
“sort”:
{
“member_age”: “desc”
}
,
“from”:0,
“size”:4,
“query” : {
“bool” : {
“must” : {
“term”:{
“member_id” : “7992254”
}
},
“filter” : {
“range”: { “member_age”:{“gt”: 4995}}
}

}

}
}

你可能感兴趣的:(es,es常用基本查询)