1.修改映射中的内容
老的映射为如下,可以看到 许多字段的 index及 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"
}
}
},
"autoGeneratedTimestamp": {
"type": "long"
},
"brandId": {
"type": "long"
},
"brandImg": {
"type": "keyword"
},
"brandName": {
"type": "keyword"
},
"catalogId": {
"type": "long"
},
"catalogName": {
"type": "keyword"
},
"description": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"hasStock": {
"type": "boolean"
},
"hotScore": {
"type": "long"
},
"parentTask": {
"properties": {
"id": {
"type": "long"
},
"nodeId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"set": {
"type": "boolean"
}
}
},
"refreshPolicy": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"retry": {
"type": "boolean"
},
"saleCount": {
"type": "long"
},
"shouldStoreResult": {
"type": "boolean"
},
"skuId": {
"type": "long"
},
"skuImg": {
"type": "keyword"
},
"skuPrice": {
"type": "keyword"
},
"skuTitle": {
"type": "text",
"analyzer": "ik_smart"
},
"spuId": {
"type": "keyword"
}
}
}
}
2.将老的数据reindex过来,相当于迁移数据
POST _reindex
{
"source": {
"index": "product"
},
"dest": {
"index": "gulimall_product"
}
}
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.添加聚合分析
包含三个:分类品牌属性,其中属性是聚合属性,如下,要先加nested指定path
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