es查询

elasticsearch基本查询笔记(一) -- 基本查询

2019-05-07  本文已影响0人  我才不是大神

Search APIs简介

REST request URI

轻便快速的URI查询方法

REST request body

可以有许多限制条件的json格式查询方法

"range": {
      "FIELD": {# 指定具体过滤的字段
        "gte": 1,# gte: >=, gt: >
        "lte": 10
      }
}
"script_fields": {
    "FIELD": {# 指定脚本计算之后值得名称
      "script": {# 脚本内的运算
      }
    }
 }
"aggs": {
    "NAME": {# 指定结果的名称
      "AGG_TYPE": {# 指定具体的聚合方法,
        TODO: # 聚合体内制定具体的聚合字段
      }
    }
    TODO: # 该处可以嵌套聚合
 }

查询

查询所有数据

GET logstash-zlead-web-v3.1.1-2019.03.14/_search

{
  "query": {
    "match_all": {}
  }
}

output:

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 6344,
    "max_score": 1,
    "hits": [
      {
        "_index": "logstash-zlead-api-2019.03",
        "_type": "logs",
        "_id": "Z0pff2kB2B_5yQNjn_8T",
        "_score": 1,
        "_source": {
          "callsite": "ZENSURE.EHandWare.Core.Log.Info",
          "level": "INFO",
          "@timestamp": "2019-03-15T03:22:44.756Z",
          "tags": [
            "api",
            "beats_input_codec_json_applied"
          ],
          "callsite_linenumber": "62",
          "offset": 23030,
          "node_name": "webApi",
          "host": "iZ97uwvkgixopzZ",
      
          "date": "2019-03-15 11:22:39.2948",
          "@version": "1",
          "beat": {
            "name": "iZ97uwvkgixopzZ",
            "hostname": "iZ97uwvkgixopzZ",
            "version": "6.2.2"
          },
          "prospector": {
            "type": "log"
          },
          "message": "验签",
          "counter": "18"
        }
      }
    ]
  }
}

返回数据含义


查询数据总量

GET logstash-zlead-web-v3.1.1-2019.03.14/_count

{
  "query": {
    "match_all": {}
  }
}

output

{
  "count": 4483,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  }
}

根据ID查询

GET logstash-zlead-web-v3.1.1-2019.03.14/logs/iQ7CeWkBhhgPSpnGOCS9

output

{
  "_index": "logstash-zlead-web-v3.1.1-2019.03.14",
  "_type": "logs",
  "_id": "iQ7CeWkBhhgPSpnGOCS9",
  "_version": 1,
  "found": true,
  "_source": {
    "date": "2019-03-14 09:12:35.4887",
    "@timestamp": "2019-03-14T01:12:43.379Z",
    "prospector": {
      "type": "log"
    },
    "beat": {
      "name": "iZ1128g9ft4c8qZ",
      "hostname": "iZ1128g9ft4c8qZ",
      "version": "6.2.2"
    },
    "level": "FATAL",
    "exception": """
System.InvalidOperationException: 此请求已被阻止,因为当用在 GET 请求中时,会将敏感信息透漏给第三方网站。若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet。
   在 System.Web.Mvc.JsonResult.ExecuteResult(ControllerContext context)
   在 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
   在 System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass29.<BeginInvokeAction>b__1b()
   在 System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult)
""",
    "message": "此请求已被阻止,因为当用在 GET 请求中时,会将敏感信息透漏给第三方网站。若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet。,   在 System.Web.Mvc.JsonResult.ExecuteResult(ControllerContext context)   在 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)   在 System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass29.<BeginInvokeAction>b__1b()   在 System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult)",
    "callsite_linenumber": "35",
    "@version": "1",
    "source": """E:\www.wujinyunshang.com\logs\2019-03-14\nlog-Fatal\2019-03-14.json""",
    "tags": [
      "web",
      "beats_input_codec_json_applied"
    ],
    "host": "iZ1128g9ft4c8qZ",
    "callsite": "ZENSURE.EHandWare.WebFramework.ExpFilter.OnException",
    "offset": 56059,
    "counter": "10",
    "node_name": "webAdmin"
  }
}

