欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ES基础查询语法

程序员文章站 2022-07-06 15:47:35
...

普通查询,多条件查询
must term 类似数据库 =
must terms 类似于数据库查询的 in
must_not term 类型数据库的 !=
must_not terms 类型于数据库查询的 not in

POST /exp-order-v1-benchmark-202010/_search
{
	"query": {
		"bool": {
			"must": [{
				"terms": {
					"orderStatus": [
						4, 5
					]
				}
			},{
			  "term": {
			    "billId": {
			      "value": "1146949448946155522"
			    }
			  }
			}],
			"filter": {
			  "term": {
			    "orderType": "1"
			  }
			}
		}
	}
}
POST /exp-order-v1-benchmark-202010/_search
{
	"query": {
		"bool": {
			"must": [{
				"terms": {
					"orderStatus": [
						4, 5
					]
				}
			},{
			  "term": {
			    "billId": {
			      "value": "1146949448946155522"
			    }
			  }
			}],
			"filter": {
			  "term": {
			    "orderType": "9"
			  }
			}
		,
		  "must_not": [
		    {
		      "term": {
		        "orderStatus": {
		          "value": "4"
		        }
		      }
		    }
		  ]
		}
	}
}

count查询条数
range里是范围查询

POST /exp-order-v1-benchmark-202010/_count
{
  "query": {
    "range": {
      "billCreateTime": {
        "gte": 1603814409000,
        "lte": 1603900809000
      }
    }
  }
}

group by ,根据billSenderCityId字段进行分组,可以查询获取到每个桶的个数

POST /exp-order-v1-benchmark-202010/_search
{
	"query": {
		"range": {
			"billCreateTime": {
				"gte": 1603814409000,
				"lte": 1603900809000
			}
		}
	},
	"size": 0,
	"aggregations": {
		"field1": {
			"terms": {
				"field": "billSenderCityId",
				"size": 20
			}
		}
	}
}

类似于count(distinct(billSenderCityId)),可以获取到去重后的billSenderCityId的个数
POST /exp-order-v1-benchmark-202010/_search

{
  "query": {
    "range": {
      "billCreateTime": {
        "gte": 1603814409000,
        "lte": 1603900809000
      }
    }
  },
  "aggs": {
    "field": {
      "cardinality": {
        "field": "billSenderCityId"
      }
    }
  },
  "size": 0
}
相关标签: es