您当前的位置:首页 > 学海无涯 > 心得笔记网站首页心得笔记
elasticsearch查询语法
发布时间:2021-01-12作者:♂逸風★淩軒
术语查询:术语查询匹配字段中包含一个或多个精确术语的文档。
比如:要匹配响应字段为200的文档:
response:200
如将文档与消息字段中的短语“quick brown fox”匹配。
message:"quick brown fox"
布尔查询:KQL支持or、and和not。默认情况下,和的优先级高于或。若要覆盖默认优先级,请将运算符分组到括号中。
比如:要匹配响应为200、扩展名为php或两者都有的文档:
response:200 or extension:php
要匹配响应为200且扩展名为php的文档:
response:200 and extension:php
匹配响应为200或404的文档。
response:(200 or 404)
要匹配响应为200且扩展名为php或css的文档:
response:200 and (extension:php or extension:css)
要匹配响应为200且扩展名为php或扩展名为css且响应为anything的文档:
response:200 and extension:php or extension:css
范围查询:KQL在数字和日期类型上支持>、>=、<和<=。
比如:
account_number >= 100 and items_sold <= 200 and @timestamp >= now-5m
存在查询:exist查询匹配包含字段值的文档,在本例中为response:
response:*
通配符查询:匹配文档的位置机器操作系统以win开头
比如:“windows 7”和“windows 10”:
machine.os:win*
要匹配多个字段:
machine.os*:windows 10
附:
# -*- coding: UTF-8 -*- import requests import json import os,sys from app.config import es_host,es_account,es_passwd,es_ssl_enable,es_ca_path from elasticsearch import Elasticsearch from ssl import create_default_context import urllib3 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) if es_ssl_enable == "1": context = create_default_context(cafile=es_ca_path) es = Elasticsearch(es_host,use_ssl=True,verify_certs=False,client_cert=es_ca_path,http_auth=(es_account, es_passwd), timeout=30) else: es = Elasticsearch(es_host, http_auth=(es_account,es_passwd), timeout=30) def es_search(type,es_index,SearchQuery,es_key): if type == "1": k='["'+es_key.replace('.','"]["')+'"]' try: result=eval('es.search(index=es_index, q=SearchQuery,size=1)["hits"]["hits"][0]["_source"]' + k) except: result = eval('es.search(index=es_index, q=SearchQuery,size=1)["hits"]["hits"][0]' + k) return {"es_key": result} else: return {"es_key":result} elif type == "2": result = es.count(index=es_index, q=SearchQuery)["count"] return {"count":result} elif type == "3" or type == "4" : result = es.search(index=es_index,body={"size":0,"_source":{"excludes":[]},"aggs":{"interface":{"terms":{"field":es_key,"size":30,"order":{"_count":"desc"}}}},"stored_fields":["*"],"script_fields":{},"docvalue_fields":["@timestamp"]},q=SearchQuery)['aggregations']['interface']['buckets'] if type == "3" : fieldlist = [] for i in result: fieldlist.append(i["key"]) return {"list":json.dumps(fieldlist)} elif type == "4" : return {"count":result} elif type == "5" : return {"count":len(result)}
关键字词:elasticsearch,查询

下一篇:Python制作WHL文件
相关文章
-
无相关信息