linux服务器使用curl命令处理常用es查询

目录

场景:

结果格式化显示:

验证服务启动:

 单个参数变量查询:

分页查询:

日期范围查询:

must中单个参数条件查询

must中多个参数条件查询

 使用filter过滤查询

  使用sort过滤查询

总结:

场景:

线上存在浏览器禁止访问es的9200端口的情况,只能在es的服务器上去操作因此用到curl命令下面总结一波、

结果格式化显示:

centos 安装 jq:yum isstall jq

验证服务启动:


curl -X GET 'http://192.168.xx.xxx:9200' -u 'username:password' | jq  .

 单个参数变量查询:

 curl -X POST 'http://192.168.xx.xxx:9200/esbizlog/_search' \
-H 'Content-Type:application/json'\
-u 'username:password' \
-d '{
"query":{
   "match_phrase":{
    "gisq_request_id":"e8bcf64e-7ed9-4a94-9b54-85dd1e277790"}
    }
}' | jq .

分页查询:

curl -X POST 'http://192.168.16.155:9200/esbizlog/_search' \
-H 'Content-Type:application/json' \
-u 'username:password' \
-d '{
    "from":1,
    "size":2
}' | jq .
 

 注意:分页变量from和size是和query平级,不能放在query里面

日期范围查询:

curl -X POST 'http://192.168.xx.xxx:9200/esbizlog/_search' \
-H 'Content-Type:application/json' \
-u 'username:password' \
-d '{
  "query": {
    "range": {
        "datetime": {
          "gt": "2023-06-04 23:59:59",
          "lt": "2023-06-05 12:00:00"
        }
     }
  },
  "from": 1,
  "size": 3
}' | jq .
 

must中单个参数条件查询

curl -X POST 'http://192.168.xx.xxx:9200/esbizlog/_search' -H 'Content-Type:application/json' -u 'elastic:[email protected]' -d '{"query":{
 "bool":{
      "must":{"match_phrase":{"gisq_request_id":"e8bcf64e-7ed9-4a94-9b54-85dd1e277790"}}
 }
}}' | jq .

must中多个参数条件查询

curl -X POST 'http://192.168.xx.xxx:9200/esbizlog/_search' -H 'Content-Type:application/json' -u 'elastic:[email protected]' -d '{"query":{
 "bool":{
      "must":[
           {"match_all":{} },
           {"range": {
                    "datetime": {
                      "gt": "2023-06-04 23:59:59",
                      "lt": "2023-06-05 12:00:00"
                                 }
                     }
           }
           ]
}}}' | jq .

注:must中如果单个参数查询则后面不用数组形式嵌套,如果多个参数则需要用数组形式嵌套
 

 使用filter过滤查询

curl -X POST 'http://192.168.xx.xxx:9200/esbizlog/_search' \
-H 'Content-Type:application/json' \
-u 'elastic:[email protected]' \
-d '{
"query":{
    "bool":{
      "filter":{
          "match":{
             "gisq_request_id":"f7bbae56-405a-4f71-8827-9fff821a9679"}
          }
       }
},"from":0,"size":2
}' | jq .

注:使用filter时要将filter放到bool内部使用,must外部(并列)否则会报错 unknown field [filter]

  使用sort过滤查询

curl -X POST 'http://192.168.xx.xxx:9200/esbizlog/_search' \
-H 'Content-Type:application/json' \
-u 'elastic:[email protected]' \
-d '{
    "query":{
         "range": {
            "datetime": {
              "gt": "2023-06-04 23:59:59",
              "lt": "2023-06-05 12:00:00"
                        }
               }
},

"from":0,

"size":10,
"sort":{
   "datetime":{"order":"desc"}
}
}' | jq .

总结:

1.上面便是es的常用的查询,其中查询单个字段直接匹配查询即可,对于filter查询通常要放到bool里面位置和must并列。

2.curl命令 curl -x [请求方式] “url” -H [请求头参数设置] -u 【用户账号:密码】设置 -d 请求提设置

猜你喜欢

转载自blog.csdn.net/qq_38423256/article/details/131042245
今日推荐