es常用查询语句

# 删除索引
DELETE /statistics_add_cart
DELETE /statistics_browses
DELETE /statistics_del_cart
DELETE /statistics_keywords
DELETE /statistics_goods_rejected

# 查看索引的配置
GET /statistics_add_cart/_settings

# 查询所有文档
GET /statistics_add_cart/add_cart/_search
{
  "query": {
    "match_all": {}
  }
}

# 清空所有数据
POST /statistics_add_cart/add_cart/_delete_by_query?refresh&slices=5&pretty
{
  "query": {
    "match_all": {}
  }
}

# 查看索引的状态
GET /_cat/indices

# 查看模板
GET _template/



# 查看mapping
GET /statistics_orders/orders/_mapping

# 单条件查询
GET /statistics_orders/orders/_search
{
  "size": 0, 
  "query": {
    "term": {
      "day": {
        "value": "20190327"
      }
    }
  }
}
# 多条件查询
GET /statistics_browses/browses/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "userid": {
              "value": "5241068"
            }
          }},
        {
          "term": {
            "goodsid":{
              "value": "12324"
            }
          }
        }
      ]
    }
  }
}


# 查询并聚合数据
GET /statistics_browses/browses/_search
{
  "size": 0, 
  "query": {
    "term": {
      "userid": {
        "value": "5241068"
      }
    }
  },
  "aggs": {
    "goodsid": {
      "terms": {
        "field": "goodsid",
        "size": 10
      }
    }
  },
  "_source": ["goodsid","specid"]
}

# 多次聚合并排序(分组求和统计排序)
GET /statistics_orders/orders/_search
{
  "size":0,
  "aggs": {
    "a1": {
      "terms": { 
        "field": "goodsid",
        "size":500,
        "order": {
          "total_price": "desc"
        }
      },
      "aggs":{
        "total_price":{
          "sum":{
            "field":"total_price"
          }
        }
      }
    }
  }
}

GET /statistics_browses/browses/_search
{
  "size": 0,
  "aggs": {
    "a1": {
      "terms": {
        "field": "goodsid",
        "size": 2048,
        "order": {
          "a2": "desc"
        }
      },
      "intraday_return": {
        "sum": {
          "field": "total_price"
        }
      }
    }
  }
}

# 聚合统计
GET /statistics_browses/browses/_search
{
  "from": 0,
  "size": 0,
  "aggs": {
    "_A_": {
      "terms": {
        "field": "goodsid"
      },
      "aggs": {
        "_B_": {
          "terms": {
            "field": "total_price"
          },
          "aggs": {
            "_sum_": {
              "sum": {
                "field": "total_price"
              }
            }
          }
        }
      }
    }
  }
}

#添加所有的ES相关内容

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

# 查看所有索引
GET _all/_settings

# 添加文档
PUT /lib/user/1
{
  "first_name": "Jane",
  "last_name": "Smith",
  "age": 32,
  "about": "I like to collect rock albums",
  "interests": [
    "music"
  ]
}

# 添加文档
POST /lib/user/
{
  "first_name": "Douglas",
  "last_name": "Fir",
  "age": 23,
  "about": "I like to build cabinets",
  "interests": [
    "forestry"
  ]
}

# 根据ID查找文档
GET /lib/user/1

# 查询某个索引的内容
GET /lib/user/_search
# 根据条件查询
GET /lib/user/_search?q=last_name:smith
# 过滤字段 
GET /lib/user/1?_source=age,interests
# 批量创建document
POST /cars/transactions/_bulk
{ "index": {}}
{ "price" : 10000, "color" : "red", "make" : "honda", "sold" : "2014-10-28" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 30000, "color" : "green", "make" : "ford", "sold" : "2014-05-18" }
{ "index": {}}
{ "price" : 15000, "color" : "blue", "make" : "toyota", "sold" : "2014-07-02" }
{ "index": {}}
{ "price" : 12000, "color" : "green", "make" : "toyota", "sold" : "2014-08-19" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 80000, "color" : "red", "make" : "bmw", "sold" : "2014-01-01" }
{ "index": {}}
{ "price" : 25000, "color" : "blue", "make" : "ford", "sold" : "2014-02-12" }
# 查询document
GET /cars/transactions/_search
# 统计color的数量
GET /cars/transactions/_search
{
  "size": 10,
  "aggs": {
    "popular_colors": {
      "terms": {
        "field": "color"
      }
    }
  }
}

# 创建mapping信息
PUT /test_article_mysql
{
  "mappings": {
    "article": {
      "properties": {
        "@timestamp": {
          "type": "date"
        },
        "@version": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "content": {
          "type": "text",
          "fields": {
            "cn": {
              "type": "text",
              "analyzer": "ik_max_word"
            },
            "en": {
              "type": "text",
              "analyzer": "english"
            }
          }
        },
        "create_at": {
          "type": "date"
        },
        "id": {
          "type": "long"
        },
        "title": {
          "type": "text",
          "fields": {
            "cn": {
              "type": "text",
              "analyzer": "ik_smart"
            },
            "en": {
              "type": "text",
              "analyzer": "english"
            }
          }
        },
        "type": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "update_at": {
          "type": "date"
        }
      }
    }
  }
}

