Elasticsearch-----nested查询 多重聚合与嵌套

查询数量

 [root@localhost ~]# curl -XGET 192.168.0.***:9200/****/_count?pretty

{
  "count" : 8406117,
  "_shards" : {
    "total" : 9,
    "successful" : 9,
    "failed" : 0
  }
}

简单聚合

curl -XGET 192.168.0.***:9200/*****/_search?pretty -d '

{
  "size": 0,
  "aggs": {
    "my_group_by_cxz": {
      "terms": {
        "field": "cxz"
      }
    }
  }
}'

查看所有库
curl -XGET 192.168.1.*****:9200/_cat/indices?V


查询某库中的mapping:
curl -XGET 192.168.1.****:9200/******/_mapping?pretty

条件查询

返回内容参数 _source 、排序 sort

curl -XGET 192.168.0.****:9200/job/type1/_search?pretty -d 
'{
  "query": {
    "match_all": {}
  },
  "from": 1,
  "size": 2,
  "_source": [
    "date",
    "words"
  ],
  "sort": [
    {
      "words": "desc"
    }
  ]
}'

返回date、words数据内容  按照words数据降序排序

 

查询语句  must  must_not

{
  "filter": {
    "bool": {
      "must": [
        {
          "range": {
            "hka": {
              "gte": 370400,
              "lte": 370499
            }
          }
        }
      ],
      "must_not": [
        {
          "range": {
            "regionid": {
              "gte": 370000,
              "lte": 379999
            }
          }
        }
      ]
    }
  },
  "_source": [
    "iname"
  ],
  "size": 100
}

嵌套查询

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "inner_hits": {},
            "path": "work",
            "filter": {
              "query": {
                "bool": {
                  "must": [
                    {
                      "queryString": {
                        "query": "",
                        "fields": [
                          "work.company"
                        ],
                        "auto_generate_phrase_queries": true
                      }
                    },
                    {
                      "term": {
                        "work.islast": 1
                      }
                    }
                  ]
                }
              }
            }
          }
        },
        {
          "range": {
            "updatetime": {
              "gte": 1546272000000
            }
          }
        }
      ],
      "must_not": [
        {
          "term": {
            "shouji": 110
          }
        }
      ]
    }
  },
  "size": 4000,
  "_source": [
    "id",
    "iname",
    "shouji"
  ]
}

多重聚合与嵌套

{
  "size": 0,
  "aggs": {
    "per_count": {
      "nested": {
        "path": "edus"
      },
      "aggs": {
        "zym_term": {
          "terms": {
            "field": "edus.zym",
            "size": 1000
          }
        }
      }
    }
  },
  "query": {
    "nested": {
      "path": "edus",
      "query": {
        "bool": {
          "must_not": [
            {
              "terms": {
                "edus.zym": [
                  "",
                  "不限",
                  "其他"
                ]
              }
            }
          ]
        }
      }
    }
  },
  "bool": {
    "must": [
      {
        "range": {
          "regionid": {
            "gte": 110000,
            "lte": 119999
          }
        }
      }
    ]
  }
}



reindex 重建索引

索引的数据结构(mapping)建立后可以增加字段,但不能修改已存在的字段,如果要修改已存在的字段,只能重建索引。

1、新建一个索引,新索引的名称一般使用 原索引名称_重建日期,eg. handlemonitorlog_20210111 ,mapping可以复制原来的mapping来改

2、将数据同步到新索引中

#同步等待方式
POST /_reindex
{
    "source": {
        "index": "handlemonitorlog"
    },
    "dest": {
        "index": "handlemonitorlog_20210111"
    }
}


#如果同步时间过⻓可以使用异步方式。
POST /_reindex?wait_for_completion=false  #url中加一个参数即可 wait_for_completion=false
{
    "source": {
        "index": "handlemonitorlog"
    },
    "dest": {
        "index": "handlemonitorlog_20210111"
    }
}

别名的使用

#reindex 重建索引
POST /_reindex
{
  "source": {
    "index": "handlemonitorlog"
  },
  "dest": {
    "index": "handlemonitorlog1"
  }
}

#查询所有的别名
GET /_alias

#查询指定index的别名
GET /handlemonitorlog/_alias

#别名的增删改


#add是新增,remove是删除,更新别名:先remove再add
POST /_aliases
{
  "actions": [
    {
      "remove": {  
        "index": "handlemonitorlog1",
        "alias": "log1"
      }
    }
    ,{
       "add": {
        "index": "handlemonitorlog1",
        "alias": "log"
      }
      }
  ]
}

refresh 刷新数据

es默认每隔1s刷新1次索引,就是说增删改后可能不会立刻同步,需要等待0~1s

#增删改时,可以在url中添加?refresh,操作后立刻刷新索引

PUT /handlemonitorlog/_doc/1?refresh  
{
	//......
}


#可以修改自动刷新的默认时间间隔
#默认值1s。-1表示关闭自动刷新,需要手动刷新

PUT /handlemonitorlog/_settings
{
    "index": {
        "refresh_interval": "5s"  
    }
}

高亮查询

GET businssstatisticslog/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "industry": "其他金融业"
        }}
      ]
    }
  },
  "highlight": {
    "fields": {
       "*" : {}
    },
    "require_field_match": "false",
    "pre_tags": ["<span style='color:red;'>"],
    "post_tags": ["</span>"]
  }
}

结果:

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现 Elasticsearch嵌套聚合查询,你可以使用 Elasticsearch 的 AggregationBuilders 类来创建嵌套聚合查询。具体实现步骤如下: 1. 创建一个嵌套聚合查询,用于组合多个子聚合查询。 2. 在嵌套聚合查询中添加多个子聚合查询,分别对应不同的聚合方式。 3. 如果你需要对子聚合查询进行分组,可以在子聚合查询中添加 terms 聚合查询。 4. 如果你需要对子聚合查询进行计数,可以在子聚合查询中添加 count 聚合查询。 5. 执行查询并处理结果。 以下是一个示例 Java 代码,用于实现 Elasticsearch嵌套聚合查询: ``` SearchResponse response = client.prepareSearch("index_name") .addAggregation( AggregationBuilders.nested("nested_agg", "nested_field") .subAggregation(AggregationBuilders.terms("term_agg") .field("term_field")) .subAggregation(AggregationBuilders.count("count_agg") .field("count_field"))) .execute() .actionGet(); Nested nestedAgg = response.getAggregations().get("nested_agg"); Terms termAgg = nestedAgg.getAggregations().get("term_agg"); long totalCount = nestedAgg.getAggregations().get("count_agg").getDocCount(); ``` 其中,"index_name" 是你要查询的索引名称,"nested_field" 是你要进行嵌套聚合查询的字段名称,"term_field" 和 "count_field" 分别是你要进行分组和计数的字段名称。你可以根据实际情况进行修改。执行完查询后,你可以从查询结果中获取嵌套聚合对象,并进一步获取子聚合对象的结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值