ES 查询

POST /_analyze
{}

PUT /my_index/_doc/1
{
  "id":1,
  "title":"es简介",
  "description": "catelina cat",
  "content":"es好用好用gg真好用",
  "age": 20,
  "price": 70,
  "gender":"M"
}


PUT /my_index/_doc/2
{
  "id":2,
  "title":"java编程思想",
  "description": "javacatmorning dog",
  "content":"这就是个工具书",
  "age": 30,
  "price": 80,
  "gender":"M"
}


PUT /my_index/_doc/3
{
  "id":3,
  "title":"大数据简介",
  "description": "bigdatacatnight tiger",
  "content":"你知道什么是大数据吗,就是大数据",
  "age": 40,
  "price": 41,
  "gender":"F"
}

PUT /my_index/_doc/4
{
  "id":4,
  "title":"大数据简介",
  "description": "bigdatacatnight tiger",
  "content":"你知道什么是大数据吗,就是大数据",
  "age": 50,
  "price": 41,
  "gender":"F"
}

# 查询所有
GET /my_index/_search
{
  "query": {
    "match_all": {}
  }
}

# term 查询: 精确查找时可以使用term
GET /my_index/_search
{
    "query":{
        "term":{
            "title": "简"
        }
    }
}

# terms 查询
GET /my_index/_search
{
    "query":{
        "terms":{
            "title": ["es", "java"]
        }
    }
}

# match 多用于模糊查询
GET /my_index/_search
{
   "query": {
     "match": {
       "title": "简介"
     }
   },
   "sort": [
     {
       "_id": {
         "order": "desc"
       }
     }
   ]
}

# multi match 多值匹配查询
GET /my_index/_search
{
  "query": {
    "multi_match": {
      "query": "数据 思想",
      "fields": [
        "title",
        "content"
      ],
      "operator": "OR"
    }
  }
}

# bool查询的使用
# bool查询包含四个子句,must,filter,should,must_not
# must 返回的文档必须满足must子句的条件,并且参与计算分值
# filter 返回的文档必须满足filter子句的条件。但是不会像Must一样,参与计算分值
# should 返回的文档可能满足should子句的条件。在一个Bool查询中,如果没有must或者filter,有一个或者多个should子句,那么只要满足一个就可以返回。minimum_should_match参数定义了至少满足几个子句。
# must_not 返回的文档必须不满足must_not定义的条件。
# 如果一个查询既有filter又有should,那么至少包含一个should子句。
GET /my_index/_search
{
    "query":{
        "bool":{
          "must":[{
                "term":{
                    "title": "es"
                }
             },{
                 "term":{
                     "content": "gg"
                 }
             }
           ],
          "should": [
            {"term":{"age":20}},
            {"term":{"age":30}}
          ],
          "filter": {
            "range": {
              "create_admin_id": {
                "gte": 10,
                "lte": 20
              }
            }
          }
        }
    }
}

# range query 范围查询
# gte,lte,gt,lt,boost
GET /my_index/_search
{
	"query":{
    	"range":{
            "age":{
                "gte": 20,
                "lte": 30
            }
    	}
	}
}

# 通配符查询
# 支持的通配符为*(匹配任意字符序列,包括空字符序列)以及?(匹配任意单字符)
GET /my_index/_search
{
    "query":{
        "wildcard":{
            "description": "*cat*"
        }
    }
}

# 正则表单时查询
GET /my_index/_search
{
  "query":{
    "regexp":{
      "description":{
        "value":".*cat.*",
        "boost":10.0
      }
    }
  }
}

# 前缀查询
GET /my_index/_search
{
  "query":{
    "match_phrase_prefix":{
      "description":{
        "query":"java",
        "slop":10.0
      }
    }
  }
}

# query_string
GET /my_index/_search
{
  "query":{
    "query_string":{
      "query": "cat dog"
    }
  }
}

# 聚合查询
# 聚合提供了用户进行分组和数理统计的能力,可以把聚合理解成SQL中的GROUP BY和分组函数
# 指标聚合/桶聚合
# Metrics(度量/指标):简单的对过滤出来的数据集进行avg,max操作,是一个单一的数值
# Bucket(桶):将过滤出来的数据集按条件分成多个小数据集,然后Metrics会分别作用在这些小数据集上
# max/min/avg/sum/stats
GET /my_index/_search
{
   "aggs":{
      "avg_ages":{
      		"avg":{
      			"field":"age"
      		}
      	}
   }
}

# terms聚合
# terms根据字段值项分组聚合.
# field按什么字段分组,
# size指定返回多少个分组,
# shard_size指定每个分片上返回多少个分组,
# order排序方式.
# 可以指定include和exclude正则筛选表达式的值,指定missing设置缺省值
GET /my_index/_search
{
    "size": 0, 
    "aggs": {
      "agg_terms": {
        "terms": {
          "field": "price",
          "order": {
            "_count": "asc"
          }
        },
        "aggs": {
          "max_age": {
            "max": {
              "field": "age"
            }
          }
        }
      }
    }
}

# cardinality去重:cardinality 即去重计算,类似sql中 count(distinct),先去重再求和,计算指定field值的种类数。
GET /my_index/_search
{
  "size": 0, 
  "aggs": {
    "agg_terms": {
      "terms": {
        "field": "price",
        "order": {
          "_count": "asc"
        }
      },
      "aggs": {
        "max_age": {
          "max": {
            "field": "age"
          }
        }
      }
    }
  }
}



























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值