美文网首页ElasticSearch实战笔记
36、nested 类型查询以及聚合操作

36、nested 类型查询以及聚合操作

作者: 众神开挂 | 来源:发表于2020-04-22 23:15 被阅读0次

主要内容:nested 类型查询以及聚合操作

1、nested object

冗余数据方式的来建模,其实用的就是object类型,我们这里又要引入一种新的object类型,nested object类型

修改mapping,将comments的类型从object设置为nested

PUT /blogs
{
  "mappings": {
    "properties": {
      "comments": {
        "type": "nested",
        "properties": {
          "name": {
            "type": "keyword"
          },
          "comment": {
            "type": "keyword"
          },
          "age": {
            "type": "short"
          },
          "stars": {
            "type": "short"
          },
          "date": {
            "type": "date"
          }
        }
      }
    }
  }
}

插入数据:

PUT blogs/_doc/6
{
  "title": "花无缺发表的一篇帖子",
  "content":  "我是花无缺,大家要不要考虑一下投资房产和买股票的事情啊。。。",
  "tags":  [ "投资", "理财" ],
  "comments": [ 
    {
      "name":    "小鱼儿",
      "comment": "什么股票啊?推荐一下呗",
      "age":     28,
      "stars":   4,
      "date":    "2016-09-01"
    },
    {
      "name":    "黄药师",
      "comment": "我喜欢投资房产,风,险大收益也大",
      "age":     31,
      "stars":   5,
      "date":    "2016-10-22"
    }
  ]
}

针对nested类型进行搜索

GET blogs/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "花无缺"
          }
        },
        {
          "nested": {
            "path": "comments",
            "score_mode": "avg", ##  max,min,avg,none,默认是avg
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "comments.name": "黄药师"
                    }
                  },
                  {
                    "match": {
                      "comments.age": 31
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

score_mode:如果搜索命中了多个nested document,如何将多个nested document的分数合并为一个分数

2、针对nested 类型进行数据分析

我们讲解一下基于nested object中的数据进行聚合分析

聚合数据分析的需求1:按照评论日期进行bucket划分,然后拿到每个月的评论的评分的平均值

GET /blogs/_search 
{
  "size": 0, 
  "aggs": {
    "comments_path": {
      "nested": {
        "path": "comments"
      }, 
      "aggs": {
        "group_by_comments_date": {
          "date_histogram": {
            "field": "comments.date",
            "calendar_interval": "month",
            "format": "yyyy-MM"
          },
          "aggs": {
            "avg_stars": {
              "avg": {
                "field": "comments.stars"
              }
            }
          }
        }
      }
    }
  }
}
GET /blogs/_search 
{
  "size": 0,
  "aggs": {
    "comments_path": {
      "nested": {
        "path": "comments"
      },
      "aggs": {
        "group_by_comments_age": {
          "histogram": {
            "field": "comments.age",
            "interval": 10
          },
          "aggs": {
            "reverse_path": {
              "reverse_nested": {},  ## 根据外层的tag进行划分
              "aggs": {
                "group_by_tags": {
                  "terms": {
                    "field": "tags.keyword"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

相关文章

  • 36、nested 类型查询以及聚合操作

    主要内容:nested 类型查询以及聚合操作 1、nested object 冗余数据方式的来建模,其实用的就是o...

  • ElasticSearch 7.x 聚合查询

    聚合查询 |ES 的聚合查询和MyQL的聚合查询类型,ES 的聚合查询相比MySQL 要强大的多,ES提供的统计数...

  • 【详细教程】一文参透MongoDB聚合查询

    MongoDB聚合查询 什么是聚合查询 聚合操作主要用于处理数据并返回计算结果。聚合操作将来自多个文档的值组合在一...

  • 【MySql】mysql语法

    修改操作 UPDATE: 删除操作 DELETE 模糊查询 字段控制查询 聚合函数 sum avg max mi...

  • MongoDB Compass Aggregation的几个应用

    聚合管道,是对查询的数据进行聚合等操作,在MongoDB Compass中,此页面可以创建多个聚合操作进行数据处理...

  • zset类型聚合查询

    ZUNIONSTORE 语法 计算给定的一个或多个有序集的并集,其中给定 key 的数量必须以 numkeys 参...

  • es不简易指南

    1.nested Object mapping及查询 进阶:Elasticsearch之Nested(嵌套)系列、...

  • ES7学习笔记(十)聚合查询

    聚合查询,它是在搜索的结果上,提供的一些聚合数据信息的方法。比如:求和、最大值、平均数等。聚合查询的类型有很多种,...

  • MongoDB杂谈(二)

    聚合,帮助我们统计的操作,查询操作主要的目的是寻找数据,聚合的目的统计数据 $group 管道 $group 就是...

  • redis(九:数据结构-集合)

    Sorted Set 只支持范围查询,无法直接进行聚合计算(聚合计算是CPU密集型任务)。Set 类型可以进行聚合...

网友评论

    本文标题:36、nested 类型查询以及聚合操作

    本文链接:https://www.haomeiwen.com/subject/zkkumhtx.html