ES-DEMO

本文介绍了使用Elasticsearch进行复杂查询的方法,通过多个示例详细展示了如何构建查询条件、聚合数据以及过滤结果等技巧。

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

demo1

{
    "size": 0,
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "subscribe": 1
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "term": {
                                    "mid": "55"
                                }
                            },
                            {
                                "term": {
                                    "mid": "65"
                                }
                            },
                            {
                                "term": {
                                    "mid": "70"
                                }
                            },
                            {
                                "term": {
                                    "mid": "71"
                                }
                            },
                            {
                                "term": {
                                    "mid": "86"
                                }
                            }
                        ]
                    }
                }
            ],
            "must_not": {
                "term": {
                    "unid": ""
                }
            }
        }
    },
    "aggs": {
        "group_by_unid": {
            "terms": {
                "field": "unid",
  "size":0
            }
        }
    }
}

demo2

sql:$sql = "SELECT count(*) as count,type FROM user_action_record WHERE wid={$mid} AND type in('menu','custom_data_event','lbs','message') AND created_date>={$startintdate} AND created_date<={$endintdate} GROUP BY type";

{
  "size": 0,
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "wid": 33
              }
            },
            {
              "range": {
                "created_date": {
                  "gte": "20160701",
                  "lte": "20160731"
                }
              }
            },
            {
              "bool": {
                "should": [
                  {
                    "term": {
                      "type": "menu"
                    }
                  },
                  {
                    "term": {
                      "type": "custom_data_event"
                    }
                  },
                  {
                    "term": {
                      "type": "lbs"
                    }
                  },
                  {
                    "term": {
                      "type": "message"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  },
  "aggs": {
    "group_by_type": {
      "terms": {
        "field": "type"
      }
    }
  }
}

demo3

sql:"SELECT wid as mid,openid,type,create_time FROM user_action_record WHERE wid={$mid} AND openid ='{$order['openid']}' AND create_time>='{$beforeday}' AND create_time<='{$order['create_time']}' AND type not in ($filter_str) ORDER BY create_time DESC LIMIT 1;

{
  "size": 1,
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "terms": {
                "wid": 33,
              }
            },
            {
              "term": {
                "openid": "oOuXGtyW8yZM7EZ_IfLsKgTJr6hM"
              }
            },
            {
              "range": {
                "create_time": {
                  "gte": "0000000000",
                  "lte": "1472659200"
                }
              }
            },
            {
              "bool": {
                "must_not": [
                  {
                    "term": {
                      "type": "subscribe"
                    }
                  },
                  {
                    "term": {
                      "type": "unsubscribe"
                    }
                  },
                  {
                    "term": {
                      "type": "scratch"
                    }
                  },
                  {
                    "term": {
                      "type": "wheel_of_fortune"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  },
  "sort": {
    "create_time": {
      "order": "desc"
    }
  }
}

demo4

sql: SELECT count(DISTINCT(openid)) AS taged FROM wechat_customer_tag WHERE mid = 71 AND STATUS = 1 AND create_time < '2016-09-19 23:59:59';

{
  "aggs": {
    "followers_counts": {
      "value_count": {
        "field": "id"
      }
    }
  },
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "mid": 33
              }
            },
            {
              "has_child": {
                "type": "wechat_customer_tag",
                "query": {
                  "filtered": {
                    "filter": {
                      "bool": {
                        "must": [
                          {
                            "term": {
                              "mid": "33"
                            }
                          },
                          {
                            "term": {
                              "status": 1
                            }
                          },
                          {
                            "range": {
                              "create_time": {
                                "lte": "1474300799"
                              }
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}

demo5

{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "mid": "35"
              }
            },
            {
              "has_child": {
                "type": "user_action_record",
                "query": {
                  "filtered": {
                    "filter": {
                      "bool": {
                        "must": [
                          {
                            "term": {
                              "wid": "35"
                            }
                          },
                          {
                            "terms": {
                              "type": [
                                "read",
                                "readmore",
                                "shares_reads",
                                "share_post"
                              ]
                            }
                          },
                          {
                            "wildcard": {
                              "keyword": "*42@0*"
                            }
                          },
                          {
                            "range": {
                              "create_time": {
                                "from": 1471708800,
                                "to": 1474387199
                              }
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              }
            }
          ]
        }
      }
    }
  },
  "aggregations": {
    "group_by_sex": {
      "terms": {
        "field": "sex"
      }
    }
  }
}

demo6

{
  "query": {
    "bool": {
      "must": {
        "term": {
          "mid": 33
        }
      }
    }
  },
  "size": 0,
  "aggs": {
    "group_by_tag": {
      "terms": {
        "field": "tag_id_integer",
        "order": {
          "tagcount": "desc"
        },
        "size": 0
      },
      "aggs": {
        "tagcount": {
          "sum": {
            "field": "count"
          }
        }
      }
    }
  }
}

demo7

SELECT count(*) as click FROM user_action_record WHERE wid={$mid} AND type='menu' AND created_date>={$startdate} AND created_date<={$enddate}";

{
  "aggs": {
    "followers_counts": {
      "value_count": {
        "field": "id"
      }
    }
  },
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "wid": 71
              }
            },
            {
              "term": {
                "type": "menu"
              }
            },
            {
              "range": {
                "created_date": {
                  "gte": "20160901",
                  "lte": "20160930"
                }
              }
            }
          ]
        }
      }
    }
  }
}

转载于:https://my.oschina.net/wsyblog/blog/752846

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值