How to use reverse_nested in this aggs query?


#1

I've successfully implemented nesting and inner_hits, which is great. However, I'm using Searchkit and the filters have built-in aggregators which are now showing aggregations over inner_hits, not their parent docs. From googling, I think reverse_nested should fix this; I just can't figure out where to place it. This is what I've attempted:

"editions.rarity.raw163": {
  "filter": {
    ...
  },
  "aggs": {
    "inner": {
      "nested": {
        "path": "editions",
        "reverse_nested": {}
      },
      "aggs": {
        "editions.rarity.raw": {
          "terms": {
            "field": "editions.rarity.raw",
            "size": 6
          }
        },
        "editions.rarity.raw_count": {
          "cardinality": {
            "field": "editions.rarity.raw"
          }
        }
      }
    }
  }
}

All of this is generated by Searchkit's code, I can access it before it gets sent to the ES server and modify it, which is how I've managed to insert my test reverse_nested code. However right now it gives me a Bad Request 400 error, so I am clearly not putting this in the right place. Where should it go? The docs say it has to be in a nested query so that's where I put it.


(Adrien Grand) #2

It would look like this:

"editions.rarity.raw163": {
  "filter": {
    ...
  },
  "aggs": {
    "inner": {
      "nested": {
        "path": "editions"
      },
      "aggs": {
        "editions.rarity.raw": {
          "terms": {
            "field": "editions.rarity.raw",
            "size": 6
          },
          "aggs": {
            "outer": {
              "reverse_nested": {}
            }
          }
        },
        "editions.rarity.raw_count": {
          "cardinality": {
            "field": "editions.rarity.raw"
          }
        }
      }
    }
  }
}

#3

edit: It did actually work! However, it's returning the parent doc count like this:

"editions.rarity.raw13": {
  "doc_count": 3,
  "inner": {
    "doc_count": 19,
    "editions.rarity.raw": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "Rare",
          "doc_count": 14,
          "outer": {
            "doc_count": 2
          }
        },
        {
          "key": "Common",
          "doc_count": 5,
          "outer": {
            "doc_count": 1
          }
        }
      ]
    },
    "editions.rarity.raw_count": {
      "value": 2
    }
  }
}

Unfortunately the UI is still using the doc_count property directly under the bucket, rather than the outer one (and there's not a lot I can do about that, given it's deep in the UI code somewhere). Is there a way to overwrite the top-level doc_count with the doc_count currently trapped in the outer object?


(system) #4