2017-03-16 51 views
2

我有订单和订购产品作为Elastic Search中的子数组。当我汇总价格时,我需要在我的订单文件中过滤我的订单产品。在弹性我的文档我可以在Elasticsearch中过滤子数组吗?

例子:

{ 
    "OrderID":4567488, 
    "projectId":"4", 
    "Project":"direkt", 
    "legacy_id":null, 
    "supporterId":null, 
    "Origin":"FR", 
    "orderProducts":[ 
     { 
     "OrderProductID":"15694898", 
     "OrderID":"4567488", 
     "brandNo":"30", 
     "Price":"26.95", 
     }, 
     { 
     "OrderProductID":"15694898", 
     "OrderID":"4567488", 
     "brandNo":"15", 
     "Price":"15.22", 
     }, 
     { 
     "OrderProductID":"15694898", 
     "OrderID":"4567488", 
     "brandNo":"123", 
     "Price":"24.55", 
     }, 
    ] 
} 

如何IM过滤器现在:

{ 
     "index":"order_index", 
     "from":0, 
     "size":100, 
     "body":{ 
     "query":{ 
      "filtered":{ 
       "filter":{ 
        "bool":{ 
        "must":[ 
         { 
          "term":{ 
           "orderProducts.brandNo":"30" 
          } 
         } 
        ], 
        } 
       } 
      } 
     } 
     } 
    } 

什么,我期待

{ 
    "OrderID":4567488, 
    "projectId":"4", 
    "Project":"direkt", 
    "legacy_id":null, 
    "supporterId":null, 
    "Origin":"FR", 
    "orderProducts":[ 
     { 
     "OrderProductID":"15694898", 
     "OrderID":"4567488", 
     "brandNo":"30", 
     "Price":"26.95", 
     }, 
    ] 
} 

我真正得到:

全部文件。

这可能吗?要过滤子数据?

UPD。

是的,这是我的架构映射:

"mappings":{ 
    "order":{ 
     "dynamic_templates":[ 
     { 
      "strings":{ 
       "mapping":{ 
        "type":"string", 
        "fields":{ 
        "raw":{ 
         "index":"not_analyzed", 
         "type":"string" 
        } 
        } 
       }, 
       "match_mapping_type":"string" 
      } 
     } 
     ], 
     "properties":{ 
     "orderProducts":{ 
      "include_in_parent":true, 
      "properties":{ 
       "OrderProductID":{ 
        "type":"long" 
       }, 
       "OrderID":{ 
        "type":"long" 
       }, 
       "brandNo":{ 
        "type":"long" 
       }, 
       "Price":{ 
        "type":"double" 
       } 

      }, 
      "type":"nested" 
     }, 
     "OrderID":{ 
      "type":"long" 
     } 
     } 
    } 
}, 
+1

你可以显示你的模式映射吗? – user3775217

+0

@ user3775217添加到发布我的架构 – sandra1n

回答

0

好吧,一些实验后,我发现,那聚集可以这样做:

{ 
    "aggs":{ 
     "sales":{ 
     "nested":{ 
      "path":"orderProducts" 
     }, 
     "aggs":{ 
      "filtered_nestedobjects":{ 
       "filter":{ 
        "bool":{ 
        "must":[ 
         { 
          "terms":{ 
           "orderProducts.brandNo":[ 
           "30" 
           ] 
          } 
         } 
        ] 
        } 
       }, 
       "aggs":{ 
        "Quantity":{ 
        "sum":{ 
         "field":"orderProducts.Quantity" 
        } 
        } 
       } 
      } 
     } 
     } 
    } 
} 

而且回答的主要问题我们是否可以过滤子弹弹性。与inner_hits只有我做到了这一点。

相关问题