ElasticSearch 使用指南

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"
      }
    }
  ]
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值