2017-03-05 1 views
0

나는 10m 간격을 그룹화하고 하나의 집계에서 거래 수를 계산하고 다른 집계에서 이동 평균을 계산하는 거래 데이터 스트림이 있습니다. total_count가> moving average 인 경우에만 결과를 쿼리하고 싶습니다.탄성 검색에서 두 개의 집계를 비교하는 방법

이 쿼리는 정상적으로 처리됩니다.

GET/_search

{ 
    "aggs": { 
     "my_date_histo":{     
      "date_histogram":{ 
       "field":"created_at", 
       "interval":"10m" 
      }, 

      "aggs":{ 
       "the_count":{ 
        "value_count" : {"field" : "user_id"} 
       }, 

       "the_movavg":{ 
        "moving_avg":{ 
        "buckets_path": "the_count" , 
        "window": 5, 
        "model": "simple" 
        } 
       } 
     } 
    } 
    } 
} 

하지만 다음이 발생합니다 오류를하려고 할 때,

GET /_search 
{ 
    "aggs": { 
     "my_date_histo":{     
      "date_histogram":{ 
       "field":"created_at", 
       "interval":"10m" 
      }, 

      "aggs":{ 
       "the_count":{ 
        "value_count" : {"field" : "user_id"} 
       }, 

       "the_movavg":{ 
        "moving_avg":{ 
        "buckets_path": "the_count" , 
        "window": 5, 
        "model": "simple" 
        } 
       }, 

       "final_filter": { 
      "bucket_selector": { 
      "buckets_path": { 
      "TheCount": "the_count", 
      "TheMovAvg": "the_movavg" 

      }, 
      "script": "params.TheCount > params.TheMovAvg" 
     } 
    } 

     } 
    } 
    } 

} 

편집 :

매핑

{ 
    "transaction-live": { 
    "mappings": { 
     "logs": { 
     "properties": { 
      "@timestamp": { 
      "type": "date" 
      }, 
      "@version": { 
      "type": "text", 
      "fields": { 
       "keyword": { 
       "type": "keyword", 
       "ignore_above": 256 
       } 
      } 
      }, 
      "correspondent_id": { 
      "type": "text", 
      "fields": { 
       "keyword": { 
       "type": "keyword", 
       "ignore_above": 256 
       } 
      } 
      }, 
      "created_at": { 
      "type": "date" 
      }, 
      "discount": { 
      "type": "float" 
      }, 
      "endpoint": { 
      "type": "text", 
      "fields": { 
       "keyword": { 
       "type": "keyword", 
       "ignore_above": 256 
       } 
      } 
      }, 
      "event_type": { 
      "type": "text", 
      "fields": { 
       "keyword": { 
       "type": "keyword", 
       "ignore_above": 256 
       } 
      } 
      }, 
      "fees": { 
      "type": "float" 
      }, 
      "from_country_code": { 
      "type": "text", 
      "fields": { 
       "keyword": { 
       "type": "keyword", 
       "ignore_above": 256 
       } 
      } 
      }, 
      "from_currency_code": { 
      "type": "text", 
      "fields": { 
       "keyword": { 
       "type": "keyword", 
       "ignore_above": 256 
       } 
      } 
      }, 
      "fx_sent_receive": { 
      "type": "float" 
      }, 
      "receive_amount": { 
      "type": "float" 
      }, 
      "response_code": { 
      "type": "long" 
      }, 
      "send_amount": { 
      "type": "float" 
      }, 
      "source": { 
      "type": "text", 
      "fields": { 
       "keyword": { 
       "type": "keyword", 
       "ignore_above": 256 
       } 
      } 
      }, 
      "source_version": { 
      "type": "text", 
      "fields": { 
       "keyword": { 
       "type": "keyword", 
       "ignore_above": 256 
       } 
      } 
      }, 
      "startedtransaction_id": { 
      "type": "long" 
      }, 
      "to_country_code": { 
      "type": "text", 
      "fields": { 
       "keyword": { 
       "type": "keyword", 
       "ignore_above": 256 
       } 
      } 
      }, 
      "user_agent": { 
      "type": "text", 
      "fields": { 
       "keyword": { 
       "type": "keyword", 
       "ignore_above": 256 
       } 
      } 
      }, 
      "user_id": { 
      "type": "long" 
      } 
     } 
     } 
    } 
    } 
} 

