Elasticsearch-URL查询实例解析

本文详细介绍了Elasticsearch的URL查询方式,包括查询节点、节点健康状态、索引信息、记录控制、排序、模糊搜索、大小比较等多种操作。通过对不同参数的使用,展示了如何有效地管理和检索ES数据。
摘要由CSDN通过智能技术生成

ES(elasticsearch),以下简称ES

ES的查询有query、URL两种方式,而URL是比较简洁的一种,本文主要以实例探讨和总结URL的查询方式

1、语法

curl [ -s][ -g][ -X<REST Verb>][ -H 'Content-Type: application/json'] '<Node>:<Port>/<Index>[/Type][/ID]/_search?pretty&q=<search string>'
  注意要用''包起来,否则后面的&会被识别为“后台执行”,即&后面的内容被忽略
  -s 不输出查询的时间那些东西
  -g 做转义用  
  <REST Verb>:REST风格的语法谓词,GET/POST/PUT   <Node>:节点ip,默认使用localhost   <port>:节点端口号,默认80,ES默认使用9200   <Index>:索引名,支持通配符,power_json*   <Type>:索引类型,由于一个index只有一个type,可不输入   <ID>:操作对象的ID号,可不输入
  q :前面加&,后跟查询语句

2、常用参数

q---查询字符串
sort---排序执行。可以是fieldName或 fieldName:asc/ 的形式fieldName:desc。fieldName可以是文档中的实际字段,也可以是特殊_score名称,表示基于分数的排序。可以有几个sort参数(顺序很重要)。
from---从命中的索引开始返回。默认为0。
size---要返回的点击次数。默认为10。
_source_include---查询包含某些source字段的文档。
_source_exclude---查询不包含某些source字段的文档。
timeout---搜索超时,将搜索请求限制在指定的时间值内执行,并使用在到期时累积的点击数进行保释。默认为无超时。
default_field---默认为index.query.default_field,即未指定字段前缀时返回所有字段,索引设置为*
default_operator---默认查询运算符,未指定时默认为OR。
analyzer---用于分析查询字符串的分析器名称。
_source---设置为false禁用_source字段检索。
analyze_wildcard---是否应分析通配符和前缀查询,默认为false
status:active---where the status field contains active
        ---(status相当于fieldname,active相当于值----->TESTID:39232032303039,由于=被用在了前面“q=”,所以这里用“:”代替了“=”) title:(quick OR brown)
---where the title field contains quick or brown. If you omit the OR operator the default operator will be used author:"John Smith"---where the author field contains the exact phrase "john smith" _exists_:title---where the field title has any non-null value date:[2012-01-01 TO 2012-12-31]---All days in 2012 count:[10 TO *]---Numbers from 10 upwards count:>=10---Numbers from 10 upwards

3、总览

curl localhost:9200/_cat/nodes?v        //查看node状态
curl localhost:9200/_cat/health?v        //查看集群健康状况curl 'localhost:9200/_cat/indices?v&s=index'    //s=sort排序    
curl 'localhost:9200/_cat/indices/power_json*?v&s=index'  //只查power_json
curl 'localhost:9200/_cat/allocation?v&s=node'  //获取集群的节点列表
curl 'localhost:9200/power_json*?pretty'   //pretty,美观
curl 'localhost:9200/power_json*/_search?pretty&q=_exists_:MULT'     //是否存在
curl 'localhost:9200/power_json*/_search?pretty&q=TESTID:10000000107326732674'   //查指定的字段值
curl 'localhost:9200/power_json*/_search?pretty&q=TESTID:10000000107326732674&size=3' //查指定的字段值,并只显示3个
curl 'localhost:9200/power_json*/_search?pretty&q=TESTID:10000000107326732674&from=2&size=3' //从第3个开始只显示3个,即3/4/5
curl 'localhost:9200/power_json*/_search?pretty&q=TESTID:10000000107326732674&sort=TIME:desc' //按时间排序,desc降序,默认为升序
curl 'localhost:9200/power_json*/_search?pretty&analyze_wildcard&q=TESTID:10000000107326732674'   //模糊查询
curl 'localhost:9200/power_json*/_search?pretty&q=VAL:<200'  //比较大小
curl 'localhost:9200/power_json*/_search?pretty&_source=false'  //是否显示
curl 'localhost:9200/power_json*/_search?pretty&_source_includes=TIME,VAL'  //设置包含的字段
curl -g 'localhost:9200/power_json*/_search?pretty&q=(SOLAR:1%20AND%20CENTRAL:1)'  //组合查询
curl -g 'localhost:9200/power_json*/_search?pretty&q=TIME:[2019-05%20TO%202019-06]' //范围查询

4、详解

4.1、查询节点

heap.percent:堆内存,1/2最大内存-1和31之间取较小的值,高的话就是ES集群负担比较重,解决:关闭一些索引(阈值差不多可以定在80左右)后会释放一部分heap.percent,但不会释放disk.percent
ram.percent:一直挺高,物理内存的使用情况,Lucene会将闲置的内存都占用做cache,如果有应用使用内存时,cache会被释放出来
cpu:一般不会很高,5以下吧,如果有很多query的话会高,如果一直高释放不掉可能查询语句有问题
node.role:【mdi】master  data  i查询接口(可否在节点上查询)

