day116-Mall Business-Search service-Search DSL test-Aggregation part

1. Modificar el contenido en el mapeo. 

La asignación anterior es la siguiente, puede ver que el índice y los valores de documento de muchos campos son falsos, entonces estos campos no se utilizarán para la recuperación y visualización de contenido.

{
  "product" : {
    "mappings" : {
      "properties" : {
        "attrs" : {
          "type" : "nested",
          "properties" : {
            "attrId" : {
              "type" : "long"
            },
            "attrName" : {
              "type" : "keyword",
              "index" : false,
              "doc_values" : false
            },
            "attrValue" : {
              "type" : "keyword"
            }
          }
        },
        "brandId" : {
          "type" : "long"
        },
        "brandImg" : {
          "type" : "keyword",
          "index" : false,
          "doc_values" : false
        },
        "brandName" : {
          "type" : "keyword",
          "index" : false,
          "doc_values" : false
        },
        "catelogId" : {
          "type" : "long"
        },
        "catelogName" : {
          "type" : "keyword",
          "index" : false,
          "doc_values" : false
        },
        "hasStock" : {
          "type" : "boolean"
        },
        "hotScore" : {
          "type" : "long"
        },
        "saleCount" : {
          "type" : "long"
        },
        "skuId" : {
          "type" : "long"
        },
        "skuImg" : {
          "type" : "keyword",
          "index" : false,
          "doc_values" : false
        },
        "skuPrice" : {
          "type" : "keyword"
        },
        "skuTitle" : {
          "type" : "text",
          "analyzer" : "ik_smart"
        },
        "spuId" : {
          "type" : "keyword"
        }
      }
    }
  }
}

Modifique de la siguiente manera y cree un nuevo mapeo gulimall_product (elimine todo falso)

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"
        },
        "catelogId" : {
          "type" : "long"
        },
        "catelogName" : {
          "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"
        }
      }
    }
}

2. Vuelva a indexar los datos antiguos, lo que equivale a migrar datos.

POST _reindex
{
  "source": {
    "index": "product"
  },
  "dest": {
    "index": "gulimall_product"
  }
}

3.

E inserte los datos antiguos en los datos 

GET product/_search
{
  "query": {
  "bool": {
    "must": [
      {
        "match": {
          "skuTitle": "华为"
        }
      }
    ],
    "filter": [
    {
      "term": {
        "hasStock": "false"
      }
    },
    {
      "terms": {
        "brandId": [
          "6",
          "10"
        ]
      }
    },{
        "nested": {
         "path": "attrs",
         "query": {
           "bool": {
             "must": [
               {
                 "term": {
                   "attrs.attrId": {
                     "value": "1"
                   }
                 }
               }
             ]
           }
         }
       }
    },{
      "range": {
        "skuPrice": {
          "gte": 0,
          "lte": 4000
        }
      }
    }
    ]
  }
  },
  "sort": [
    {
      "skuPrice": {
        "order": "desc"
      }
    }
  ],
  "from": 0,
  "size": 2,
    "highlight": {
    "fields": {"skuTitle": {}},
    "pre_tags": "<b style='color:red'>",
    "post_tags": "</b>"
  },
  "aggs": {
    "brand_agg": {
      "terms": {
        "field": "brandId",
        "size": 10
      },
      "aggs": {
        "brandNameAgg": {
          "terms": {
            "field": "brandName",
            "size": 10
          }
        },
        "brandImgAgg": {
          "terms": {
            "field": "brandImg",
            "size": 10
          }
        }
      }
    },
    "catalogAgg": {
      "terms": {
        "field": "catelogId",
        "size": 10
      },
      "aggs": {
        "catalogNameAgg": {
          "terms": {
            "field": "catelogName",
            "size": 10
          }
        }
      }
    }
  }
}

3. Agregar análisis de agregación

Contiene tres: atributos de marca clasificados, donde los atributos son atributos agregados, de la siguiente manera, primero agregue anidado para especificar la ruta

