whatapalaver
whatapalaver

Reputation: 915

Elasticsearch - How to Generate Facets for Doubly Nested Objects

Using elasticsearch 7, I am trying to build facets for doubly nested objects. So in the example below I would like to pull out the artist id codes from the artistMakerPerson field. I can pull out the association which is nested at a single depth but I can't get the syntax for the nested nested objects.

You could use the following code in Kibana to recreate an example.

My mapping looks like this:

PUT test_artist
{
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
      "properties": {
        "object" : {
          "type" : "text",
          "fields" : {
            "raw" : {
              "type" : "keyword"
            }
          },
          "copy_to" : [
            "global_search"
          ]
        },
        "uniqueID" : {
          "type" : "keyword",
          "copy_to" : [
            "global_search"
          ]
        },
        "artistMakerPerson" : {
          "type" : "nested",
          "properties" : {
            "association" : {
              "type" : "keyword"
            },
            "name" : {
              "type" : "nested",
              "properties" : {
                "id" : {
                  "type" : "keyword"
                },
                "text" : {
                  "type" : "text",
                  "fields" : {
                    "raw" : {
                      "type" : "keyword"
                    }
                  },
                  "copy_to" : [
                    "gs_authority"
                  ]
                }
              }
            },
            "note" : {
              "type" : "text"
            }
          }
        }
      }
    }
}

Index a document with:

PUT /test_artist/_doc/123
{
  "object": "cup",
  "uniquedID": "123",
  "artistMakerPerson" : [
              {
                "name" : {
                  "text" : "Johann Kandler",
                  "id" : "A6734"
                },
                "association" : "modeller",
                "note" : "probably"
              },
              {
                "name" : {
                  "text" : "Peter Reinicke",
                  "id" : "A27702"
                },
                "association" : "designer",
                "note" : "probably"
              }
            ]
} 

I am using this query to pull out facets or aggregations for artistMakerPerson.association

GET test_artist/_search
{
  "size": 0,
  "aggs": {
    "artists": {
      "nested": {
        "path": "artistMakerPerson"
      },
      "aggs": {
        "kinds": {
          "terms": {
            "field": "artistMakerPerson.association",
            "size": 10
          }
        }
      }
    }
  }
}

and I am rewarded with buckets for designer and modeller but I get nothing when I try to pull out the deeper artist id:

GET test_artist/_search
{
  "size": 0,
  "aggs": {
    "artists": {
      "nested": {
        "path": "artistMakerPerson"
      },
      "aggs": {
        "kinds": {
          "terms": {
            "field": "artistMakerPerson.name.id",
            "size": 10
          }
        }
      }
    }
  }
}

What am I doing wrong?

Upvotes: 1

Views: 910

Answers (1)

Joe - Check out my books
Joe - Check out my books

Reputation: 16905

Change the path from artistMakerPerson to artistMakerPerson.name.

GET test_artist/_search
{
  "size": 0,
  "aggs": {
    "artists": {
      "nested": {
        "path": "artistMakerPerson.name"
      },
      "aggs": {
        "kinds": {
          "terms": {
            "field": "artistMakerPerson.name.id",
            "size": 10
          }
        }
      }
    }
  }
}

Upvotes: 3

Related Questions