zoukankan      html  css  js  c++  java
  • Elasticsearch根据查询条件求和sum

    一、概述

    现有news表,数据存放在Elasticsearch中。需要根据关键字查询之后,sum计算点击数。

    数据如下:

    {"content":"变异毒株在国内首次出现社区传播","hits":468}
    {"content":"昆明市委书记:做好象群进主城准备","hits":489}
    {"content":"吸入式新冠疫苗正在申请紧急使用","hits":476}

    注意:hits表示点击数。

    二、效果演示

    环境说明

    操作系统:centos 7.6

    ip地址:192.168.7.160 

    Elasticsearch版本:7.10.1

    初始化数据

    1. 创建索引news

    进入linux系统,手动执行

    curl -XPUT http://localhost:9200/news

    2. 创建一个映射

    curl -XPOST http://localhost:9200/news/_mapping -H 'Content-Type:application/json' -d'
    {
            "properties": {
                "content": {"type": "text"},
                "hits": {"type": "long"}
            }
    
    }'

    3. 索引加入一些文档

    curl -XPOST http://localhost:9200/news/_create/1 -H 'Content-Type:application/json' -d'
    {"content":"变异毒株在国内首次出现社区传播","hits":468}
    '
    
    curl -XPOST http://localhost:9200/news/_create/2 -H 'Content-Type:application/json' -d'
    {"content":"昆明市委书记:做好象群进主城准备","hits":489}
    '
    
    curl -XPOST http://localhost:9200/news/_create/3 -H 'Content-Type:application/json' -d'
    {"content":"吸入式新冠疫苗正在申请紧急使用","hits":476}
    '

    4. 查询数据

    使用postman工具发送GET请求,url地址:http://192.168.7.160:9200/news/_search

    返回数据

    {
        "took": 2,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 3,
                "relation": "eq"
            },
            "max_score": 1,
            "hits": [
                {
                    "_index": "news",
                    "_type": "_doc",
                    "_id": "1",
                    "_score": 1,
                    "_source": {
                        "content": "变异毒株在国内首次出现社区传播",
                        "hits": 468
                    }
                },
                {
                    "_index": "news",
                    "_type": "_doc",
                    "_id": "2",
                    "_score": 1,
                    "_source": {
                        "content": "昆明市委书记:做好象群进主城准备",
                        "hits": 489
                    }
                },
                {
                    "_index": "news",
                    "_type": "_doc",
                    "_id": "3",
                    "_score": 1,
                    "_source": {
                        "content": "吸入式新冠疫苗正在申请紧急使用",
                        "hits": 476
                    }
                }
            ]
        }
    }
    View Code

    可以看到数据有3条

    sum查询

    使用postman工具发送POST请求,url地址:http://192.168.7.160:9200/news/_search

    body参数为:

    {
        "query": {
                    "match" : { "content" : "" }
                },
        "aggs" : {
            "total_hits" : { "sum" : { "field" : "hits" } }
        }
    }

    参数说明:

    查询content内容中,包含"在"的关键字,并sum计算点击数。

    关于aggs下文会有详细说明。

    aggs 聚合的模板

    当query和aggs一起存在时,会先执行query的主查询,主查询query执行完后会搜出一批结果,而这些结果才会被拿去aggs拿去做聚合

    另外要注意aggs后面会先接一层自定义的这个聚合的名字,然后才是接上要使用的聚合桶

    如果有些情况不在意查询结果是什麽,而只在意aggs的结果,可以把size设为0,如此可以让返回的hits结果集是0,加快返回的速度

    一个aggs裡可以有很多个聚合,每个聚合彼此间都是独立的,因此可以一个聚合拿来统计数量、一个聚合拿来分析数据、一个聚合拿来计算标准差...,让一次搜索就可以把想要做的事情一次做完

    此例只定义了1个聚合total_hits,使用sum计算hits字段。

    返回结果:

    {
        "took": 3,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 2,
                "relation": "eq"
            },
            "max_score": 0.4700036,
            "hits": [
                {
                    "_index": "news",
                    "_type": "_doc",
                    "_id": "1",
                    "_score": 0.4700036,
                    "_source": {
                        "content": "变异毒株在国内首次出现社区传播",
                        "hits": 468
                    }
                },
                {
                    "_index": "news",
                    "_type": "_doc",
                    "_id": "3",
                    "_score": 0.4700036,
                    "_source": {
                        "content": "吸入式新冠疫苗正在申请紧急使用",
                        "hits": 476
                    }
                }
            ]
        },
        "aggregations": {
            "total_hits": {
                "value": 944
            }
        }
    }

    注意:

    aggregations 里面已经返回了sum结果,总的点击次数为944。可以计算一下468+476=944

    本文参考链接:

    https://blog.csdn.net/weixin_40341116/article/details/81173016

  • 相关阅读:
    从TCP三次握手说起——浅析TCP协议中的疑难杂症
    动态绑定是如何实现的?
    C++对象的内存模型
    C/C++关键字
    libevent库介绍--事件和数据缓冲
    libevent编程疑难解答
    大型工程多个目录下的Makefile写法
    C++中的RAII机制
    C++中的智能指针
    二叉树的非递归遍历
  • 原文地址:https://www.cnblogs.com/xiao987334176/p/14850492.html
Copyright © 2011-2022 走看看