侧边栏壁纸
  • 累计撰写 11 篇文章
  • 累计创建 10 个标签
  • 累计收到 3 条评论
隐藏侧边栏

P178集商城业务-检索服务-检索DSL测试-聚合部分相关代码

九怀
2022-01-15 / 0 评论 / 0 点赞 / 98 阅读 / 6,806 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-01-15,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

3.聚合分析

3.1聚合品牌,并显示10个
#聚合
GET product/_search
{
  "query":{
    "match_all": {}
  },
  "aggs": {
    "brand_agg": {
      "terms": {
        "field": "brandId",
        "size": 10
      }
    }
  }
}
3.2聚合品牌,并显示10个+聚合三级分类id
#聚合
GET product/_search
{
  "query":{
    "match_all": {}
  },
  "aggs": {
    "brand_agg": {
      "terms": {
        "field": "brandId",
        "size": 10
      }
    },
    "catalog_agg":{
      "terms": {
        "field": "catalogId",
        "size": 10
      }
    }
  }
}
3.3创建新的索引--迁移老的索引
#查看product索引的数据类型
GET /product/_mapping 

#创建新的索引--迁移老的数据
PUT gulimall_product
{
  "mappings": {
    "properties": {
      "attrs": {
        "type": "nested",
        "properties": {
          "attrId": {
            "type": "long"
          },
          "attrName": {
            "type": "keyword"
          },
          "attrValue": {
            "type": "keyword"
          }
        }
      },
      "brandId": {
        "type": "long"
      },
      "brandImg": {
        "type": "keyword"
      },
      "brandName": {
        "type": "keyword"
      },
      "catalogId": {
        "type": "long"
      },
      "catalogName": {
        "type": "keyword"
      },
      "hasStock": {
        "type": "boolean"
      },
      "hotScore": {
        "type": "long"
      },
      "saleCount": {
        "type": "long"
      },
      "skuId": {
        "type": "long"
      },
      "skuImg": {
        "type": "keyword"
      },
      "skuPrice": {
        "type": "keyword"
      },
      "skuTitle": {
        "type": "text",
        "analyzer": "ik_smart"
      },
      "spuId": {
        "type": "keyword"
      }
    }
  }
}
3.4迁移product索引中的数据到gulimall_product索引
#迁移product索引中的数据到gulimall_product索引
POST _reindex
{
  "source": {
    "index": "product"
  },
  "dest": {
    "index": "gulimall_product"
  }
}



#查询gulimall_product的数据
GET gulimall_product/_search

3.5聚合品牌,并显示10个(子聚合品牌名)+聚合三级分类id
#聚合检索gulimall_product
GET gulimall_product/_search
{
  "query":{
    "match_all": {}
  },
  "aggs": {
    "brand_agg": {
      "terms": {
        "field": "brandId",
        "size": 10
      },
      "aggs": {
        "brand_name_agg": {
          "terms": {
            "field": "brandName",
            "size": 10
          }
        }
      }
    },
    "catalog_agg":{
      "terms": {
        "field": "catalogId",
        "size": 10
      }
    }
  }
}

3.6聚合品牌,并显示10个(子聚合品牌名+子聚合品牌图片)+聚合三级分类id

