2015-03-03 35 views
0

我試圖應用統計過濾後的數據,但它不工作...satistical過濾後的數據在elasticsearch

我的方案是:

我想申請的統計上的一些過濾的數據。所以我申請了以下查詢:

{ 
    "from": 0, 
    "size": 10000, 
    "facets": { 
    "stats": { 
     "statistical": { 
     "field": "xxxx" 
     },"facet_filter": { 
     "and": [ 
      { 
       "term": { 
       "id_1": "33" 
       } 
      } 
      ], 
      "or": [ 
      { 
       "term": { 
       "type": "aaaa" 
       } 
      }, 
      { 
       "term": { 
       "type": "bbbb" 
       } 
      } 
      ] 
     } 
    } 
    } 
} 

它的工作很好。但是,當我在多個領域應用其不working.The以下是查詢:

{ 
    "from": 0, 
    "size": 10000, 
    "facets": { 
    "stats": { 
     "statistical": { 
     "field": "xxxx" 
     },"facet_filter": { 
      "and": [ 
      { 
       "term": { 
       "id_1": "33" 
       } 
      } 
      ], 
      "or": [ 
      { 
       "term": { 
       "type": "aaaa" 
       } 
      }, 
      { 
       "term": { 
       "type": "bbbb" 
       } 
      } 
      ] 
     } 
    }, 
    "stats1": { 
     "statistical": { 
     "field": "yyyy" 
     },"facet_filter": { 
      "and": [ 
      { 
       "term": { 
       "id_1": "33" 
       } 
      } 
      ], 
      "or": [ 
      { 
       "term": { 
       "type": "aaaa" 
       } 
      }, 
      { 
       "term": { 
       "type": "bbbb" 
       } 
      } 
      ] 
     } 
    } 
    } 
} 

它拋出以下錯誤:

Parse Failure [No parser for element [stats1]]]; }]", 
    "status": 400 

請分享您的想法。提前致謝。

+0

如果你的過濾器是facetA和facetB相同,你可以拉起來的層面之上以便您擁有適用於所有構面的過濾器。 – jhilden 2015-03-03 16:07:49

+0

我會使用[聚合API](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations.html),我認爲他們正試圖逐步取消方面。 – slim 2015-03-03 19:26:37

+0

@ jhilden感謝您的時間。我的FIlter是相同的,所以你可以請更多的解釋..統計不適用於過濾器。 – Subburaj 2015-03-04 05:28:33

回答

0

您設置了構面過濾器的方式使用的語法不正確,也不符合邏輯意義,因爲您在同一級別上有"and""or"。這是你錯誤的原因。我假設你的意思是這樣的,與"or""and"

 "facet_filter": { 
     "and": [ 
      { 
       "term": { 
       "id_1": "33" 
       } 
      }, 
      { 
       "or": [ 
       { 
        "term": { 
         "type": "aaaa" 
        } 
       }, 
       { 
        "term": { 
         "type": "bbbb" 
        } 
       } 
       ] 
      } 
     ] 
    } 

此外,由於你的過濾器是在兩種情況下是相同的,你不妨在過濾查詢中使用它,你只必須指定一次。利用這一點,我得到了以下的玩具爲例來工作,我假設你想:

DELETE /test_index 

PUT /test_index 
{ 
    "settings": { 
     "number_of_shards": 1 
    } 
} 

POST /test_index/_bulk 
{"index":{"_index":"test_index","_type":"doc","_id":1}} 
{"id_1":"32", "xxxx":2, "yyyy":5,"type":"aaaa"} 
{"index":{"_index":"test_index","_type":"doc","_id":2}} 
{"id_1":"32", "xxxx":3, "yyyy":10,"type":"bbbb"} 
{"index":{"_index":"test_index","_type":"doc","_id":3}} 
{"id_1":"33", "xxxx":4, "yyyy":15,"type":"aaaa"} 
{"index":{"_index":"test_index","_type":"doc","_id":4}} 
{"id_1":"33", "xxxx":5, "yyyy":20,"type":"bbbb"} 


