栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 前沿技术 > 大数据 > 大数据系统

es - elasticsearch - aggs - bucket - composite

es - elasticsearch - aggs - bucket - composite

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

问:composite有什么特点?
答:

问:composite如何使用?
答:

# 删除
DELETE /composite_test

# 映射
PUT /composite_test
{
  "mappings": {
    "properties": {
      "name": {"type": "keyword"},
      "num": {"type": "integer"},
      "date": {"type": "date"}
    }
  }
}

# 索引
POST /composite_test/_bulk?routing=1&refresh
{"index": {"_id": 1}}
{"name": "hello", "num": 3, "date": "2021-09-29"}
{"index": {"_id": 2}}
{"name": "good", "num": 5, "date": "2020-09-29"}
{"index": {"_id": 3}}
{"name": "me", "num": 5, "date": "2024-09-29"}
{"index": {"_id": 4}}
{"name": "hello", "num": 7, "date": "2024-09-29"}


# 搜索
GET /composite_test/_search
{
  "size": 0,
  "aggs": {
    "com_aggs": {
      "composite": {
        "sources": [
          {
            "names": {
              "terms": {
                "field": "name"
              }
            }
          }
        ]
      }
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "com_aggs" : {
      "after_key" : {
        "names" : "me"
      },
      "buckets" : [
        {
          "key" : {
            "names" : "good"
          },
          "doc_count" : 1
        },
        {
          "key" : {
            "names" : "hello"
          },
          "doc_count" : 2
        },
        {
          "key" : {
            "names" : "me"
          },
          "doc_count" : 1
        }
      ]
    }
  }
}

# 搜索
GET /composite_test/_search
{
  "size": 0,
  "aggs": {
    "com_aggs": {
      "composite": {
        "sources": [
          {
            "nums": {
              "histogram": {
                "field": "num",
                "interval": 2
              }
            }
          }
        ]
      }
    }
  }
}

# 结果
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "com_aggs" : {
      "after_key" : {
        "nums" : 6.0
      },
      "buckets" : [
        {
          "key" : {
            "nums" : 2.0
          },
          "doc_count" : 1
        },
        {
          "key" : {
            "nums" : 4.0
          },
          "doc_count" : 2
        },
        {
          "key" : {
            "nums" : 6.0
          },
          "doc_count" : 1
        }
      ]
    }
  }
}

# 搜索
GET /composite_test/_search
{
  "size": 0,
  "aggs": {
    "com_aggs": {
      "composite": {
        "sources": [
          {
            "dates": {
              "date_histogram": {
                "field": "date",
                "calendar_interval": "1d",
                "format": "yyyy-MM-dd"
              }
            }
          }
        ]
      }
    }
  }
}

# 结果
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "com_aggs" : {
      "after_key" : {
        "dates" : "2024-09-29"
      },
      "buckets" : [
        {
          "key" : {
            "dates" : "2020-09-29"
          },
          "doc_count" : 1
        },
        {
          "key" : {
            "dates" : "2021-09-29"
          },
          "doc_count" : 1
        },
        {
          "key" : {
            "dates" : "2024-09-29"
          },
          "doc_count" : 2
        }
      ]
    }
  }
}

# 搜索 - 组合统计
GET /composite_test/_search
{
  "size": 0,
  "aggs": {
    "com_aggs": {
      "composite": {
        "sources": [
          {
            "names": {
              "terms": {
                "field": "name"
              }
            }
          },{
            "nums": {
              "histogram": {
                "field": "num",
                "interval": 2
              }
            }
          },{
            "dates": {
              "date_histogram": {
                "field": "date",
                "calendar_interval": "1d",
                "format": "yyyy-MM-dd"
              }
            }
          }
        ]
      }
    }
  }
}

# 结果
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "com_aggs" : {
      "after_key" : {
        "names" : "me",
        "nums" : 4.0,
        "dates" : "2024-09-29"
      },
      "buckets" : [
        {
          "key" : {
            "names" : "good",
            "nums" : 4.0,
            "dates" : "2020-09-29"
          },
          "doc_count" : 1
        },
        {
          "key" : {
            "names" : "hello",
            "nums" : 2.0,
            "dates" : "2021-09-29"
          },
          "doc_count" : 1
        },
        {
          "key" : {
            "names" : "hello",
            "nums" : 6.0,
            "dates" : "2024-09-29"
          },
          "doc_count" : 1
        },
        {
          "key" : {
            "names" : "me",
            "nums" : 4.0,
            "dates" : "2024-09-29"
          },
          "doc_count" : 1
        }
      ]
    }
  }
}
转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/280001.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号