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

本文介绍Elasticsearch的多种实用查询技巧,包括条件查询、聚合分析、嵌套查询及索引重建等高级功能,并详细解释了如何利用这些功能提高搜索效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

查询数量

 [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>"]
  }
}

结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值