# Aggrégation avec filtre

**URL:** <https://discuss.elastic.co/t/aggregation-avec-filtre/177414>\
**Category:** Discussions en français\
**Created:** [April 18, 2019, 8:52am UTC](https://discuss.elastic.co/t/aggregation-avec-filtre/177414 "2019-04-18T08:52:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RSTMAN](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@RSTMAN](https://discuss.elastic.co/u/RSTMAN)\
**Post date:** [April 18, 2019, 8:52am UTC](https://discuss.elastic.co/t/aggregation-avec-filtre/177414/1 "2019-04-18T08:52:39Z")

</div>

Bonjour,

j'ai essayé de faire une requête ES avec une aggregation, mais je n'ai pas le résultat que je veux.  
c'est certainement ma requête n'est pas bonne.

voilà mon index:

```auto
DELETE test

PUT test
{
  "mappings": {
    "test": {
      "properties": {
        "id": {
          "type": "long"
        },
        "tags": {
          "properties": {
            "cnt": {
              "type": "long"
            },
            "id": {
              "type": "text",
              "fielddata": true,
              "fields": {
                "keyword": {
                  "type": "keyword"
                }
              }
            },
            "label": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword"
                }
              }
            }
          }
        },
        "title": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}

POST _bulk
{ "index" : { "_index" : "test", "_type" : "test", "_id" : "1" } }
{ "id": 1, "title" : "cyber securité","tags": [{"id": "1","cnt": 8,"label": "categorie 1"},{"id": "2","cnt": 6,"label": "categorie 2"},{"id": "3","cnt": 4,"label": "categorie 3"}]}
{ "index" : { "_index" : "test", "_type" : "test", "_id" : "2" } }
{ "id": 2, "title" : "cybersécurité","tags": [{"id": "1","cnt": 8,"label": "categorie 1"},{"id": "2","cnt": 6,"label": "categorie 2"},{"id": "3","cnt": 4,"label": "categorie 3"}] }
{ "index" : { "_index" : "test", "_type" : "test", "_id" : "3" } }
{ "id": 3, "title" : "technicien bigdata","tags": [{"id": "1","cnt": 8,"label": "categorie 1"},{"id": "2","cnt": 6,"label": "categorie 2"},{"id": "3","cnt": 4,"label": "categorie 3"}] }
{ "index" : { "_index" : "test", "_type" : "test", "_id" : "4" } }
{ "id": 4, "title" : "fullstack","tags": [{"id": "1","cnt": 8,"label": "categorie 1"},{"id": "2","cnt": 6,"label": "categorie 2"},{"id": "3","cnt": 4,"label": "categorie 3"}] }
{ "index" : { "_index" : "test", "_type" : "test", "_id" : "5" } }
{ "id": 5, "title" : "aafullstack", "tags":[] }
{ "index" : { "_index" : "test", "_type" : "test", "_id" : "6" } }
{ "id": 6, "title" : "6fullstack", "tags":[] }
{ "index" : { "_index" : "test", "_type" : "test", "_id" : "7" } }
{ "id": 7, "title" : "7fullstack", "tags":[] }

```

ma requête:

```auto
GET test/_search
{
  "size": 0,
  "aggs": {
    "test": {
      "filter": {
        "range": {
          "tags.cnt":{
            "from": 4
          }
        }
      },
      "aggs": {
        "names": {
          "terms": {
            "field": "tags.id",
            "size": 10
          }
        }
      }
    }
  }
}

```

et le résultat:

```auto
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 7,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "test": {
      "doc_count": 4,
      "names": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": "1",
            "doc_count": 4
          },
          {
            "key": "2",
            "doc_count": 4
          },
          {
            "key": "3",
            "doc_count": 4
          }
        ]
      }
    }
  }
}

```

mais ce que je veux avoir en sortie:

```auto
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 7,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "test": {
      "doc_count": 4,
      "names": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": "3",
            "doc_count": 4
          }
        ]
      }
    }
  }
}

```

si quelqu'un peut m'aider !  
merci

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 23, 2019, 9:46am UTC](https://discuss.elastic.co/t/aggregation-avec-filtre/177414/2 "2019-04-23T09:46:30Z")

</div>

Je ne suis pas certain d'avoir compris la question malgré l'exemple.  
Je me demande juste si ton "problème" vient du fait que tu utilises des objets dans un tableau. Les objets sont indexés à plat par défaut dans Elasticsearch, à moins que tu n'utilises spécifiquement le type `nested` dans le mapping pour le champ `tags`.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 21, 2019, 9:57am UTC](https://discuss.elastic.co/t/aggregation-avec-filtre/177414/3 "2019-05-21T09:57:11Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
