es常见查询
ES集群管理
ES提供了一套_cat API, 可以查看ES中的各类数据.
多条件查询
1.关键词:"china"
2.发布时间:"2021-09-13 00:00:00","2021-09-22 13:42:27"
3.排除站点:aol.com
4.排除国建:中国-CHN
GET /new*/_count
{
"query":{
"bool":{
"must":[
{
"term":{
"text":{
"value":"china"
}
}
},{
"term": {
"mediaLevel": {
"value": "5"
}
}
}
],
"must_not": [
{
"term": {
"countryCode": {
"value": "CHN"
}
}
},
{
"term": {
"domain": {
"value": "aol.com"
}
}
}
],
"filter":{
"range":{
"pubTime":{
"gte":1631462400000,
"lte":16322893470007
}
}
}
}
}
}
多条件分组查询
1.查询以上条件,并且按照sourceType进行分组:
GET /new*/_search
{
"query":{
"bool":{
"must":[
{
"term":{
"text":{
"value":"china"
}
}
}
],
"must_not": [
{
"term": {
"countryCode": {
"value": "CHN"
}
}
},
{
"term": {
"domain": {
"value": "aol.com"
}
}
}
],
"filter":{
"range":{
"pubTime":{
"gte":1631462400000,
"lte":16322893470007
}
}
}
}
},
"size":0,
"aggs": {
"group_by_sourceType": {
"terms": {
"field": "sourceType",
"order": {
"_count": "asc"
}
}
}
}
}
查询某一个字段不为空
extend不为空
GET /social_user/_search
{
"query": {
"bool": {
"must": {
"exists": {
"field": "extend"
}
}
}
}
}
in or not in查询
GET /news*/_search
{
"query": {
"terms": {
"siteUrls": [
"https://www.bechtel.com/newsroom/releases/",
"https://www.bechtel.com/newsroom/coverage/"
]
}
},
"aggs": {
"distinct_sourcetype": {
"terms": {
"field": "sourceType"
}
}
},
"stored_fields": ["sourceType","siteUrls"]
}
多条件聚合
GET /news*/_search
{
"query": {
"terms": {
"siteUrls": [
"https://www.rolls-royce.com/media/press-releases.aspx"
]
}
},
"aggs": {
"distinct_sourcetype": {
"terms": {
"field": "sourceType"
}
},
"max_aggs":{
"max": {
"field": "pubTime"
}
},
"min_ahhs":{
"min": {
"field": "pubTime"
}
}
},
"stored_fields": ["sourceType","siteUrls","pubTime"]
}
ES针对于text类型的数据进行模糊匹配查询使用:query_string
系统日志存储在es的logstash下,然后是每天生成一个日志,其对应的mapping如下:
"logstash-2021.10.28" : {
"mappings" : {
"properties" : {
"class" : {
"type" : "keyword"
},
"kubernetes" : {
"properties" : {
"container_hash" : {
"type" : "keyword"
},
"container_image" : {
"type" : "keyword"
},
"container_name" : {
"type" : "keyword"
},
"docker_id" : {
"type" : "keyword"
},
"host" : {
"type" : "keyword"
},
"namespace_name" : {
"type" : "keyword"
},
"pod_id" : {
"type" : "keyword"
},
"pod_name" : {
"type" : "text"
}
}
},
"level" : {
"type" : "keyword"
},
"log" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"log_flie_path" : {
"type" : "text"
},
"log_time" : {
"type" : "keyword"
},
"message" : {
"type" : "text"
},
"source" : {
"type" : "keyword"
},
"stream" : {
"type" : "keyword"
},
"thread" : {
"type" : "text"
},
"time" : {
"type" : "date",
"format" : "strict_date_optional_time_nanos"
},
"timestamp" : {
"type" : "date"
}
}
}
}
查询日志如下:
GET /logstash-2021.11.10/_search
{
"query": {
"bool": {
"must": [
{
"query_string": {
"default_field": "kubernetes.pod_name",
"query": "\"gtcom-governance-news-k8s-kafka-test-taskmanager\""
}
}
],
"filter": [
{
"term": {
"level": "ERROR"
}
}
]
}
},
"sort": [
{
"timestamp": {
"order": "desc"
}
}
],
"_source": ["message","level","kubernetes.pod_name"]
}
根据日期聚合查询
GET /news*/_search
{
"query": {
"bool": {
"filter": [
{
"range": {
"createTime": {
"gte": "2021-11-01"
}
}
}
]
}
},
"aggs": {
"NAME": {
"date_histogram": {
"field": "createTime",
"interval": "day"
}
}
}
}
推荐阅读
阿里云https免费证书申请
项目牵涉到做一个语音在线实时收集,然后进行asr识别的B/S应用。所以需要浏览器调用“麦克风”来获取用户语音。此时需要在localhost或者https下才能实现这样的功能。类似于google的语音翻译,如下所示:
startshineye阅读 122
美团外卖搜索基于Elasticsearch的优化实践
美团外卖搜索工程团队在Elasticsearch的优化实践中,基于Location-Based Service(LBS)业务场景对Elasticsearch的查询性能进行优化。该优化基于Run-Length Encoding(RLE)设计了一款高效的倒排索引结构,使检索...
美团技术团队赞 1阅读 800
Elasticsearch 按照标签匹配个数优先排序查询
首先最外层的数组就是我们通常写的query语句,放在body中进行请求的,主要看query里面的结构,这种需要自定义脚本处理评分的,query中只放了一个script_score:
kumfo赞 2阅读 725
elasticsearch的开发应用(2)
Query DSL:ElasticSearch提供了一个可以执行的JSON风格的DSL(domain-specific language 领域特定语言),这个被称为Query DSL。
KerryWu赞 1阅读 672
ElasticSearch必知必会-基础篇
定义: 相同文档结构(Mapping)文档的结合 由唯一索引名称标定 一个集群中有多个索引 不同的索引代表不同的业务类型数据 注意事项: 索引名称不支持大写 索引名称最大支持255个字符长度 字段的名称,支持大写,...
京东云开发者赞 2阅读 357
Elasticsearch 7.x 保留字符(qbit)
前言本文对 Elasticsearch 7.x 有效query_string 保留字符官方文档: [链接] {代码...} 在 query_string 里面做通配符匹配时,空格需要转义regex 保留字符官方文档:[链接] {代码...} query_string 与 regex 保留...
qbit阅读 1.6k
跨机房ES同步实战
众所周知单个机房在出现不可抗拒的问题(如断电、断网等因素)时,会导致无法正常提供服务,会对业务造成潜在的损失。所以在协同办公领域,一种可以基于同城或异地多活机制的高可用设计,在保障数据一致性的同时...
京东云开发者赞 1阅读 708
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。