ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Elasticsearch概念及查询语法

2021-11-29 10:30:29  阅读:217  来源: 互联网

标签:indicator level ip term 查询 语法 xx Elasticsearch threat


Elasticsearch概念及查询语法

ES 是使用 Java 编写的一种开源搜索引擎,它在内部使用 Lucene 做索引与搜索,通过对 Lucene 的封装,隐藏了 Lucene 的复杂性,取而代之的提供一套简单一致的 RESTful API。

然而,Elasticsearch 不仅仅是 Lucene,并且也不仅仅只是一个全文搜索引擎。

它可以被下面这样准确的形容:

一个分布式的实时文档存储,每个字段可以被索引与搜索。
一个分布式实时分析搜索引擎。
能胜任上百个服务节点的扩展,并支持 PB 级别的结构化或者非结构化数据。

官网对 Elasticsearch 的介绍是 Elasticsearch 是一个分布式、可扩展、近实时的搜索与数据分析引擎。

1 、Es查询字符串检索

1.1 查看索引列表

http://xx.xx.xxx.xx:9200/_cat/indices

1.2 查某个索引内的所有数据

例如:查indicator_ip内所有数据:

http://xx.xx.xxx.xx:9200/indicator_ip/_search?q=*

1.3 Pretty:按照json格式输出

http://xx.xx.xxx.xx:9200/indicator_ip/_search?q=*&pretty

1.4 Sort:指定字段排序

例如:indicator_ip内的数据按modified排序

http://xx.xx.xxx.xx:9200/indicator_ip/_search?sort=modified:desc&pretty

1.5 *values:模糊搜索某个字段包含values的数据

例如:查indicator_ip内values字段值包含61.161的所有数据

http://xx.xx.xxx.xx:9200/indicator_ip/_search?q=values:*61.161&pretty

1.6 _source:只输出指定字段

例如:只输出indicator_ip内modified字段

http://xx.xx.xxx.xx:9200/indicator_ip/_search?_source=modified&pretty

1.7 组合查询

例如:查询indicator_ip内threat_level为1 并且 threat_types为9的数据

http://xx.xx.xxx.xx:9200/indicator_ip/_search?q=threat_level:1 AND threat_types:9&pretty

1.8 范围查询

例如:查询indicator_ip内threat_level取值1-5之间的数据,不包含1,5

http://xx.xx.xxx.xx:9200/indicator_ip/_search?q=threat_level:{1 TO 5}&pretty

2、 ES结构化检索

推荐火狐es插件: Elasticvue
推荐谷歌es插件: ElasticSearch Head

2.1 term 过滤

term主要用于精确匹配哪些值,比如数字,日期,布尔值或 not_analyzed 的字符串(未经分析的文本数据类型):

{ “term”: { “age”: 26 }}
{ “term”: { “date”: “2014-09-01” }}
{ “term”: { “public”: true }}
{ “term”: { “tag”: “full_text” }}

例如:查询indicator_ip内threat_level取值为1 的数据:

{ 
  "query": { 
    "term": { 
      "hostname": "xxx" 
    } 
  } 
}

2.2 terms 过滤

terms 跟 term 有点类似,但 terms 允许指定多个匹配条件。 如果某个字段指定了多个值,那么文档需要一起去做匹配:
模板:

{ "terms": { 
    "tag": [ "search", "full_text", "nosql" ] 
    } 
}

例如:查询indicator_ip内threat_level取值为1,3,5 的数据

{ 
  "query": { 
    "terms": { 
      "threat_level": [1,3,5] 
    } 
  } 
}

2.3 range 过滤

range过滤允许我们按照指定范围查找一批数据:
范围操作符包括:
gt : 大于
gte: 大于等于
lt : 小于
lte: 小于等于
例如:查询indicator_ip索引内modified值为当前时间和2021年11月17日0点之间 的数据

{
	"query": {
		"range": {
			"modified": {
			    "gt":"2021-11-17T00:00:00.000Z",
			    "lt":"now"
			}
		}
	},
	"size": 10,
	"from": 0,
	"sort": []
}

2.4 bool过滤、联合查询must,should,must_not,filter**

bool 过滤可以用来合并多个过滤条件查询结果的布尔逻辑,它包含一下操作符:
must : 多个查询条件的完全匹配,相当于 and。
must_not : 多个查询条件的相反匹配,相当于 not。
should : 至少有一个查询条件匹配, 相当于 or。filter: 作用与must一直,不过must会有评分,而filter仅仅只是过滤,所以性能更高bool: 下一层级可以包含must,should,must_not,filter,当bool的的下一层级(must,should,must_not,filter)都返回true,则当前bool返回true(返回true表示当前条件匹配)
例如:查询indicator_ip表中threat_level为1、modified在当前和2021年11月15日时间范围内 且confidence为80 或者credit_level为1 的数据

{
	"query": {
		"bool": {
			"must": [
				{
					"term": {
						"threat_level": 1
					}
				},
				{
					"range": {
						"modified": {
							"gt": "2021-11-15T06:27:11.000Z",
							"lt": "now"
						}
					}
				},
				{
					"bool": {
						"should": [
							{
								"term": {
									"confidence": 80
								}
							},
							{
								"term": {
									"credit_level": 1
								}
							}
						]
					}
				}
			]
		}
	},
	"size": 10,
	"from": 0,
	"sort": []
}

注意should无法与另外三个并列使用,如果并列使用,should会失效

标签:indicator,level,ip,term,查询,语法,xx,Elasticsearch,threat
来源: https://blog.csdn.net/qq_45481507/article/details/121556195

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有