GET gulimall_product/_search
{
  "query": {
  "bool": {
    "must": [
      {
        "match": {
          "skuTitle": "华为"
        }
      }
    ],
    "filter": [
    {
      "term": {
        "hasStock": "false"
      }
    },
    {
      "terms": {
        "brandId": [
          "6",
          "10"
        ]
      }
    },{
        "nested": {
         "path": "attrs",
         "query": {
           "bool": {
             "must": [
               {
                 "term": {
                   "attrs.attrId": {
                     "value": "1"
                   }
                 }
               }
             ]
           }
         }
       }
    },{
      "range": {
        "skuPrice": {
          "gte": 0,
          "lte": 4000
        }
      }
    }
    ]
  }
  },
  "sort": [
    {
      "skuPrice": {
        "order": "desc"
      }
    }
  ],
  "from": 0,
  "size": 2,
    "highlight": {
    "fields": {"skuTitle": {}},
    "pre_tags": "<b style='color:red'>",
    "post_tags": "</b>"
  },
  "aggs": {
    "brand_agg": {
      "terms": {
        "field": "brandId",
        "size": 10
      },
      "aggs": {
        "brandNameAgg": {
          "terms": {
            "field": "brandName",
            "size": 10
          }
        },
        "brandImgAgg": {
          "terms": {
            "field": "brandImg",
            "size": 10
          }
        }
      }
    },
    "catalogAgg": {
      "terms": {
        "field": "catelogId",
        "size": 10
      },
      "aggs": {
        "catalogNameAgg": {
          "terms": {
            "field": "catelogName",
            "size": 10
          }
        }
      }
    },
    "attrAgg": {
      "nested": {
        "path": "attrs"
      },
      "aggs": {
      "attrIdAgg": {
          "terms": {
            "field": "attrs.attrId",
            "size": 10
          },
          "aggs": {
            "attrNameAgg": {
              "terms": {
                "field": "attrs.attrName",
                "size": 10
              }
            }
          }
        }
      }
    }
  }
}

