通过嵌套字段值将弹性返回的命中分开

时间:2019-05-22 08:15:18

标签: elasticsearch kibana nest

我已经在那里索引了产品。我正在尝试通过嵌套字段值来区分从弹性返回的匹配。我的索引缩短了:

{
  "mapping": {
    "product": {
      "properties": {        
        "id": {
          "type": "integer"
        },
        "model_name": {
          "type": "text",          
        },
        "variants": {
          "type": "nested",
          "properties": {
            "attributes": {
              "type": "nested",
              "properties": {
                "id": {
                  "type": "integer"
                },
                "name": {
                  "type": "text"
                },
                "product_attribute_id": {
                  "type": "integer"
                },               
                "value": {
                  "type": "text"
                }
              }
            },
            "id": {
              "type": "integer"
            },
            "product_id": {
              "type": "integer"
            }            
          }
        }
      }
    }
  }
}

还有产品示例(产品中还有更多的变体和属性-我只是将其切除):

{
   "_index":"product_index",
   "_type":"product",
   "id":192,
   "model_name":"Some tshirt",
   "variants":[
      {
         "id":1271,
         "product_id":192,
         "attributes":[
            {
               "id":29,
               "name":"clothesSize",
               "value":"XL",
               "product_attribute_id":36740
            }
         ]
      },
      {
         "id":1272,
         "product_id":192,
         "attributes":[
            {
               "id":29,
               "name":"clothesSize",
               "value":"L",
               "product_attribute_id":36741
            }
         ]
      }
   ]
} 

有问题的字段是属性ID。假设我想按大小属性-id 29来分隔产品。如果响应看起来像这样,那将是完美的:

"hits" : [
{
   "_index":"product_index",
   "_type":"product",
   "id":192,
   "model_name":"Some tshirt",
   "variants":[
      {
         "id":1271,
         "product_id":192,
         "attributes":[
            {
               "id":29,
               "name":"clothesSize",
               "value":"XL",
               "product_attribute_id":36740
            }
         ]
      }
   ]
},     
{
   "_index":"product_index",
   "_type":"product",
   "id":192,
   "model_name":"Some tshirt",
   "variants":[      
      {
         "id":1272,
         "product_id":192,
         "attributes":[
            {
               "id":29,
               "name":"clothesSize",
               "value":"L",
               "product_attribute_id":36741
            }
         ]
      }
   ]
}]

我考虑过将弹性请求中的所有变体分开,然后通过那些属性在应用程序侧将它们分组,但我认为这不是最优雅的方法,最重要的是,高效的方法。 我应该对哪些弹性关键字感兴趣?
预先感谢您的帮助。

0 个答案:

没有答案