# 检测分词器
GET _analyze?pretty
{
  "analyzer": "ik_smart",
  "text": "标准错误重定向到标准输出"
}
GET _analyze?pretty
{
  "analyzer": "ik",
  "text": "标准错误重定向到标准输出"
}
GET _analyze?pretty
{
  "analyzer": "ik_smart",
  "text": "标准错误重定向到标准输出"
}
GET _analyze?pretty
{
  "analyzer": "ik_max_word",
  "text": "标准错误重定向到标准输出"
}

# 根据分词查询
POST /test_article_mysql/article/_search
{
  "query": {

    "match": {
      "content": "国歌"
    }
  },
  "highlight" : {
        "pre_tags" : ["<tag1>", "<tag2>"],
        "post_tags" : ["</tag1>", "</tag2>"],
        "fields" : {
            "content" : {}
        }
    }
}

# 查看mapping
GET /cars/_mapping
# 删除索引
DELETE /cars

# 添加document
POST /cars/transactions/_bulk
{ "index": {}}
{ "price" : 15000, "color" : "red", "make" : "honda", "sold" : "2014-10-28" }
{ "index": {}}
{ "price" : 43000, "color" : "blue", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 23000, "color" : "green", "make" : "ford", "sold" : "2014-05-18" }
{ "index": {}}
{ "price" : 10300, "color" : "green", "make" : "toyota", "sold" : "2014-07-02" }
{ "index": {}}
{ "price" : 41000, "color" : "green", "make" : "toyota", "sold" : "2014-08-19" }
{ "index": {}}
{ "price" : 2000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 6000, "color" : "blue", "make" : "bmw", "sold" : "2014-01-01" }
{ "index": {}}
{ "price" : 10800, "color" : "blue", "make" : "ford", "sold" : "2014-02-12" }

# 统计查询
GET /cars/transactions/_search
{
  "size": 0,
  "aggs": {
    "popular_colors" :{
      "terms": {
        "field": "color"
      }
    },
    "_datetime" :{
      "terms": {
        "field": "sold"
      }
    }
  }
}
GET /cars/transactions/_search
{
  "size": 0,
  "aggs": {
    "color": {
      "terms": {
        "field": "color"
      }, 
      "aggs": {
        "avg_price":{
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}

# 聚合数据
GET /cars/transactions/_search
{
  "size": 0,
  "aggs": {
    "color": {
      "terms": {
        "field": "color"
      }, 
      "aggs": {
        "avg_price":{
          "avg": {
            "field": "price"
          }
        },
        "make":{
          "terms": {
            "field": "make"
          },
          "aggs": {
            "min_price": {
              "min": {
                "field": "price"
              }
            },
            "max_price":{
              "max": {
                "field": "price"
              }
            }
          }

        }
      }
    }
  }
}

统计去重聚合

# top_hits聚合
GET /alarm_errors-*/alarm_errors/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "@timestamp": {
              "gte": "2019-12-27 09:50:18",
              "lt": "2019-12-27 10:45:28",
              "format": "yyyy-MM-dd HH:mm:ss",
              "time_zone": "+08:00"
            }
          }
        }
      ]
    }
  },
  "size": 0,
  "aggs": {
    "id_aggs": {
      "terms": {
        "field": "id",
        "size": 100
      },
      "aggs": {
        "id_top": {
          "top_hits": {
            "sort": [
              {
                "@timestamp": "desc"
              }
            ],
            "size": 1,
            "_source": [
              "host_name",
              "domain",
              "request_uri",
              "file",
              "error_type",
              "message",
              "request_time_float",
              "id"
            ]
          }
        }
      }
    }
  }
}

# 折叠
GET /alarm_errors-*/alarm_errors/_search
{
    "query":{
        "match_all":{

        }
    },
    "collapse":{
        "field":"id"
    }
}

# 统计去重数目
GET /alarm_errors-*/alarm_errors/_search
{
  "query": {
    "match_all": {}
  },
  "size": 0,
  "aggs": {
    "uid_aggs": {
      "cardinality": {
        "field": "id"
      }
    }
  }
}

GET /qidingdong_access-2020-01/qidingdong_access/_search
{
  "size": 0,
  "aggs": {
    "request_uri": {
      "terms": {
        "field": "request_uri",
        "size": 1000
      }
    }
  }
}

统计单价商品的销售总额

GET /top_orders/orders/_search
{
  "query": {
    "term":{
      "goods_id":"298554"
    }
  },
  "aggs": {
    "total": {
      "scripted_metric": {
        "init_script": "params._agg.transactions = []",
        "map_script": "double mon = 0; if(doc.enjoy_type.value==3){ mon=doc.enjoy_price.value;}else{mon=doc.standard_price.value;} params._agg.transactions.add((doc.num.value * mon)/100)",
        "combine_script": "double total = 0; for (t in params._agg.transactions) { total += t } return total",
        "reduce_script": "double total = 0; for (a in params._aggs) { total += a } return total"
      }
    }
  }
  , "size": 1
}

动态设置mapping “fielddata”: true

PUT /qidingdong_access-*/qidingdong_access/_mapping
{       
  "properties": {
        "request_uri": {  
            "type": "text",
            "fielddata": true
        }       
    }         
}

设置索引的字段最大数量

PUT /qidingdong_access-*/_settings
{
  "index": {
    "mapping.total_fields.limit": 2000
  }
}