ElasticSearch - 匹配文档中的所有嵌套变体

时间:2017-09-06 14:29:26

标签: json elasticsearch elasticsearch-plugin

我有几种产品,如下所示

{
    "title": "100% Cotton Unstitched Suit For Men",
    "slug": "100-cotton-unstitched-suit-for-men",
    "price": 200,
    "sale_price": 0,
    "vendor_id": 32,
    "featured": 0,
    "viewed": 20,
    "stock": 4,
    "sku": "XXX-B",
    "rating": 0,
    "active": 1,
    "vendor_name": "vendor_name",
    "category": [
        "men_fashion",
        "traditional_clothing",
        "unstitched_fabric"
    ],
    "image": "imagename.jpg",
    "variations": [
        {
            "variation_id": "34",
            "stock": 5,
            "price": 200,
            "variation_image": "",
            "sku": "XXX-C",
            "size": "m",
            "color": "red"
        },
        {
            "variation_id": "35",
            "stock": 5,
            "price": 200,
            "variation_image": "",
            "sku": "XXX-D",
            "size": "l",
            "color": "red"
        }
    ]
}

我正在寻找具有以下所有参数的查询

  1. 获取特定类别的所有产品
  2. 获取所有黑色产品
  3. 获取尺寸l和m
  4. 我当前的查询:

    {
        "size": 15,
        "from": 0,
        "query": {
            "filtered": {
                "filter": {
                    "bool": {
                        "must": [
                            {
                                "match": {
                                    "category": "women_fashion"
                                }
                            },
                            {
                                "nested": {
                                    "path": "variations",
                                    "query": {
                                        "bool": {
                                            "must": [{
                                                    "match": {
                                                        "variations.color": "red"
                                                    }
                                                },
                                                {
                                                    "match": {
                                                        "variations.size": "l"
                                                    }
                                                },
                                                {
                                                    "match": {
                                                        "variations.size": "m"
                                                    }
                                                }
                                            ]
                                        }
                                    }
                                }
                            }
                        ]
                    }
                }
            }
        }
    }
    

    如果我只搜索1个尺寸,它可以正常工作。但是一旦我搜索了2个尺寸,它就没有记录。我的猜测是它在每个嵌套变体中查找所有3个参数,显然它无法找到。我如何修改查询以搜索

    1. 尺寸:m,颜色:黑色
    2. 尺寸:l,颜色:黑色
    3. 我也尝试过使用嵌套过滤器,但问题是当我正在寻找“必须”查询时,它就像“应该”查询一样。即。它显示所有产品都有很大的变化,而我只是想展示大而红的产品。

      我的第二个查询:

      {
          "size": 15,
          "from": 0,
          "query": {
              "filtered": {
                  "filter": {
                      "bool": {
                          "must": [
                              {
                                  "match": {
                                      "category": "women_fashion"
                                  }
                              },
                              {
                                  "nested": {
                                      "path": "variations",
                                      "query": {
                                          "bool":{
                                              "filter": [
                                                  {
                                                      "term": {
                                                          "variations.color": "red"
                                                      }
                                                  },
                                                  {
                                                      "term": {
                                                          "variations.size": "l"
                                                      }
                                                  }
      
                                              ]
                                          }
                                      }
                                  }
                              }
                          ]
                      }
                  }
              }
          }
      }
      

1 个答案:

答案 0 :(得分:0)

您可以将尺寸变体放入其自己的子句中,以便至少有一种尺寸匹配

{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "match": {
                "category": "women_fashion"
              }
            },
            {
              "nested": {
                "path": "variations",
                "query": {
                  "bool": {
                    "must": [
                      {
                        "match": {
                          "variations.color": "red"
                        }
                      },
                      {
                        "query": {
                          "bool": {
                            "should": [
                              {
                                "match": {
                                  "variations.size": "l"
                                }
                              },
                              {
                                "match": {
                                  "variations.size": "m"
                                }
                              }
                            ]
                          }
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}