2016-12-21 1 views
1

elasticsearch를 사용하여 무언가를하려하고 아무 대답도 찾지 못했습니다.Elasticsearch : 필터링 된 중첩 필드의 스크립트 된 합계에 대한 필터

"products": { 
    "include_in_root": true, 
    "type": "nested", 
    "properties": { 
     "date": { 
     "format": "strict_date_optional_time||epoch_millis", 
     "type": "date" 
     }, 
     "type": { 
     "index": "not_analyzed", 
     "type": "string" 
     }, 
     "cat4": { 
     "index": "not_analyzed", 
     "type": "string" 
     }, 
     "geo": { 
     "type": "geo_point" 
     }, 
     "baseprice": { 
     "type": "long" 
     }, 
     "cat2": { 
     "index": "not_analyzed", 
     "type": "string" 
     }, 
     "cat3": { 
     "index": "not_analyzed", 
     "type": "string" 
     }, 
     "feeltemp": { 
     "type": "long" 
     }, 
     "cat1": { 
     "index": "not_analyzed", 
     "type": "string" 
     }, 
     "price": { 
     "type": "double" 
     }, 
     "qty": { 
     "index": "not_analyzed", 
     "type": "string" 
     }, 
     "name": { 
     "index": "not_analyzed", 
     "type": "string" 
     }, 
     "weather": { 
     "index": "not_analyzed", 
     "type": "string" 
     }, 
     "id": { 
     "index": "not_analyzed", 
     "type": "string" 
     }, 
     "stock": { 
     "type": "long" 
     }, 
     "brand": { 
     "index": "not_analyzed", 
     "type": "string" 
     } 
    } 
    } 

내가 그들을 조회하려는 경우에만 예를 들어 유형 = 'cartadd'와 CAT1 = "테스트":

는 중첩 된 오브젝트의 제품을 얻었다.

문제, 중첩 된 필터 쿼리가있는 경우 :

{ 
    "query": { 
    "bool": { 
     "filter": [ 
     { 
      "nested": { 
      "path": "products", 
      "filter": { 
       "bool": { 
       "must": [ 
        { 
        "script": { 
         "script": "sum=0;for(obj in _source.products) {sum = sum + 1 }; sum>=1;" 
        } 
        }, 
        { 
        "term": { 
         "products.type": "view" 
        } 
        } 
       ] 
       } 
      } 
      } 
     } 
     ] 
    } 
    } 
} 

그것은 아무것도 계산하지 않습니다.

내가 중첩 된 작업을 제거하는 경우 :

{ 
    "query": { 
    "bool": { 
     "filter": { 
     "bool": { 
      "must": [ 
      { 
       "script": { 
       "script": "sum=0;for(obj in _source.products) {if(obj.type=='cartadd') {sum = sum + obj.price }}; sum>=2;" 
       } 
      } 
      ] 
     } 
     } 
    } 
    } 
} 

그것은 작동하고 객체를 계산합니다. 하지만 중첩 된 개체를 더 이상 필터링 할 수 없습니다.

Groovy 스크립트에서 if 조건을 추가했는데, 동적으로 더 많은 조건을 추가 할 수 있습니다.

누군가 내 생각에 어떻게 할 수 있습니까?

고맙습니다.

{ 
    "query": { 
    "bool": { 
     "filter": [ 
     { 
      "script": { 
      "script": "sum=0;for(obj in _source.products) {sum = sum + 1 }; sum>=1;" 
      } 
     }, 
     { 
      "nested": { 
      "path": "products", 
      "filter": { 
       "term": { 
       "products.type": "view" 
       } 
      } 
      } 
     } 
     ] 
    } 
    } 
} 

게다가, 좋은 생각 것 :

답변

1

당신은 products에서 작동하기 때문에 부모 문서의 필드 (안 중첩 된 것들)는 어느 nested 쿼리의 외부 script 필터를 이동해야 제품 오브젝트 수를 포함하는 nbProducts이라는 새 필드를 상위 문서에 추가하십시오. 그러면 해당 스크립트를 제거하고 다음과 같은 간단한 쿼리를 수행 할 수 있습니다.

{ 
    "query": { 
    "bool": { 
     "filter": [ 
     { 
      "range": { 
      "nbProducts": { 
       "gte": 1 
      } 
      } 
     }, 
     { 
      "nested": { 
      "path": "products", 
      "filter": { 
       "term": { 
       "products.type": "view" 
       } 
      } 
      } 
     } 
     ] 
    } 
    } 
} 
+0

답장을 보내 주셔서 감사합니다. 문제는 다음과 같습니다. > 첫 번째 해결 방법에서는 모든 제품을 계산하지만 type = view로 필터링하지 않음 > 두 번째 솔루션에서 nbProducts는 동적이지 않습니다. 다른 필터를 많이 추가해야합니다. – Kaliseo

+0

그렇다면'products.type = view' 조건과 일치하는 제품 만 셀 수 있다는 것입니다. 그것은 본질적으로 하나의 중첩 된 '제품'일치를 얻으면 적어도 하나가 일치하기 때문에 제품 수는 반드시> = 1입니다. 더 많은 기준을 가지고 있다면'nested' 쿼리의'bool/filter'에 추가 할 수 있습니다. – Val

관련 문제