Los resultados de la búsqueda son los siguientes

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 16,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "gulimall_product",
        "_type" : "_doc",
        "_id" : "17",
        "_score" : 1.0,
        "_source" : {
          "attrs" : [
            {
              "attrId" : 1
            },
            {
              "attrId" : 5
            },
            {
              "attrId" : 6
            }
          ],
          "brandId" : 6,
          "brandImg" : "https://gulimall-juege.oss-cn-shanghai.aliyuncs.com/2021-01-01/7a9a3407-5406-4348-8e47-669127bdc37f_2020-10-14_211111.png",
          "brandName" : "华为",
          "catelogName" : "手机",
          "hasStock" : true,
          "hotScore" : 0,
          "saleCount" : 0,
          "skuId" : 17,
          "skuImg" : "https://gulimall-juege.oss-cn-shanghai.aliyuncs.com/2021-01-05/d73733ba-b8ec-4ee9-8bc5-938566075851_b9840ff8da7762ae.jpg",
          "skuPrice" : 2000.0,
          "skuTitle" : "麟985 5G SoC芯片 6400万魅力四摄 66W华为超级快充 8号色全网通5G手机",
          "spuId" : 3
        }
      },
      {
        "_index" : "gulimall_product",
        "_type" : "_doc",
        "_id" : "18",
        "_score" : 1.0,
        "_source" : {
          "attrs" : [
            {
              "attrId" : 1
            },
            {
              "attrId" : 5
            },
            {
              "attrId" : 6
            }
          ],
          "brandId" : 6,
          "brandImg" : "https://gulimall-juege.oss-cn-shanghai.aliyuncs.com/2021-01-01/7a9a3407-5406-4348-8e47-669127bdc37f_2020-10-14_211111.png",
          "brandName" : "华为",
          "catelogName" : "手机",
          "hasStock" : true,
          "hotScore" : 0,
          "saleCount" : 0,
          "skuId" : 18,
          "skuImg" : "https://gulimall-juege.oss-cn-shanghai.aliyuncs.com/2021-01-05/d73733ba-b8ec-4ee9-8bc5-938566075851_b9840ff8da7762ae.jpg",
          "skuPrice" : 3000.0,
          "skuTitle" : "麟985 5G SoC芯片 6400万魅力四摄 66W华为超级快充 8号色全网通5G手机",
          "spuId" : 3
        }
      },
      {
        "_index" : "gulimall_product",
        "_type" : "_doc",
        "_id" : "19",
        "_score" : 1.0,
        "_source" : {
          "attrs" : [
            {
              "attrId" : 1
            },
            {
              "attrId" : 5
            },
            {
              "attrId" : 6
            }
          ],
          "brandId" : 6,
          "brandImg" : "https://gulimall-juege.oss-cn-shanghai.aliyuncs.com/2021-01-01/7a9a3407-5406-4348-8e47-669127bdc37f_2020-10-14_211111.png",
          "brandName" : "华为",
          "catelogName" : "手机",
          "hasStock" : false,
          "hotScore" : 0,
          "saleCount" : 0,
          "skuId" : 19,
          "skuImg" : "",
          "skuPrice" : 4000.0,
          "skuTitle" : "麟985 5G SoC芯片 6400万魅力四摄 66W华为超级快充 8号色全网通5G手机",
          "spuId" : 3
        }
      },
      {
        "_index" : "gulimall_product",
        "_type" : "_doc",
        "_id" : "20",
        "_score" : 1.0,
        "_source" : {
          "attrs" : [
            {
              "attrId" : 1
            },
            {
              "attrId" : 5
            },
            {
              "attrId" : 6
            }
          ],
          "brandId" : 6,
          "brandImg" : "https://gulimall-juege.oss-cn-shanghai.aliyuncs.com/2021-01-01/7a9a3407-5406-4348-8e47-669127bdc37f_2020-10-14_211111.png",
          "brandName" : "华为",
          "catelogName" : "手机",
          "hasStock" : false,
          "hotScore" : 0,
          "saleCount" : 0,
          "skuId" : 20,
          "skuImg" : "",
          "skuPrice" : 5000.0,
          "skuTitle" : "麟985 5G SoC芯片 6400万魅力四摄 66W华为超级快充 8号色全网通5G手机",
          "spuId" : 3
        }
      },
      {
        "_index" : "gulimall_product",
        "_type" : "_doc",
        "_id" : "21",
        "_score" : 1.0,
        "_source" : {
          "attrs" : [
            {
              "attrId" : 1
            },
            {
              "attrId" : 5
            },
            {
              "attrId" : 6
            }
          ],
          "brandId" : 6,
          "brandImg" : "https://gulimall-juege.oss-cn-shanghai.aliyuncs.com/2021-01-01/7a9a3407-5406-4348-8e47-669127bdc37f_2020-10-14_211111.png",
          "brandName" : "华为",
          "catelogName" : "手机",
          "hasStock" : false,
          "hotScore" : 0,
          "saleCount" : 0,
          "skuId" : 21,
          "skuImg" : "",
          "skuPrice" : 2000.0,
          "skuTitle" : "麟985 5G SoC芯片 6400万魅力四摄 66W华为超级快充 8号色全网通5G手机",
          "spuId" : 3
        }
      },
      {
        "_index" : "gulimall_product",
        "_type" : "_doc",
        "_id" : "22",
        "_score" : 1.0,
        "_source" : {
          "attrs" : [
            {
              "attrId" : 1
            },
            {
              "attrId" : 5
            },
            {
              "attrId" : 6
            }
          ],
          "brandId" : 6,
          "brandImg" : "https://gulimall-juege.oss-cn-shanghai.aliyuncs.com/2021-01-01/7a9a3407-5406-4348-8e47-669127bdc37f_2020-10-14_211111.png",
          "brandName" : "华为",
          "catelogName" : "手机",
          "hasStock" : false,
          "hotScore" : 0,
          "saleCount" : 0,
          "skuId" : 22,
          "skuImg" : "",
          "skuPrice" : 3000.0,
          "skuTitle" : "麟985 5G SoC芯片 6400万魅力四摄 66W华为超级快充 8号色全网通5G手机",
          "spuId" : 3
        }
      },
      {
        "_index" : "gulimall_product",
        "_type" : "_doc",
        "_id" : "23",
        "_score" : 1.0,
        "_source" : {
          "attrs" : [
            {
              "attrId" : 1
            },
            {
              "attrId" : 5
            },
            {
              "attrId" : 6
            }
          ],
          "brandId" : 6,
          "brandImg" : "https://gulimall-juege.oss-cn-shanghai.aliyuncs.com/2021-01-01/7a9a3407-5406-4348-8e47-669127bdc37f_2020-10-14_211111.png",
          "brandName" : "华为",
          "catelogName" : "手机",
          "hasStock" : false,
          "hotScore" : 0,
          "saleCount" : 0,
          "skuId" : 23,
          "skuImg" : "",
          "skuPrice" : 4000.0,
          "skuTitle" : "麟985 5G SoC芯片 6400万魅力四摄 66W华为超级快充 8号色全网通5G手机",
          "spuId" : 3
        }
      },
      {
        "_index" : "gulimall_product",
        "_type" : "_doc",
        "_id" : "24",
        "_score" : 1.0,
        "_source" : {
          "attrs" : [
            {
              "attrId" : 1
            },
            {
              "attrId" : 5
            },
            {
              "attrId" : 6
            }
          ],
          "brandId" : 6,
          "brandImg" : "https://gulimall-juege.oss-cn-shanghai.aliyuncs.com/2021-01-01/7a9a3407-5406-4348-8e47-669127bdc37f_2020-10-14_211111.png",
          "brandName" : "华为",
          "catelogName" : "手机",
          "hasStock" : false,
          "hotScore" : 0,
          "saleCount" : 0,
          "skuId" : 24,
          "skuImg" : "",
          "skuPrice" : 5000.0,
          "skuTitle" : "麟985 5G SoC芯片 6400万魅力四摄 66W华为超级快充 8号色全网通5G手机",
          "spuId" : 3
        }
      },
      {
        "_index" : "gulimall_product",
        "_type" : "_doc",
        "_id" : "25",
        "_score" : 1.0,
        "_source" : {
          "attrs" : [
            {
              "attrId" : 1
            },
            {
              "attrId" : 5
            },
            {
              "attrId" : 6
            }
          ],
          "brandId" : 6,
          "brandImg" : "https://gulimall-juege.oss-cn-shanghai.aliyuncs.com/2021-01-01/7a9a3407-5406-4348-8e47-669127bdc37f_2020-10-14_211111.png",
          "brandName" : "华为",
          "catelogName" : "手机",
          "hasStock" : false,
          "hotScore" : 0,
          "saleCount" : 0,
          "skuId" : 25,
          "skuImg" : "",
          "skuPrice" : 2000.0,
          "skuTitle" : "麟985 5G SoC芯片 6400万魅力四摄 66W华为超级快充 8号色全网通5G手机",
          "spuId" : 3
        }
      },
      {
        "_index" : "gulimall_product",
        "_type" : "_doc",
        "_id" : "26",
        "_score" : 1.0,
        "_source" : {
          "attrs" : [
            {
              "attrId" : 1
            },
            {
              "attrId" : 5
            },
            {
              "attrId" : 6
            }
          ],
          "brandId" : 6,
          "brandImg" : "https://gulimall-juege.oss-cn-shanghai.aliyuncs.com/2021-01-01/7a9a3407-5406-4348-8e47-669127bdc37f_2020-10-14_211111.png",
          "brandName" : "华为",
          "catelogName" : "手机",
          "hasStock" : false,
          "hotScore" : 0,
          "saleCount" : 0,
          "skuId" : 26,
          "skuImg" : "",
          "skuPrice" : 3000.0,
          "skuTitle" : "麟985 5G SoC芯片 6400万魅力四摄 66W华为超级快充 8号色全网通5G手机",
          "spuId" : 3
        }
      }
    ]
  }
}

 Eso es todo, la revisión básica de DSL está casi completa, el siguiente artículo usa código para implementar DSL

Supongo que te gusta

Origin blog.csdn.net/JavaCoder_juejue/article/details/115383764
Recomendado
Clasificación