2016-11-14 81 views
0

我有一個ElasticSearch索引,用於存儲電話事務(SMS,MMS,Calls等)及其相關成本。通過字符串數組進行Elasticsearch聚合

這些文檔的關鍵是MSISDN(MSISDN =電話號碼)。在我的應用程序中,我知道有一組用戶。每個用戶可以有一個或多個MSISDN。

下面是這種文件的映射:

"mappings" : { 
     "cdr" : { 
     "properties" : { 
      "callDatetime" : { 
      "type" : "long" 
      }, 
      "callSource" : { 
      "type" : "string" 
      }, 
      "callType" : { 
      "type" : "string" 
      }, 
      "callZone" : { 
      "type" : "string" 
      }, 
      "calledNumber" : { 
      "type" : "string" 
      }, 
      "companyKey" : { 
      "type" : "string" 
      }, 
      "consumption" : { 
      "properties" : { 
       "data" : { 
       "type" : "long" 
       }, 
       "voice" : { 
       "type" : "long" 
       } 
      } 
      }, 
      "cost" : { 
      "type" : "double" 
      }, 
      "country" : { 
      "type" : "string" 
      }, 
      "included" : { 
      "type" : "boolean" 
      }, 
      "msisdn" : { 
      "type" : "string" 
      }, 
      "network" : { 
      "type" : "string" 
      } 
     } 
     } 
    } 

我的目標和問題:

我的目標是使檢索通過成本通過CALLTYPE查詢組。但是,組只在我的PostgreSQL數據庫中不在ElasticSearch中表示。

所以我會檢索所有的MSISDN爲每個現有組的方法,並得到類似的字符串數組的列表,其中包含每個組中的每個MSISDN。

比方說,我有這樣的事情:

"msisdn_by_group" : [ 
    { 
     "group1" : ["01111111111", "02222222222", "033333333333", "044444444444"] 
    }, 
    { 
     "group2" : ["05555555555","06666666666"] 
    } 
] 

現在,我將用它來生成Elasticsearch查詢。我想用匯總(總成本)來計算不同桶中所有這些術語的總和,然後再通過callType分割它。 (製作一個疊式條形圖)。

我已經試過幾件事情,但沒有管理,使其工作(直方圖,水桶,期限和金額,主要是我與打關鍵字)。

如果有人在這裏可以幫助我的訂單,以及關鍵字,我可以用它來實現這一點,那將是巨大的:)謝謝

編輯: 這是我最後一次嘗試: QUERY:

{ 
    "aggs" : { 
     "cost_histogram": { 
      "terms": { 
       "field": "callType" 
      }, 
      "aggs": { 
       "cost_histogram_sum" : { 
        "sum": { 
         "field": "cost" 
        } 
       } 
      } 
     } 
    } 
} 

我去了預期的結果,但它缺少「組」分裂,因爲我不知道如何通過MSISDN數組作爲一個標準:

結果:

"aggregations": { 
    "cost_histogram": { 
     "doc_count_error_upper_bound": 0, 
     "sum_other_doc_count": 0, 
     "buckets": [ 
     { 
      "key": "data", 
      "doc_count": 5925, 
      "cost_histogram_sum": { 
      "value": 0 
      } 
     }, 
     { 
      "key": "sms_mms", 
      "doc_count": 5804, 
      "cost_histogram_sum": { 
      "value": 91.76999999999995 
      } 
     }, 
     { 
      "key": "voice", 
      "doc_count": 5299, 
      "cost_histogram_sum": { 
      "value": 194.1196 
      } 
     }, 
     { 
      "key": "sms_mms_plus", 
      "doc_count": 35, 
      "cost_histogram_sum": { 
      "value": 7.2976 
      } 
     } 
     ] 
    } 
    } 
+1

也許顯示您現在的查詢並解釋仍然缺少的內容? – Val

+0

@Val當然我忘了,我的壞!請參閱我的編輯請 – Alex

+1

您需要另外一個「術語」聚合來包裝您當前的一個。 – Val

回答

0

好,我發現瞭如何使用一個查詢使這個,而是因爲它重複每一個組是該死的長查詢,但我沒有最佳的選擇。我正在使用「過濾器」聚合器。

這是基於我寫在我的問題上面的陣列上的工作示例:

POST本地主機:9200/CDR/_search大小= 0

{ 
    "query": { 
     "term" : { 
      "companyKey" : 1 
     } 
    }, 
    "aggs" : { 
     "group_1_split_cost": { 
      "filter": { 
       "bool": { 
        "should": [{ 
         "bool": { 
          "must": { 
           "match": { 
            "msisdn": "01111111111" 
           } 
          } 
         } 
        },{ 
         "bool": { 
          "must": { 
           "match": { 
            "msisdn": "02222222222" 
           } 
          } 
         } 
        },{ 
         "bool": { 
          "must": { 
           "match": { 
            "msisdn": "03333333333" 
           } 
          } 
         } 
        },{ 
         "bool": { 
          "must": { 
           "match": { 
            "msisdn": "04444444444" 
           } 
          } 
         } 
        }] 
       } 
      }, 
      "aggs": { 
       "cost_histogram": { 
        "terms": { 
         "field": "callType" 
        }, 
        "aggs": { 
         "cost_histogram_sum" : { 
          "sum": { 
           "field": "cost" 
          } 
         } 
        } 
       } 
      } 
     }, 
     "group_2_split_cost": { 
      "filter": { 
       "bool": { 
        "should": [{ 
         "bool": { 
          "must": { 
           "match": { 
            "msisdn": "05555555555" 
           } 
          } 
         } 
        },{ 
         "bool": { 
          "must": { 
           "match": { 
            "msisdn": "06666666666" 
           } 
          } 
         } 
        }] 
       } 
      }, 
      "aggs": { 
       "cost_histogram": { 
        "terms": { 
         "field": "callType" 
        }, 
        "aggs": { 
         "cost_histogram_sum" : { 
          "sum": { 
           "field": "cost" 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 

多虧了新版本的Elasticsearch,我們現在可以嵌套非常深的聚合,但是它仍然有點太糟糕,我們無法將值數組傳遞給「OR」運算符或類似的東西。我想,這可能會減少這些查詢的大小。即使它們有點特殊,並用於利基案例,就像我的一樣。

相關問題