Lucas Lopes
Lucas Lopes

Reputation: 35

How to use aggregations with Elastic Search

I'm using Elastic Search to create a search filter and I need to find all the values saved in the database of the "cambio" column without repeating the values.

The values are saved as follows: "Manual de 5 marchas" or "Manual de 6 marchas"....

I created this query to return all saved values:

GET /crawler10/crawler-vehicles10/_search
{
  "size": 0,
  "aggregations": {
    "my_agg": {
      "terms": {
        "field": "cambio"
      }
    }
  }
}

But when I run the returned values they look like this:

   "aggregations": {
      "my_agg": {
         "doc_count_error_upper_bound": 2,
         "sum_other_doc_count": 2613,
         "buckets": [
            {
               "key": "de",
               "doc_count": 2755
            },
            {
               "key": "marchas",
               "doc_count": 2714
            },
            {
               "key": "manual",
               "doc_count": 2222
            },
            {
               "key": "modo",
               "doc_count": 1097
            },
            {
               "key": "5",
               "doc_count": 1071
            },
            {
               "key": "d",
               "doc_count": 1002
            },
            {
               "key": "n",
               "doc_count": 1002
            },
            {
               "key": "automática",
               "doc_count": 935
            },
            {
               "key": "com",
               "doc_count": 919
            },
            {
               "key": "6",
               "doc_count": 698
            }
         ]
      }
   }

Upvotes: 0

Views: 32

Answers (1)

user378704
user378704

Reputation:

Aggregations are based on the mapping type of the saved field. The field type for cambio seems to be set to analyzed(by default). Please create an index with the mapping not_analyzed for your field cambio.

You can create the index with a PUT request as below (if your ES version is less than 5) and then you will need to re-index your data in the crawler10 index.

PUT crawler10/_mapping/
{
  "mappings": {
    "crawler-vehicles10": {
      "properties": {
        "cambio": {
          "type": "string"
          "index": "not_analyzed"

        }
      }
    }
  }
}

For ES v5 or greater

PUT crawler10/_mapping/
{
  "mappings": {
    "crawler-vehicles10": {
      "properties": {
        "cambio": {
          "type": "keyword"
        }
      }
    }
  }
}

Upvotes: 1

Related Questions