DocumentDB对数组进行多个过滤查询

时间:2016-10-13 04:25:08

标签: azure azure-cosmosdb

使用DocumentDB query playground,我正在处理过滤器类型的查询。我的数据中有一组属性,这些属性设置为允许用户按特定属性进行搜索。如果从名称/值集合中的名称中选择了多个项目,则每个属性类型都成为OR语句。如果选择的属性不同(即颜色和大小),则这将成为AND语句。

SELECT food.id,
    food.description,
    food.tags,
    food.foodGroup
FROM food
JOIN tag1 IN food.tags
JOIN tag2 IN food.tags
WHERE (tag1.name = "snacks" OR tag1.name = "granola bars")
AND (tag2.name = "microwave")

此查询在playground

中运行得非常好

主要问题是我有多达12个属性,甚至更多。一旦我点击了5个连接,那就是我的maximum allowed个连接数,所以下面的查询不起作用。 (请注意,这不是游乐场数据,而是我自己的样本)

SELECT s.StyleID FROM StyleSearch s
JOIN a0 in s.Attributes
JOIN a1 in s.Attributes
JOIN a2 in s.Attributes
JOIN a3 in s.Attributes
JOIN a4 in s.Attributes
JOIN a5 in s.Attributes
WHERE (a0 = "color-finish|Grey" OR a0 = "color-finish|Brown" OR a0 = "color-finish|Beige")
AND (a1 = "fabric-type|Polyester" OR a1 = "fabric-type|Faux Leather")
AND (a2 = "design-features|Standard" OR a2 = "design-features|Reclining")
AND (a3 = "style_parent|Contemporary" OR a3 = "style_parent|Modern" OR a3 = "style_parent|Transitional")
AND (a4 = "price_buckets|$1500 - $2000" OR a4 = "price_buckets|$2000 and Up")
AND (a5 = "dimension_width|84 in +")

我不是100%确定我正在使用正确的查询来执行此操作,但是在SQL中工作的下面的简单where子句会在or语句中返回任何匹配,因此我最终得到每个“AND语句中的项目。

SELECT s.StyleID FROM StyleSearch s
JOIN a in s.Attributes
WHERE (a = "color-finish|Grey" OR a = "color-finish|Brown" OR a = "color-finish|Beige")
AND (a = "fabric-type|Polyester" OR a = "fabric-type|Faux Leather")
AND (a = "design-features|Standard" OR a = "design-features|Reclining")
AND (a = "style_parent|Contemporary" OR a = "style_parent|Modern" OR a = "style_parent|Transitional")
AND (a = "price_buckets|$1500 - $2000" OR a = "price_buckets|$2000 and Up")
AND (a = "dimension_width|84 in +")

以下是数据示例:

{
    "StyleID": "chf_12345-bmc",
    "Attributes": [
      "brand|chf",
      "color|red",
      "color|yellow",
      "dimension_depth|30 in +",
      "dimension_height|counter height",
      "materials_parent|wood",
      "price_buckets|$500 - $1000",
      "style_parent|rustic",
      "dimension_width|55 in +"
    ]
  }

我正在寻找处理此问题的正确方法。提前谢谢。

1 个答案:

答案 0 :(得分:3)

您是否可以更改文档的结构,以专门为您的查询添加过滤器属性,例如:

     {
        "StyleID": "chf_12345-bmc",
        "Attributes": [
          "brand|chf",
          "color|red",
          "color|yellow",
          "dimension_depth|30 in +",
          "dimension_height|counter height",
          "materials_parent|wood",
          "price_buckets|$500 - $1000",
          "style_parent|rustic",
          "dimension_width|55 in +"
        ],
        "filter_color": "red,yellow",
        "filter_fabric_type":"Polyester,leather"
      }

这将消除连接限制,因为现在您的查询看起来像这样:

SELECT s.StyleID FROM StyleSearch s
WHERE (CONTAINS(s.filter_color, "Grey") OR CONTAINS(s.filter_color, "Red"))
AND (CONTAINS(s.filter_fabric_type, "Polyester") OR CONTAINS(s.filter_fabric_type, "Leather"))

当然这意味着您还需要维护其他字段。

您也可以考虑为此编写存储过程,并使用javascript循环访问您的集合并以此方式过滤:DocumentDB stored procedure tutorial

相关问题