ES 嵌套对象数据问题

一,数据结构
"properties": {
    "type": { "type": "keyword" } ,
    "title": { "type": "text", "analyzer": "ik_smart" }, 
    "long_title": { "type": "text", "analyzer": "ik_smart" }, 
    "category_id": { "type": "integer" },
    "category": { "type": "keyword" },
    "category_path": { "type": "keyword" },
    "description": { "type": "text", "analyzer": "ik_smart" },
    "price": { "type": "scaled_float", "scaling_factor": 100 },
    "on_sale": { "type": "boolean" },
    "rating": { "type": "float" },
    "sold_count": { "type": "integer" },
    "review_count": { "type": "integer" },
    "skus": {
      "type": "nested",
      "properties": {
        "title": { "type": "text", "analyzer": "ik_smart", "copy_to": "skus_title" }, 
        "description": { "type": "text", "analyzer": "ik_smart", "copy_to": "skus_description" },
        "price": { "type": "scaled_float", "scaling_factor": 100 }
      }
    },
    "properties": {
      "type": "nested",
      "properties": {
        "name": { "type": "keyword" }, 
        "value": { "type": "keyword", "copy_to": "properties_value" }
      }
    }
  }
二,场景

分面查询条件

{
						"nested": {
							"path": "properties",
							"query": [
								{
									"term": {
										"properties.name": "外包装颜色"
									}
								},
								{
									"term": {
										"properties.value": "白色"
									}
								}
							]
						}
					}

我想要的结果是外包装属性值为白色的产品,结果会多出来另外一个属性机器颜色也是白色的产品。
原因是 ES 对于json对象数组的做了压扁处理,比如上面的例子在 ES真实 存储的结构是这样的

[
    "properties" => [
        "name"  => ["外包装颜色", "机器颜色"],
        "value" => ["白色", "黑色"]
    ]
]

很明显,这样的结构丢失了属性名称和属性值的关联,导致查询的时候,出现失效,如果业务要求实现精准搜索,那么这种方案是不满足要求的。我使用新增一个字段,来解决这个问题,当然还有别的方法可以解决(详见此文章

"properties": {
    "type": { "type": "keyword" } ,
    "title": { "type": "text", "analyzer": "ik_smart" }, 
    "long_title": { "type": "text", "analyzer": "ik_smart" }, 
    "category_id": { "type": "integer" },
    "category": { "type": "keyword" },
    "category_path": { "type": "keyword" },
    "description": { "type": "text", "analyzer": "ik_smart" },
    "price": { "type": "scaled_float", "scaling_factor": 100 },
    "on_sale": { "type": "boolean" },
    "rating": { "type": "float" },
    "sold_count": { "type": "integer" },
    "review_count": { "type": "integer" },
    "skus": {
      "type": "nested",
      "properties": {
        "title": { "type": "text", "analyzer": "ik_smart", "copy_to": "skus_title" }, 
        "description": { "type": "text", "analyzer": "ik_smart", "copy_to": "skus_description" },
        "price": { "type": "scaled_float", "scaling_factor": 100 }
      }
    },
    "properties": {
      "type": "nested",
      "properties": {
        "name": { "type": "keyword" }, 
        "value": { "type": "keyword", "copy_to": "properties_value" },
        "search_value": { "type": "keyword" }
      }
    }
  }

新增一个search_value ,把属性名和属性值拼接之后存入这个字段,然后在筛选时也将属性名和属性值拼接,并精确匹配 search_value 字段即可。

相关推荐

  1. ES 嵌套对象数据问题

    2024-03-29 12:36:04       44 阅读
  2. 问题数组对象去重

    2024-03-29 12:36:04       68 阅读
  3. ES6简化对象

    2024-03-29 12:36:04       50 阅读
  4. ES6中 对象合并

    2024-03-29 12:36:04       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-29 12:36:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-29 12:36:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-29 12:36:04       87 阅读
  4. Python语言-面向对象

    2024-03-29 12:36:04       96 阅读

热门阅读

  1. 面试算法-125-除自身以外数组的乘积

    2024-03-29 12:36:04       35 阅读
  2. 【OpenModelica】4命令行大全

    2024-03-29 12:36:04       43 阅读
  3. .jayy勒索病毒来袭:如何有效防范与应对?

    2024-03-29 12:36:04       40 阅读
  4. 【ssh免密设置】

    2024-03-29 12:36:04       43 阅读
  5. 王道c语言-顺序查找、二分查找

    2024-03-29 12:36:04       47 阅读
  6. C语言和C++实现Stack有什么区别?

    2024-03-29 12:36:04       44 阅读
  7. C#中的PLINQ和LINQ的效率对比

    2024-03-29 12:36:04       39 阅读
  8. go实现哈夫曼编码

    2024-03-29 12:36:04       45 阅读