Faceting for array of dictionaries with elasticsearch-dsl Python

I'm using elasticsearch-dsl to add search functionality to my Python/Django app.

I'm pretty new to Elasticsearch and still learning as I go, but I have the base search functionality working. I'm now trying to implement some type of faceting.

To simplify somewhat, the model I'm indexing has a field called data, which consists of an array of dictionaries, kind of like tags. They are added by the user when creating a new model instance, so cannot be predefined fields. They will vary from instance to instance, and provide some random supplementary details about the model instance. For example:

{'color': 'red', 'size'; 'M'} or {'condition': 'new', 'manufacturer': 'Testco'}

class MyIndex(Document):
    category = Text()
    title = Text()
    description = Text()
    data = Nested()

    class Index:
        name = 'my-index'

Is there a way that I can set up faceting for the data field?

Ideally I would like to be able to perform a search, then have filters for color, manufacturer, size and any other keys present in the data lists.

So far I've tried this, which (expectedly), returns nothing when I execute the search and check response.facets.data:

class ModelSearch(FacetedSearch):
    doc_types = [MyIndex]
    fields = ['title^3', 'category']
    facets = {
        'data': NestedFacet('data', TermsFacet(field='data.*'))
    }

    def search(self):
        return super().search()

I'm not really sure how to approach this given the dynamic/nested nature of the information I want to access.

Here's how the data looks in Elasticsearch:

{
    "_score": 2.3287308,
    "_type": "_doc",
    "_id": "193",
    "_source": {
      "category": "Property To Buy",
      "description": "3 bedroom house with 2 garages and garden.",
      "title": "3 bedroom house",
      "data": [
        {
          "bedrooms": "3"
        },
        {
          "garages": "2"
        },
        {
          "type": "detached"
        }
      ]
    },
    "_index": "my-index"
},
{
    "_score": 2.2227821,
    "_type": "_doc",
    "_id": "190",
    "_source": {
      "category": "Property To Buy",
      "description": "6 bedroom house with gardens and garages. Quiet and peaceful spot.",
      "title": "Detached 6 Bedroom House",
      "data": [
        {
          "county": "West Yorkshire"
        }
        {
          "type": "detached"
        }
      ]
    },
    "_index": "my-index"
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.