ES【查询】参考文章:https://blog.csdn.net/lazyboy2/article/details/125122833
查询
单条查询
// /_index/_search
POST /free_study_stu_all_info_*/_search
{
"query": {
"match": {
"orderNo": "1_1657873553700"
}
}
}
多条查询
// 多条查询,相当于sql中的in查询
GET /free_study_stu_all_info_*/_search
{
"query": {
"terms": { // 多条精确匹配
"orderNo": [
"11_1603955970027",
"11_1603957926122"
]
}
}
}
范围查询
// 范围查询,相当于sql的>,<,!=
GET /free_study_stu_all_info_*/_search
{
"query": {
"range": {
"createTime": {
"gte": "2022-11-15 00:00:00" // 大于等于
"gt": "2022-11-15 00:00:00" // 大于
}
}
}
}
多个字段查询
// 多个字段查询
GET /{索引名}/_search
{
"query": {
"bool": { // bool查询
"must": [], // must条件,类似SQL中的and, 代表必须匹配条件
"must_not": [], // must_not条件,跟must相反,必须不匹配条件
"should": [] // should条件,类似SQL中or, 代表匹配其中一个条件
}
}
}
示例
GET /free_study_stu_all_info_*/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"orderNo": "1_1666265202972"
}
},
{
"term": {
"brandId": 1
}
}
]
}
}
}
综合示例
GET /order_v2/_search
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"term": {
"order_no": "2020031312091209991"
}
},
{
"range": {
"shop_id": {
"gte": 10,
"lte": 200
}
}
}
]
}
},
{
"terms": {
"tag": [
1,
2,
3,
4,
5,
12
]
}
}
]
}
}
}
特殊查询(字符串为空或空数组)
字符串不为空
{
"query": {
"wildcard": {
"entUserId": { // 字段名称
"value": "*"
}
}
}
}
字符串为空
{
"query": {
"bool": {
"must_not": [
{
"wildcard": {
"entUserId": {
"value": "*"
}
}
}
]
}
}
}
对象数组为空
{
"query": {
"nested": {
"path": "studentRoundInfos",
"query": {
"bool": {
"must": {
"exists": {
"field": "studentRoundInfos"
}
}
}
}
}
}
}
对象数组不为空
{
"query": {
"bool": {
"must_not": [{
"nested": {
"path": "studentRoundInfos",
"query": {
"exists": {
"field": "studentRoundInfos"
}
}
}
}]
}
}
}
更新
// 按条件修改 /_index/_type/_id/_update
POST /free_study_stu_all_info_test2/free_study_stu_all_info_test2/3974830/_update
{
"doc": {
"userTagList":["A"]
}
}
结果
// 返回结果
{
"_index" : "free_study_stu_all_info_test2",
"_type" : "free_study_stu_all_info_test2",
"_id" : "590048",
"_version" : 3,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 3418842,
"_primary_term" : 4
}
指定返回字段
分页
排序
GET /order_v2/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"user.id": { // 嵌套json对象,使用 点 连接字段名即可
"order": "desc"
}
}
]
}