0

I'm trying to find out if it's possible to facet/aggregate on a field, if that field (which is an array) contains 1 or more records.

For example - Let's say I have 3 documents in my index, all under the "user" type.

  {
    "id": 1,
    "name": "Thomas",
    "surname": "Test",
    "photos": []
  },
  {
    "id": 2,
    "name": "Thomas",
    "surname": "Test",
    "photos": [
      "photo 1",
      "photo 2",
      "photo 3"
    ]
  },
  {
    "id": 3,
    "name": "Thomas",
    "surname": "Test",
    "photos": [
      "photo 1",
      "photo 2",
      "photo 3"
    ]
  }

I want some kind of way (using aggregations) to return the "number of users who have (or who don't have, seeing as I can just subtract that from the _totalHits) photos in their profile." In this example, the expected result will be 2, seeing as 2 users have photos, or 1, seeing as 1 user doesn't have a photos. Obviously the preferred one I want is the one returning the count of users who have photos.

Is this possible? If so - which type of aggregation would I use to accomplish this?

iLikeBreakfast
  • 1,545
  • 23
  • 46

1 Answers1

0

The Missing Aggregation seems like the right tool:

A field data based single bucket aggregation, that creates a bucket of all documents in the current document set context that are missing a field value (effectively, missing a field or having the configured NULL value set). This aggregator will often be used in conjunction with other field data bucket aggregators (such as ranges) to return information for all the documents that could not be placed in any of the other buckets due to missing field data values.

Everything not in in this bucket would be the users that do have photos. Useful reference page in the guide: Dealing with Null Values.

BenG
  • 1,292
  • 8
  • 11