简单条件搜索

GET logstash-zlead-web-v3.1.1-2019.03.14/_search

{
  "query": {
    "match": {
      "level": "INFO"
    }
  },
  "size": 1
}

output

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3352,
    "max_score": 0.34418565,
    "hits": [
      {
        "_index": "logstash-zlead-web-v3.1.1-2019.03.14",
        "_type": "logs",
        "_id": "pQ7HeWkBhhgPSpnGBCTj",
        "_score": 0.34418565,
        "_source": {
          "date": "2019-03-14 09:17:54.6293",
          "@timestamp": "2019-03-14T01:17:58.787Z",
          "prospector": {
            "type": "log"
          },
          "beat": {
            "name": "iZ1128g9ft4c8qZ",
            "hostname": "iZ1128g9ft4c8qZ",
            "version": "6.2.2"
          },
          "level": "INFO",
          "message": "检测到Token的Cookie值",
          "callsite_linenumber": "68",
          "@version": "1",
          "source": """E:\www.wujinyunshang.com\logs\2019-03-14\nlog-Info\2019-03-14.json""",
          "tags": [
            "web",
            "beats_input_codec_json_applied"
          ],
          "host": "iZ1128g9ft4c8qZ",
          "callsite": "ZENSURE.EHandWare.WebFramework.BaseAgentsAdminController.get_CurrentAgentsManager",
          "offset": 5962,
          "counter": "26",
          "node_name": "webAdmin"
        }
      }
    ]
  }
}

更复杂的搜索

GET logstash-zlead-web-v3.1.1-2019.03.14/_search

{
  "query": {
   "bool": {
    "must": [
      {
        "match": {
          "level": "INFO"
        }
      },
      {
        "match": {
          "callsite_linenumber": 68
        } 
      },
      {
        "match": {
          "offset": 1014364
        }
      }
    ],
    "filter": {
      "range": {
        "offset": {
          "gte": 1014096
        }
      }
    }
   }
  },
  "sort": [
    {
      "offset": {
        "order": "desc"
      }
    }
  ]
}

output

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": null,
    "hits": [
      {
        "_index": "logstash-zlead-web-v3.1.1-2019.03.14",
        "_type": "logs",
        "_id": "eA4Qe2kBhhgPSpnG3Tce",
        "_score": null,
        "_source": {
          "date": "2019-03-14 15:18:15.1137",
          "@timestamp": "2019-03-14T07:18:15.488Z",
          "prospector": {
            "type": "log"
          },
          "beat": {
            "name": "iZ1128g9ft4c8qZ",
            "hostname": "iZ1128g9ft4c8qZ",
            "version": "6.2.2"
          },
          "level": "INFO",
          "message": "检测到Token的Cookie值",
          "callsite_linenumber": "68",
          "@version": "1",
          "source": """E:\www.wujinyunshang.com\logs\2019-03-14\nlog-Info\2019-03-14.json""",
          "tags": [
            "web",
            "beats_input_codec_json_applied"
          ],
          "host": "iZ1128g9ft4c8qZ",
          "callsite": "ZENSURE.EHandWare.WebFramework.BaseAgentsAdminController.get_CurrentAgentsManager",
          "offset": 1014364,
          "counter": "2053",
          "node_name": "webAdmin"
        },
        "sort": [
          1014364
        ]
      }
    ]
  }
}

注:filter的优先级在query中会高于must,即先过滤,后查询


短语搜索

GET logstash-zlead-web-v3.1.1-2019.03.14/_search

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "message": "请求 并增加"
          }
        }
      ]
    }
  }
}

这个搜索会返回message中包含“请求”或者“并增加”的数据,也就是关键词之间默认是or的关系。如果希望精确匹配这个短语呢?

GET logstash-zlead-web-v3.1.1-2019.03.14/_search

{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "message": "请求"
          }
        }
      ]
    }
  }
}

就是用match_phrase查询。

上一篇下一篇

猜你喜欢

热点阅读