3

I query ES index to filter results and get aggregations by selected terms. A sample query is like this:

GET buyer_requests/vehicle_requests/_search
{
  "query": {
    "filtered": {
        "filter": {
          "and": [
            {
              "terms": {
                "vehicle.make.raw": [
                  "Audi",
                  "BMW",
                  "Chevrolet"
                ]
              }
            },
            {
              "range": {
                "style.price": {
                  "gte": 15000,
                  "lte": 20000
                }
              }
            },
            {
              "geo_distance": {
                "distance": "20000km",
                "info.pin": {
                  "lat": 42,
                  "lon": 21
                }
              }
            }
          ]
        }
    }
  }, 
  "aggs": {
    "makes": {
      "filter": {
        "range": {
          "style.price": {
            "gte": 5000,
            "lte": 40000
          }
        }
      },
      "aggs": {
        "makes": {
          "terms": {
            "field": "vehicle.make.raw",
            "order": {
              "_term": "asc"
            }
          }
        }
      }
    },
    "model": {
      "filter": {
        "and": [
          {
            "terms": {
              "vehicle.make.raw": [
                "Audi",
                "BMW",
                "Chevrolet"
              ]
            }
          }
        ]
      },
      "aggs": {
        "models": {
          "terms": {
            "field": "vehicle.model.raw",
            "size": 10,
            "order": {
              "_term": "asc"
            }
          }
        }
      }
    }
  }
}

The result I get is something like: Elasicsearch results

How can I get in "buckets" section on "models" terms another field from result set. I want to get reference to Makes so the result would look like this:

"model": {
   "doc_count": 7,
   "models": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
         {
            "key": "3 Series",
            "make": "bmw",                  <----------- this key
            "doc_count": 3
         },
         {
            "key": "4 Series",
            "make": "bmw",                  <----------- this key
            "doc_count": 4
         },
         {
           "key": "Camaro",
           "make": "chevrolet",             <----------- this key
           "doc_count": 2
         }
      ]
   }
}
ppavlovic
  • 53
  • 7

1 Answers1

3

You need to move your models aggregation as a sub-aggregation of the make aggregation and re-arrange the filter aggregation a bit. The result won't be syntactically like you expect, but semantically you'll get the data you need.

GET buyer_requests/vehicle_requests/_search
{
  "query": {
    "filtered": {
      "filter": {
        "and": [
          {
            "terms": {
              "vehicle.make.raw": [
                "Audi",
                "BMW",
                "Chevrolet"
              ]
            }
          },
          {
            "range": {
              "style.price": {
                "gte": 15000,
                "lte": 20000
              }
            }
          },
          {
            "geo_distance": {
              "distance": "20000km",
              "info.pin": {
                "lat": 42,
                "lon": 21
              }
            }
          }
        ]
      }
    }
  },
  "aggs": {
    "makes": {
      "filter": {
        "and": [
          {
            "terms": {
              "vehicle.make.raw": [
                "Audi",
                "BMW",
                "Chevrolet"
              ]
            }
          },
          {
            "range": {
              "style.price": {
                "gte": 5000,
                "lte": 40000
              }
            }
          }
        ]
      },
      "aggs": {
        "makes": {
          "terms": {
            "field": "vehicle.make.raw",
            "order": {
              "_term": "asc"
            }
          },
          "aggs": {
            "models": {
              "terms": {
                "field": "vehicle.model.raw",
                "size": 10,
                "order": {
                  "_term": "asc"
                }
              }
            }
          }
        }
      }
    }
  }
}
Val
  • 207,596
  • 13
  • 358
  • 360