Skip to content

Instantly share code, notes, and snippets.

@llvtt
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save llvtt/4f4b6e6c2e6372ab0d32 to your computer and use it in GitHub Desktop.

Select an option

Save llvtt/4f4b6e6c2e6372ab0d32 to your computer and use it in GitHub Desktop.
DocumentFormatter for flattening documents for use in Solr. Does not include array indices.
from mongo_connector.doc_managers.formatters import DocumentFlattener
class DocumentFlattenerUnordered(DocumentFlattener):
"""Formatter that completely flattens documents, but does not unwind arrays.
Order of elements within arrays is not recorded.
An example:
{"a": 2,
"b": {
"c": {
"d": 5
}
},
"e": [6, 7, 8]
}
becomes:
{"a": 2, "b.c.d": 5, "e": [6, 7, 8]}
"""
def transform_element(self, key, value):
if isinstance(value, list):
result = {}
for lv in value:
for inner_k, inner_v in self.transform_element(key, lv):
inner_l = result.get(inner_k, [])
if isinstance(inner_v, list):
inner_l.extend(inner_v)
else:
inner_l.append(inner_v)
result[inner_k] = inner_l
for k in result:
yield k, result[k]
else:
parent = super(DocumentFlattenerUnordered, self)
for k, v in parent.transform_element(key, value):
yield k, v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment