day116-モールビジネス-検索サービス-検索DSLテスト-集約部分

1.マッピングのコンテンツを変更します 

古いマッピングは次のとおりです。多くのフィールドのインデックスとdoc_valuesがfalseであることがわかります。これらのフィールドは、取得とコンテンツの表示に使用されません。

{
  "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"
        }
      }
    }
  }
}

次のように変更し、新しいマッピングgulimall_productを作成します(すべてfalseを削除します)

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.古いデータのインデックスを再作成します。これは、データの移行に相当します。

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

3.3。

そして、古いデータをデータに挿入します 

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.集計分析を追加します

次の3つが含まれます。分類されたブランド属性。属性は次のように集約属性です。最初にネストを追加してパスを指定します。

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
              }
            }
          }
        }
      }
    }
  }
}

検索結果は以下のとおりです。

{
  "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
        }
      }
    ]
  }
}

 これで、DSLの基本的なレビューはほぼ完了しました。次の記事では、コードを使用してDSLを実装します。

おすすめ

転載: blog.csdn.net/JavaCoder_juejue/article/details/115383764