Elasticsearch-dsl-py: skip_empty=False and nested objects

Created on 23 Dec 2018  路  2Comments  路  Source: elastic/elasticsearch-dsl-py

I am indexing a category structure, and if categories live as children of another category, they are listed inside a children_data field.

        "_index": "storefront_catalog",
        "_type": "category",
        "_id": "2",
        "_score": 1,
        "_source": {
          "id": 2,
          "parent_id": 0,
          "name": "Books",
          "is_active": true,
          "position": 2,
          "level": 2,
          "product_count": 1,
          "children_data": [
            {
              "id": 3,
              "parent_id": 2,
              "name": "Fiction",
              "is_active": true,
              "position": 2,
              "level": 3,
              "product_count": 1,
              "tsk": 0,
              "include_in_menu": 0,
              "sgn": ""
            },
            {
              "id": 5,
              "parent_id": 2,
              "name": "Non-Fiction",
              "is_active": true,
              "position": 2,
              "level": 3,
              "product_count": 1,
              "tsk": 0,
              "include_in_menu": 0,
              "sgn": ""
            }
          ],
          "tsk": 0,
          "include_in_menu": 0,
          "sgn": ""
        }

And if there are no child categories I need the children_data field to exsist even though empty. I am able to do that with obj.to_dict(skip_empty=False) But this only work on the main object, not the nested values, as you can see from example above the children_data objects do not have an empty children_data field, even though they are set to [] in serializer.

They do work on main category object, and result are like:

{
        "_index": "vue_storefront_catalog",
        "_type": "category",
        "_id": "1",
        "_score": 1,
        "_source": {
          "id": 1,
          "parent_id": 0,
          "name": "Clothing",
          "is_active": true,
          "position": 2,
          "level": 2,
          "product_count": 1,
          "children_data": [],
          "tsk": 0,
          "include_in_menu": 0,
          "sgn": ""
        }
      }

These are my functions I use for indexing:

def category_subs(category, parent):
    depth = category.get_depth()
    sub_categories = []
    obj = InnerCategoriesIndex(
        id = category.id,
        parent_id = parent.id,
        name = category.name,
        is_active = True,
        position = 2,
        level = depth + 1,
        product_count = 1,
        children_data = sub_categories,
        tsk = 0,
        include_in_menu = 0,
        sgn = "",
    )
    obj.children_data
    return obj.to_dict(skip_empty=False)

def obj_indexing_category(category):
    rootpage = category.get_root()
    depth = category.get_depth()
    children_data = []
    if category.get_children():
        for child in category.get_children():
            obj_child = category_subs(child, category)
            children_data.append(obj_child)

    depth = category.get_depth()
    obj = CategoriesIndex(
        meta={
            'id': category.id,
        },
        id = category.id,
        parent_id = 0,
        name = category.name,
        is_active = True,
        position = 2,
        level = depth + 1,
        product_count = 1,
        children_data = children_data,
        tsk = 0,
        include_in_menu = 0,
        sgn = "",
    )
    obj.save(skip_empty=False)
    return obj.to_dict(include_meta=True, skip_empty=False)

This might not be an issue and intended behaviour, but I dont see why this should not be valid.

Most helpful comment

In this case it's integration towards a 3d party system, that requires this field set regardless of empty or not.

All 2 comments

... I need the children_data field to exsist even though empty.

Why is that, do you have a specific use case that is not possible without the field present?

In this case it's integration towards a 3d party system, that requires this field set regardless of empty or not.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rokcarl picture rokcarl  路  4Comments

njoannin picture njoannin  路  3Comments

ypkkhatri picture ypkkhatri  路  4Comments

primoz-k picture primoz-k  路  4Comments

zahir-koradia picture zahir-koradia  路  3Comments