Elasticsearch学习笔记(四)

聚合查询

sql语句中的having 条件在es聚合中的应用

1.Having count(*) > 1在Elasticsearch 中对应的是 Bucket Filter 聚合,语句如下所示:

GET /spu/_search
{
  "size": 0,
  "aggs": {
    "models": {
      "terms": {
        "field": "categoryId"
      },
      "aggs": {
        "spu_count": {
          "cardinality": {
            "field": "id"
          }
        },
        "spu_count_filter": {
          "bucket_selector": {
            "buckets_path": {
              "spuCount": "spu_count"
            },
            "script": "params.spuCount>1"
          }
        }
      }
    }
  }
}

示例返回结果如下:

{
  "took": 39,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 13,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "models": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "1101",
          "doc_count": 5,
          "spu_count": {
            "value": 4
          }
        },
        {
          "key": "1103",
          "doc_count": 5,
          "spu_count": {
            "value": 5
          }
        },
        {
          "key": "1105",
          "doc_count": 2,
          "spu_count": {
            "value": 2
          }
        }
      ]
    }
  }
}

此时返回结果只包含spu数大于1的类目,但会发现spu数多的1103 不是在第一个位置,我们还需要做排序处理

2.Order By Limit在 Elasticsearch 中可以使用 Bucket Sort 聚合实现,语句如下所示

GET /spu/_search
{
  "size": 0,
  "aggs": {
    "models": {
      "terms": {
        "field": "categoryId"
      },
      "aggs": {
        "spu_count": {
          "cardinality": {
            "field": "id"
          }
        },
        "spu_count_filter": {
          "bucket_selector": {
            "buckets_path": {
              "spuCount": "spu_count"
            },
            "script": "params.spuCount>1"
          }
        },
        "spu_count_sort": {
          "bucket_sort": {
            "sort": {
              "spu_count": "desc"
            },
            "size": 2
          }
        }
      }
    }
  }
}

spu_count_sort 中的size作用相当于sql查询中的limit用于返回数据的前几条

你可能感兴趣的:(elasticsearch,java,sql,mysql,oracle)