2017-02-08 87 views
0

要求: 我想要查找特定类别ID的aID计数。 (即对于categoryID 2532,我希望计数为2,这意味着它被分配给两个aID)。使用Elasticsearch获取文档中特定字段的计数

我尝试了聚合,但我只能得到doc数而不是字段数。

映射

"List": { 
      "properties": { 

       "aId": { 
       "type": "long" 
       }, 
       "CategoryList": { 
       "properties": {     
        "categoryId": { 
        "type": "long" 
        }, 
        "categoryName": { 
        "type": "string" 
        } 
       } 
       }    
      } 
      } 

样本文档:

"List": [ 
      { 
       "aId": 33074,   
       "CategoryList": [ 
       { 
        "categoryId": 2532, 
        "categoryName": "VODAFONE"     
       } 
       ] 
      }, 
     { 
       "aId": 12074,   
       "CategoryList": [ 
       { 
        "categoryId": 2532, 
        "categoryName": "VODAFONE"     
       } 
       ] 
      }, 

     { 
       "aId": 120755,   
       "CategoryList": [ 
       { 
        "categoryId": 1234, 
        "categoryName": "SMPLKE"     
       } 
       ] 
      } 
      ] 
+0

您是否尝试过['cardinality'聚集(https://www.elastic.co/导向/ EN/elasticsearch /参考/电流/搜索聚合度量基数-aggregation.html)? – Val

+0

我会尽量让你知道.. – Seeker

+0

你不能使用基数聚合得到你想要的结果,请检查我的答案 – user3775217

回答

0

使用基数聚集不会帮助你得到想要的结果。基数聚合返回该字段的不同值的计数,您希望在哪里查找字段次数的外观计数。

您可以使用下面的查询,在这里,你可以先过滤文档CategoryList.categoryId然后在此领域运行一个简单的术语聚集以上的查询

POST index_name1111/_search 
{ 
    "query": { 
     "bool": { 
      "must": [{ 
       "term": { 
        "CategoryList.categoryId": { 
         "value": 2532 
        } 
       } 
      }] 
     } 
    }, 
    "aggs": { 
     "count_is": { 
      "terms": { 
       "field": "CategoryList.categoryId", 
       "size": 10 
      } 
     } 
    } 
} 

响应 -

{ 
    "took": 3, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 2, 
    "max_score": 0, 
    "hits": [] 
    }, 
    "aggregations": { 
    "count_is": { 
     "doc_count_error_upper_bound": 0, 
     "sum_other_doc_count": 0, 
     "buckets": [ 
     { 
      "key": 2532, 
      "doc_count": 2 
     } 
     ] 
    } 
    } 
} 

或者你也可以查看过滤器并运行聚合只会返回所有categoryId与他们的外观计数。上述查询的

POST index_name1111/_search 
{ 
size: 0, 
    "aggs": { 
    "count_is": { 
     "terms": { 
     "field": "CategoryList.categoryId", 
     "size": 10 
     } 
    } 
    } 
} 

响应

{ 
     "took": 2, 
     "timed_out": false, 
     "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
     }, 
     "hits": { 
     "total": 3, 
     "max_score": 0, 
     "hits": [] 
     }, 
     "aggregations": { 
     "count_is": { 
      "doc_count_error_upper_bound": 0, 
      "sum_other_doc_count": 0, 
      "buckets": [ 
      { 
       "key": 2532, 
       "doc_count": 2 
      }, 
      { 


     "key": 1234, 
      "doc_count": 1 
     } 
     ] 
    } 
    } 
} 

使用基数聚集你会得到与上述查询的以下查询

POST index_name1111/_search 
{ 
    "size": 0, 
    "query": { 
     "bool": { 
      "must": [{ 
       "term": { 
        "CategoryList.categoryId": { 
         "value": 2532 
        } 
       } 
      }] 
     } 
    }, 
    "aggs": { 
     "id_count": { 
      "cardinality": { 
       "field": "CategoryList.categoryId" 
      } 
     } 
    } 
} 

响应这不下面的响应给你想要的结果,因为两个文件matc建置都与作为的categoryId 252这样算的不同是1

{ 
    "took": 4, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 2, 
    "max_score": 0, 
    "hits": [] 
    }, 
    "aggregations": { 
    "id_count": { 
     "value": 1 
    } 
    } 
} 

希望这有助于 感谢

相关问题