#聚合检索gulimall_product
GET gulimall_product/_search
{
  "query":{
    "match_all": {}
  },
  "aggs": {
    "brand_agg": {
      "terms": {
        "field": "brandId",
        "size": 10
      },
      "aggs": {
        "brand_name_agg": {
          "terms": {
            "field": "brandName",
            "size": 10
          }
        },
        "brand_img_agg":{
          "terms": {
            "field": "brandImg", #我的这个没显示,填skuImg可以显示
            "size": 10
          }
        }
      }
    },
    "catalog_agg":{
      "terms": {
        "field": "catalogId",
        "size": 10
      }
    }
  }
}
3.7聚合品牌,并显示10个(子聚合品牌名+子聚合品牌图片)+聚合三级分类id(子聚合分类名)
#聚合检索gulimall_product
GET gulimall_product/_search
{
  "query":{
    "match_all": {}
  },
  "aggs": {
    "brand_agg": {
      "terms": {
        "field": "brandId",
        "size": 10
      },
      "aggs": {
        "brand_name_agg": {
          "terms": {
            "field": "brandName",
            "size": 10
          }
        },
        "brand_img_agg":{
          "terms": {
            "field": "brandImg",
            "size": 10
          }
        }
      }
    },
    "catalog_agg":{
      "terms": {
        "field": "catalogId",
        "size": 10
      },"aggs":{
        "catelog_name_agg":{
          "terms": {
            "field": "catalogName",
            "size": 10
          }
        }
      }
    }
  }
}
3.8聚合品牌,并显示10个(子聚合品牌名+子聚合品牌图片)+聚合三级分类id(子聚合分类名)+聚合属性id
#聚合检索gulimall_product
GET gulimall_product/_search
{
  "query":{
    "match_all": {}
  },
  "aggs": {
    "brand_agg": {
      "terms": {
        "field": "brandId",
        "size": 10
      },
      "aggs": {
        "brand_name_agg": {
          "terms": {
            "field": "brandName",
            "size": 10
          }
        },
        "brand_img_agg":{
          "terms": {
            "field": "brandImg",
            "size": 10
          }
        }
      }
    },
    "catalog_agg":{
      "terms": {
        "field": "catalogId",
        "size": 10
      },"aggs":{
        "catelog_name_agg":{
          "terms": {
            "field": "catalogName",
            "size": 10
          }
        }
      }
    },
    "attr_agg":{
      "nested": {
        "path": "attrs"
      },
      "aggs":{
        "attr_id_agg":{
          "terms": {
            "field": "attrs.attrId",
            "size": 10
          }
        }
      }
    }
    
  }
}
3.9聚合品牌,并显示10个(子聚合品牌名+子聚合品牌图片)+聚合三级分类id(子聚合分类名)+聚合属性id(子聚合属性名)
#聚合检索gulimall_product
GET gulimall_product/_search
{
  "query":{
    "match_all": {}
  },
  "aggs": {
    "brand_agg": {
      "terms": {
        "field": "brandId",
        "size": 10
      },
      "aggs": {
        "brand_name_agg": {
          "terms": {
            "field": "brandName",
            "size": 10
          }
        },
        "brand_img_agg":{
          "terms": {
            "field": "brandImg",
            "size": 10
          }
        }
      }
    },
    "catalog_agg":{
      "terms": {
        "field": "catalogId",
        "size": 10
      },"aggs":{
        "catelog_name_agg":{
          "terms": {
            "field": "catalogName",
            "size": 10
          }
        }
      }
    },
    "attr_agg":{
      "nested": {
        "path": "attrs"
      },
      "aggs":{
        "attr_id_agg":{
          "terms": {
            "field": "attrs.attrId",
            "size": 10
          },
          "aggs": {
            "attr_name_agg": {
              "terms": {
                "field": "attrs.attrName",
                "size": 10

3.10

3.11总结

聚合内容总结

=

#聚合
GET gulimall_product/_search
{
  "query":{
    "match_all": {}
  },
  "aggs": {
    "brand_agg": { //聚合商品id
      "terms": {
        "field": "brandId",
        "size": 10
      }
      "aggs": { //子聚合品牌名
        "brand_name_agg": {
          "terms": {
            "field": "brandName",
            "size": 10
          }
        },
        "brand_img_agg":{ //子聚合品牌图片
          "terms": {
            "field": "brandImg", #我的这个没显示,填skuImg可以显示
            "size": 10
          }
        }
      }
    },
    "catalog_agg":{ //聚合三级分类id
      "terms": {
        "field": "catalogId",
        "size": 10
      },"aggs":{
        "catelog_name_agg":{ //子聚合分类名
          "terms": {
            "field": "catalogName",
            "size": 10
}
        }
      }
    },
    "attr_agg":{
      "nested": {
        "path": "attrs"
      },
      "aggs":{
        "attr_id_agg":{ //聚合属性id
          "terms": {
            "field": "attrs.attrId",
            "size": 10
          }
        }
      }
    }
    
  }
}

=

=

=

0

评论区