ERROR :

{ 
    "error": { 
    "root_cause": [], 
    "type": "reduce_search_phase_exception", 
    "reason": "[reduce] ", 
    "phase": "fetch", 
    "grouped": true, 
    "failed_shards": [], 
    "caused_by": { 
     "type": "script_exception", 
     "reason": "runtime error", 
     "caused_by": { 
     "type": "null_pointer_exception", 
     "reason": null 
     }, 
     "script_stack": [ 
     "params.TheCount > params.TheMovAvg", 
     "      ^---- HERE" 
     ], 
     "script": "params.TheCount > params.TheMovAvg", 
     "lang": "painless" 
    } 
    }, 
    "status": 503 
} 
+0

매핑/스키마를 공유 할 수 있습니까? – user3775217

+0

또한 받고있는 오류를 붙여주십시오. – user3775217

+0

@ user3775217 : 요청한 세부 정보로 내 대답을 편집했습니다. – user2635060

답변

2

검색어를 사용하여 약간 놀아서 문제를 발견했습니다. 다음 당신이 문제를 이해하려면 지금

{ 
    "size": 0, 
    "aggs": { 
     "my_date_histo": { 
      "date_histogram": { 
       "field": "created_at", 
       "interval": "10m" 
      }, 
      "aggs": { 
       "the_count": { 
        "value_count": { 
         "field": "user_id" 
        } 
       }, 
       "the_movavg": { 
        "moving_avg": { 
         "buckets_path": "the_count", 
         "window": 5, 
         "model": "simple" 
        } 
       }, 
       "final_filter": { 
        "bucket_selector": { 
         "buckets_path": { 
          "TheCount": "the_count", 
          "TheMovAvg": "the_movavg" 

         }, 
         "script": "params.TheCount > (params.TheMovAvg == null ? 0 : params.TheMovAvg)" 
        } 
       } 
      } 
     } 
    } 
} 

를 사용하는 bucket_selector 집계하지 않고 통합의 다음과 같은 결과에서 살펴볼 수있는 작업 쿼리입니다.

{ 
    "took": 10, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 42, 
    "max_score": 0, 
    "hits": [] 
    }, 
    "aggregations": { 
    "my_date_histo": { 
     "buckets": [ 
     { 
      "key_as_string": "2017-03-06T15:30:00.000Z", 
      "key": 1488814200000, 
      "doc_count": 14, 
      "the_count": { 
      "value": 14 
      } 
     }, 
     { 
      "key_as_string": "2017-03-06T15:40:00.000Z", 
      "key": 1488814800000, 
      "doc_count": 0, 
      "the_count": { 
      "value": 0 
      } 
     }, 
     { 
      "key_as_string": "2017-03-06T15:50:00.000Z", 
      "key": 1488815400000, 
      "doc_count": 14, 
      "the_count": { 
      "value": 14 
      }, 
      "the_movavg": { 
      "value": 7 
      } 
     }, 
     { 
      "key_as_string": "2017-03-06T16:00:00.000Z", 
      "key": 1488816000000, 
      "doc_count": 3, 
      "the_count": { 
      "value": 3 
      }, 
      "the_movavg": { 
      "value": 14 
      } 
     }, 
     { 
      "key_as_string": "2017-03-06T16:10:00.000Z", 
      "key": 1488816600000, 
      "doc_count": 8, 
      "the_count": { 
      "value": 7 
      }, 
      "the_movavg": { 
      "value": 8.5 
      } 
     }, 
     { 
      "key_as_string": "2017-03-06T16:20:00.000Z", 
      "key": 1488817200000, 
      "doc_count": 3, 
      "the_count": { 
      "value": 3 
      }, 
      "the_movavg": { 
      "value": 6.375 
      } 
     } 
     ] 
    } 
    } 
} 

당신이 moving_agg 설정을/그 창에 대한 moving_aggs을 계산하지 않는 첫 번째 두 버킷 위의 결과를 관찰합니다. 따라서 필터 선택기가 비교할 때 JAVA 비교 연산자가 null 포인터 예외를 throw하기 때문에 null pointer exception on runtime을 던지고있었습니다.

희망이 도움이됩니다. 감사합니다.

+0

솔루션에 대한 많은 감사를드립니다. 정말 도움이되었고 문제가 해결되었습니다. – user2635060

관련 문제