# Aggregate query doesn't return expected results

**URL:** <https://discuss.elastic.co/t/aggregate-query-doesnt-return-expected-results/55543>\
**Category:** Elasticsearch\
**Created:** [July 14, 2016, 4:37pm UTC](https://discuss.elastic.co/t/aggregate-query-doesnt-return-expected-results/55543 "2016-07-14T16:37:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ignaqui](https://avatars.discourse-cdn.com/v4/letter/i/c89c15/32.png) [@ignaqui](https://discuss.elastic.co/u/ignaqui)\
**Post date:** [July 14, 2016, 4:37pm UTC](https://discuss.elastic.co/t/aggregate-query-doesnt-return-expected-results/55543/1 "2016-07-14T16:37:40Z")

</div>

Guys,

Please point me to right direction.

I'm trying to create very simple aggregation query, based on tiny array of data. For that I'm using kopf's rest interface. As far as I can understand, this query is not groovy (or other) script, which I have to enable explicitly.

So:

```
GET /transactions/sales/_search:
    {
        "query": {
            "match_all": {}
        }
    }

```

Response:

```
{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": 1,
    "hits": [
      {
        "_index": "transactions",
        "_type": "sales",
        "_id": "3",
        "_score": 1,
        "_source": {
          "type": "cost",
          "amount": 30
        }
      },
      {
        "_index": "transactions",
        "_type": "sales",
        "_id": "1",
        "_score": 1,
        "_source": {
          "type": "sale",
          "amount": 80
        }
      },
      {
        "_index": "transactions",
        "_type": "sales",
        "_id": "2",
        "_score": 1,
        "_source": {
          "type": "cost",
          "amount": 10
        }
      },
      {
        "_index": "transactions",
        "_type": "sales",
        "_id": "4",
        "_score": 1,
        "_source": {
          "type": "sale",
          "amount": 130
        }
      }
    ]
  }
}

```

It looks like we are good. However:

```
{
  "size": 0,
  "aggs": {
    "sales_types": {
      "terms": {
        "field": "type"
      }
    }
  }
}

```

The result is the same as above. However, I expected something like this (below response was taken from example):

```
{
   "took": 40,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 4,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "sales_types": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "cost",
               "doc_count": 2
            },
            {
               "key": "sale",
               "doc_count": 2
            }
         ]
      }
   }
}

```

Kindly please advise.

Thank you,  
ignaqui

---

<div class="post-metadata">

**Author:** ![cstrzadala](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cstrzadala/32/8211_2.png) [@cstrzadala](https://discuss.elastic.co/u/cstrzadala)\
**Post date:** [July 14, 2016, 9:39pm UTC](https://discuss.elastic.co/t/aggregate-query-doesnt-return-expected-results/55543/2 "2016-07-14T21:39:59Z")

</div>

Hi @ignaqui,

Moved to the #elasticsearch topic as related to Elasticsearch than Elastic Cloud.

Christian

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [July 15, 2016, 12:11pm UTC](https://discuss.elastic.co/t/aggregate-query-doesnt-return-expected-results/55543/3 "2016-07-15T12:11:38Z")

</div>

The query ought to just work, yeah.

You should declare in your mapping that `type` is `not_analyzed`. That maybe help.

It'd help if I knew the version number you are using and all the commands you used to get here. If you can write all of them either in sense or curl syntax that'd be helpful to me.

---

<div class="post-metadata">

**Author:** ![ignaqui](https://avatars.discourse-cdn.com/v4/letter/i/c89c15/32.png) [@ignaqui](https://discuss.elastic.co/u/ignaqui)\
**Post date:** [July 15, 2016, 9:33pm UTC](https://discuss.elastic.co/t/aggregate-query-doesnt-return-expected-results/55543/4 "2016-07-15T21:33:34Z")

</div>

Thanks for prompt response.

I took examples from this post:

> **[Elasticsearch Aggregations - madewithlove](https://blog.madewithlove.be/post/elasticsearch-aggregations/)**
>
> I have already covered how you can easily integrate Elasticsearch with your app, but I haven’t talked anything about how you can query your data.

```
$ curl -XPUT localhost:9200/transactions/ -d '{
    "mappings": {
        "sales": {
            "properties": {
                "type": { "type": "string" },
                "amount": { "type": "double" }
            }
        }
    }
}'

$ curl -XPUT localhost:9200/transactions/sales/1 -d '{
    "type": "sale",
    "amount": 80
}'

$ curl -XPUT localhost:9200/transactions/sales/2 -d '{
    "type": "cost",
    "amount": 10
}'

$ curl -XPUT localhost:9200/transactions/sales/3 -d '{
    "type": "cost",
    "amount": 30
}'

$ curl -XPUT localhost:9200/transactions/sales/4 -d '{
    "type": "sale",
    "amount": 130
}'

$ curl -XGET localhost:9200/transactions/sales/_search -d '{
    "size": 0,
    "aggs": {
        "sales_types": {
            "terms": {
                "field": "type"
            }
        }
    }
}'

$ curl -XGET localghost:9200/transactions/sales/_search -d '{
    "size": 0,
    "aggs": {
        "sales_stats": {
            "stats": {
                "field": "amount"
            }
        }
    }
}'

```

I will try trick with `not_analyzed` as well

---

<div class="post-metadata">

**Author:** ![ignaqui](https://avatars.discourse-cdn.com/v4/letter/i/c89c15/32.png) [@ignaqui](https://discuss.elastic.co/u/ignaqui)\
**Post date:** [July 18, 2016, 8:45pm UTC](https://discuss.elastic.co/t/aggregate-query-doesnt-return-expected-results/55543/5 "2016-07-18T20:45:26Z")

</div>

Elastic version 2.3.4  
Changing type to not\_analyzed didn't help

---

<div class="post-metadata">

**Author:** ![ignaqui](https://avatars.discourse-cdn.com/v4/letter/i/c89c15/32.png) [@ignaqui](https://discuss.elastic.co/u/ignaqui)\
**Post date:** [July 19, 2016, 9:49pm UTC](https://discuss.elastic.co/t/aggregate-query-doesnt-return-expected-results/55543/6 "2016-07-19T21:49:51Z")

</div>

All I had to do is to change GET to POST!

```
{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 0,
    "hits": [

    ]
  },
  "aggregations": {
    "sales_stats": {
      "count": 4,
      "min": 10,
      "max": 130,
      "avg": 62.5,
      "sum": 250
    }
  }
}
```

---

<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:** [July 5, 2017, 10:34pm UTC](https://discuss.elastic.co/t/aggregate-query-doesnt-return-expected-results/55543/7 "2017-07-05T22:34:17Z")

</div>


