2016-10-13 3 views
0

DocumentDB query playground을 사용하여 필터 유형의 쿼리를 작성하고 있습니다. 사용자가 특정 속성으로 검색 할 수 있도록 설정되어있는 데이터에 일련의 속성이 있습니다. 이름/값 컬렉션의 이름에서 여러 항목을 선택한 경우 각 속성 유형과 OR 문이됩니다. 색상 및 크기가 다른 속성이 선택되면 AND 문이됩니다.배열의 DocumentDB 다중 필터 쿼리

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 내가 각각 "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 +" 
    ] 
    } 

내가 적절한 방법을 찾고 있어요 손에 르. 미리 감사드립니다.

답변

3

문서의 구조를 변경하여 예를 들어 검색어에 대한 필터 속성을 구체적으로 추가 할 수 있습니까?

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")) 

물론 이것은 당신이 유지하는 추가 필드를 가지고 있음을 의미 하는가 : 지금 당신의 쿼리가 같은 것을 보이기 때문에

 { 
     "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" 
     } 

이 조인 제한을 제거하는 것입니다.

또한 그 방법이에 대한 저장된 프로 시저를 작성하고 귀하의 컬렉션을 루프 자바 스크립트를 사용하여 필터링을 고려해 볼 수 있습니다 : DocumentDB stored procedure tutorial

+0

되는 NoSQL에 사용을 얻는 때 여분의 열을 사용 해요입니다. =) DateTime 필드는 너무 재미 있습니다. 나는이 접근법이 ... 이것에 잡종을 봐야 할지도 모르다는 것을 좋아한다. 문제는 붉은 색과 붉은 색을 가치로 생각할 때 발생합니다. 이 쿼리는 둘 모두에서 빨간색과 일치 할 수 있습니다. "CONTAINS (s.filter_color,"filter_color | red ")'또는 좀 더 간단한 한정자로''filter_color": "filter_color | red, filter_color | 나는 이것을 밖으로 시도하고 다시 당신에게 돌아갈거야. – Matty

+0

모든 필터와 값을 포함하는 필드를 추가했습니다. 필드 데이터는 다음과 같습니다.''| filter_color | red |, | filter_color | 가을 빨강 | ... ''필드에 파이프를 두어 성냥과 관련된 문제없이 포함을 쉽게합니다. 또한, 나는 단지 이것을 위해 사용되는 하나의 추가적인 필드를 가지고 있습니다. 매우 빠르다. – Matty