2016-03-08 90 views
0

我在MySQL-DB中有一些产品和类别。Elasicsearch的嵌套树与多嵌套

每个产品被列入n类。 所有类别都列在树中,因此每个类别都有一个parentCat,直到第一个级别。

现在我正在寻找最好的弹性索引结构来查询我的产品并按类别和子类别对其进行过滤。

我在网上搜索一些日子,但无法找到一个好的解决方案。也许这里的某个人可以帮助我或给我一个提示。

在此先感谢!

回答

0

我建议将文档聚合起来,elasticsearch是一个NoSql记录导向的。 你可以有一个这样的结构:

curl -XPOST "http://192.168.99.100:9200/shop/products" -d' 
{ 
    "name": "White shoes", 
    "sizes": [ 
     40, 
     41, 
     42 
    ], 
    "info": "bla bla bla", 
    "categories": [ 
     { 
     "name": "Social Shoes", 
     "parentCat": { 
      "name": "Social" 
     } 
     }, 
     { 
     "name": "Winter Shoes", 
     "parentCat": { 
      "name": "Winter 2016" 
     } 
     } 
    ] 
}' 

例子查询与消费这种结构,让我们supose,我需要的是有所谓的社会父类,类称为社会鞋

curl -XPOST "http://192.168.99.100:9200/shop/products/_search" -d' 
{ 
    "query": { 
     "nested": { 
      "path": "categories", 
      "query": { 
       "bool": { 
        "must": [ 
        { 
         "match": { 
          "categories.id": 1 
         } 
        }, 
        { 
         "nested": { 
          "path": "categories.parentCat", 
          "query": { 
           "bool": { 
            "must": [ 
             { 
              "match": { 
               "categories.parentCat.id": 1 
              } 
             } 
            ] 
           } 
          } 
         } 
        } 
        ] 
       } 
      } 
     } 
    } 
}' 
关系的产物