POST /test_index/_search 
{ 
    "size": 0, 
    "query": { 
     "filtered": { 
     "query": { 
      "match_all": {} 
     }, 
     "filter": { 
      "and": [ 
       { 
        "term": { 
        "id_1": "33" 
        } 
       }, 
       { 
        "or": [ 
        { 
         "term": { 
          "type": "aaaa" 
         } 
        }, 
        { 
         "term": { 
          "type": "bbbb" 
         } 
        } 
        ] 
       } 
      ] 
     } 
     } 
    }, 
    "facets": { 
     "stats": { 
     "statistical": { 
      "field": "xxxx" 
     } 
     }, 
     "stats1": { 
     "statistical": { 
      "field": "yyyy" 
     } 
     } 
    } 
} 
... 
{ 
    "took": 1, 
    "timed_out": false, 
    "_shards": { 
     "total": 1, 
     "successful": 1, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 2, 
     "max_score": 0, 
     "hits": [] 
    }, 
    "facets": { 
     "stats": { 
     "_type": "statistical", 
     "count": 2, 
     "total": 9, 
     "min": 4, 
     "max": 5, 
     "mean": 4.5, 
     "sum_of_squares": 41, 
     "variance": 0.25, 
     "std_deviation": 0.5 
     }, 
     "stats1": { 
     "_type": "statistical", 
     "count": 2, 
     "total": 35, 
     "min": 15, 
     "max": 20, 
     "mean": 17.5, 
     "sum_of_squares": 625, 
     "variance": 6.25, 
     "std_deviation": 2.5 
     } 
    } 
} 

說了這麼多,你可能會更好使用聚合,特別是因爲面是現在已經過時。用stats aggregation同樣的例子是這樣的:

POST /test_index/_search 
{ 
    "size": 0, 
    "query": { 
     "filtered": { 
     "query": { 
      "match_all": {} 
     }, 
     "filter": { 
      "and": [ 
       { 
        "term": { 
        "id_1": "33" 
        } 
       }, 
       { 
        "or": [ 
        { 
         "term": { 
          "type": "aaaa" 
         } 
        }, 
        { 
         "term": { 
          "type": "bbbb" 
         } 
        } 
        ] 
       } 
      ] 
     } 
     } 
    }, 
    "aggs": { 
     "stats": { 
     "stats": { 
      "field": "xxxx" 
     } 
     }, 
     "stats1": { 
     "stats": { 
      "field": "yyyy" 
     } 
     } 
    } 
} 
... 
{ 
    "took": 2, 
    "timed_out": false, 
    "_shards": { 
     "total": 1, 
     "successful": 1, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 2, 
     "max_score": 0, 
     "hits": [] 
    }, 
    "aggregations": { 
     "stats": { 
     "count": 2, 
     "min": 4, 
     "max": 5, 
     "avg": 4.5, 
     "sum": 9 
     }, 
     "stats1": { 
     "count": 2, 
     "min": 15, 
     "max": 20, 
     "avg": 17.5, 
     "sum": 35 
     } 
    } 
} 

或者你可以使用一個extended stats aggregation如果你想:

POST /test_index/_search 
{ 
    "size": 0, 
    "query": { 
     "filtered": { 
     "query": { 
      "match_all": {} 
     }, 
     "filter": { 
      "and": [ 
       { 
        "term": { 
        "id_1": "33" 
        } 
       }, 
       { 
        "or": [ 
        { 
         "term": { 
          "type": "aaaa" 
         } 
        }, 
        { 
         "term": { 
          "type": "bbbb" 
         } 
        } 
        ] 
       } 
      ] 
     } 
     } 
    }, 
    "aggs": { 
     "stats": { 
     "extended_stats": { 
      "field": "xxxx" 
     } 
     }, 
     "stats1": { 
     "extended_stats": { 
      "field": "yyyy" 
     } 
     } 
    } 
} 
... 
{ 
    "took": 1, 
    "timed_out": false, 
    "_shards": { 
     "total": 1, 
     "successful": 1, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 2, 
     "max_score": 0, 
     "hits": [] 
    }, 
    "aggregations": { 
     "stats": { 
     "count": 2, 
     "min": 4, 
     "max": 5, 
     "avg": 4.5, 
     "sum": 9, 
     "sum_of_squares": 41, 
     "variance": 0.25, 
     "std_deviation": 0.5 
     }, 
     "stats1": { 
     "count": 2, 
     "min": 15, 
     "max": 20, 
     "avg": 17.5, 
     "sum": 35, 
     "sum_of_squares": 625, 
     "variance": 6.25, 
     "std_deviation": 2.5 
     } 
    } 
} 
+0

非常感謝man.its正常工作.. – Subburaj 2015-03-05 06:18:31