elasticsearch查询

(1)简单查询

curl -XGET http://127.0.0.1:9201/_search
curl -XGET http://127.0.0.1:9201/test231208/_search
curl -XGET http://127.0.0.1:9201/test231208/_doc/_search
curl -XGET http://127.0.0.1:9201/test231208/_doc/id

(2)match、match_all、multi_match查询,模糊查询,即先分词后查询;match_all查询全部数据;match针对一个field做查询,multi_match针对多个field做查询,任意一个字段符合条件就行

curl -XGET http://127.0.0.1:9201/test231208/_search -d 
'{
	"query": {
		"match": {
			"name": "jerry"
		}
	}
}'
curl -XGET http://127.0.0.1:9201/test231208/_search -d 
'{
	"query": {
		"match_all": {}
	}
}'
curl -XGET http://127.0.0.1:9201/test231208/_search -d 
'{
	"query": {
		"multi_match": {
			"query": "jerry",
			"fields": [
				"name"
			]
		}
	}
}'

(3)term查询或range查询,精确查询

curl -XGET http://127.0.0.1:9201/test231208/_search -d 
'{
	"query": {
		"term": {
			"age": 2
		}
	}
}'
curl -XGET http://127.0.0.1:9201/test231208/_search -d 
'{
	"query": {
		"range": {
			"age": {
				"gte": 3,
				"lte": 4
			}
		}
	}
}'

(4)bool查询,一个或多个查询子句的组合,must表示必须匹配(类似与)、should表示选择性匹配(类似或)、must_not表示必须不匹配(类似非)、filter表示过滤条件

curl -XGET http://127.0.0.1:9201/test231208/_search -d 
'{
	"query": {
		"bool": {
			"must": [
				{
					"match": {
						"name": "tom"
					}
				},
				{
					"match": {
						"age": 2
					}
				}
			],
			"should": [
				{
					"match": {
						"name": "tom"
					}
				},
				{
					"match": {
						"name": "jerry"
					}
				}
			],
			"must_not": [
				{
					"match": {
						"name": "diana"
					}
				}
			],
			"filter": [
				{
					"range": {
						"age": {
							"gte": 2,
							"lte": 4
						}
					}
				}
			]
		}
	}
}'

(5)查询部分属性字段、分页和排序

curl -XGET http://127.0.0.1:9201/test231208/_search -d 
'{
	"query": {
		"match_all": {}
	},
	"_source": {
		"includes": [
			"age",
			"name"
		]
	},
	"from": 0,
	"size": 10,
	"sort": [
		{
			"age": {
				"order": "asc"
			}
		}
	]
}'

(6)group by分组

curl -XGET http://127.0.0.1:9201/test231208/_search -d 
'{
	"query": {
		"range": {
			"gmt_create": {
				"gte": "20230101000000",
				"lte": "20240101000000"
			}
		}
	},
	"from": 0,
	"size": 0,
	"sort": [],
	"aggs": {
		"group_by_key": {
			"terms": {
				"field": "age"
			}
		}
	}
}'

响应体,如下

{
    "took": 7,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "group_by_key": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 2,
                    "doc_count": 1
                },
                {
                    "key": 4,
                    "doc_count": 1
                }
            ]
        }
    }
}

 (7)count计数

curl -XGET http://127.0.0.1:9201/test231208/_count -d 
'{
	"query": {
		"match_all": {}
	}
}'

响应体,如下

{
    "count": 2,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    }
}

(8)max求最大值

curl -XGET http://127.0.0.1:9201/test231208/_search -d 
'{
	"query": {
		"match_all": {}
	},
	"from": 0,
	"size": 0,
	"aggs": {
		"maxf": {
			"max": {
				"field": "age"
			}
		}
	}
}'

响应体,如下

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "maxf": {
            "value": 4
        }
    }
}

其中,请求体的max可换成avg、min、sum,分别用于计算均值、最小值、总值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值