Aggregate query doesn't return expected results

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

Hi @ignaqui,

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

Christian

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.

Thanks for prompt response.

I took examples from this post:

$ 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

Elastic version 2.3.4
Changing type to not_analyzed didn't help

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
    }
  }
}