[zhang@node2 ~]$ curl localhost:19200/_cat/nodes?v
ip            heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.243.74           50          98   0    0.09    0.08     0.10 di        -      node5
192.168.243.35           39          90   0    0.11    0.19     0.23 mdi       -      node2
192.168.243.11           57          99   1    0.19    0.14     0.14 mdi       *      node1
192.168.243.51           65          95   0    0.07    0.09     0.13 di        -      node4
192.168.243.40           50          95   0    0.05    0.14     0.20 mdi       -      node3

4.2、查询节点健康状态

[zhang@node2 ~]$ curl localhost:19200/_cat/health?v
epoch      timestamp cluster status node.total node.data shards  pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1561104338 16:05:38  zhang green           5         5   3074 157326    0    0        0             0                  -                100.0%

4.3、查询节点

disk.percent显示占用的硬盘空间

[zhang@node2 ~]$ curl 'localhost:19200/_cat/allocation?v&s=node'
shards disk.indices disk.used disk.avail disk.total disk.percent host          ip            node
   615        1.9tb     9.2tb      8.9tb     18.2tb           51 192.168.243.11 192.168.243.11 node1
   615        2.3tb     8.7tb      9.4tb     18.2tb           47 192.168.243.35 192.168.243.35 node2
   614        2.2tb     7.6tb     10.5tb     18.2tb           42 192.168.243.40 192.168.243.40 node3
   615        2.2tb     7.5tb     10.6tb     18.2tb           41 192.168.243.51 192.168.243.51 node4
   615        2.1tb     7.1tb       11tb     18.2tb           39 192.168.243.74 192.168.243.74 node5

4.4、查询索引

有很多,本文只摘取了一部分。

[zhang@node2 ~]$ curl 'localhost:19200/_cat/indices?v&s=index'        //查看索引,并以索引名字排序(s=sort)
health status index                        uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   bin_power_idx-2000.07.10     ggf3VQMbRXmhzIunDDQ1Ow   2   1      54384            0     24.3gb         12.1gb
       close  bin_power_idx-2018.12.29     u4RfroS4QlGT49bFlFRqJg                                                          
green  open   meter_status_idx-2012.10.01  y6pqRWC3TmaAT5ZNfXOxSw   2   1      33335            0     18.7mb          9.3mb
green  open   power_json-7326403-2019.04     EZGhuUBUSA28nspkFL_ibQ   2   1   59416810            0     31.5gb         15.7gb
green  open   power_json-7326406-2019.02     hVow4ibaRAS2kQtLXPbCJQ   2   1  115093304            0     58.3gb         29.1gb
green  open   power_json-7326419-2019.06 uZfizhIHS361y9-u5FvgAw   2   1      97416            0       42mb         20.9mb

[zhang@node2 ~]$ curl 'localhost:19200/_cat/indices/power_json*?v&s=index' //只查看power_json索引,并以索引名字排序(s=sort) health status index uuid pri rep docs.count docs.deleted store.size pri.store.size green open power_json-7326403-2019.04 EZGhuUBUSA28nspkFL_ibQ 2 1 59416810 0 31.5gb 15.7gb green open power_json-7326406-2019.02 hVow4ibaRAS2kQtLXPbCJQ 2 1 115093304 0 58.3gb 29.1gb green open power_json-7326419-2019.06 uZfizhIHS361y9-u5FvgAw 2 1 97416 0 42mb 20.9mb

4.5、查询索引的具体记录信息

[zhang@node2 ~]$ curl 'localhost:19200/power_json*?pretty'
{
  "power_json-7326414-2019.01" : {
    "aliases" : { },
    "mappings" : {
      "_default_" : {
        "dynamic_templates" : [
          {
            "string_fields" : {
              "match" : "*",
              "unmatch" : "BIN*",
              "match_mapping_type" : "string",
              "mapping" : {
                "ignore_above" : 256,
                "type" : "keyword"
              }
            }
          },
          {
            "date_fields" : {
              "match" : "TIME",
              "mapping" : {
                "type" : "date"
              }
            }
          },
          {
            "bin_fields" : {
              "match" : "BIN*",
              "mapping" : {
                "type" : "binary"
              }
            }
          }
        ]
      },
      "power_json" : {
        "dynamic_templates" : [
          {
            "string_fields" : {
              "match" : "*",
              "unmatch" : "BIN*",
              "match_mapping_type" : "string",
              "mapping" : {
                "ignore_above" : 256,
                "type" : "keyword"    //字符串类型
              }
            }
          },
          {
            "date_fields" : {
              "match" : "TIME",
              "mapping" : {
                "type" : "date"
              }
            }
          },
          {
            "bin_fields" : {
              "match" : "BIN*",
              "mapping" : {
                "type" : "binary"
              }
            }
          }
        ],
        "properties" : {
          "CENTRAL" : {
            "type" : "long"
          },
          "CITY" : {
            "type" : "keyword",
            "ignore_above" : 256
          },
          "CT" : {
            "type" : "float"
          },
          "GLYS
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值