I am trying to define custom DocType in Python using the ElasticSearch DSL library but am having some trouble, specifically with Nested classes. In the example below, both the fields author and mentions have the same type of properties but I don’t know how to define those properties in a separate class. Any help would be appreciated!
class DiscordMessage(DocType):
"""Discord Message."""
id = Integer()
content = Text(
fields={'raw': Keyword()}
)
author = Nested(
properties={
'id': Integer(),
'name': Text(fields={'raw': Keyword()}),
'username': Text(fields={'raw': Keyword()}),
'display_name': Text(feidls={'raw': Keyword()}),
'bot': Boolean(),
'top_role': Nested(
doc_class=DiscordRole,
properties={
'id': Integer(),
'name': Text(fields={'raw': Keyword()})
}
),
'joined_at': Date(),
'roles': Nested(
doc_class=DiscordRole,
properties={
'id': Integer(),
'name': Text(fields={'raw': Keyword()})
}
)
}
)
server = Nested(
properties={
'id': Integer(),
'name': Text(fields={'raw': Keyword()})
}
)
channel = Nested(
properties={
'id': Integer(),
'name': Text(fields={'raw': Keyword()}),
'position': Integer(),
'is_default': Boolean()
}
)
mentions = Nested(
properties={
'id': Integer(),
'name': Text(fields={'raw': Keyword()}),
'username': Text(fields={'raw': Keyword()}),
'display_name': Text(feidls={'raw': Keyword()}),
'bot': Boolean(),
'top_role': Nested(
doc_class=DiscordRole,
properties={
'id': Integer(),
'name': Text(fields={'raw': Keyword()})
}
),
'joined_at': Date(),
'roles': Nested(
doc_class=DiscordRole,
properties={
'id': Integer(),
'name': Text(fields={'raw': Keyword()})
}
)
}
}
timestamp = Date()
class Meta:
index = 'discord'
def set_author(self, author):
"""Set author."""
self.author = {
'id': author.id,
'name': author.display_name,
'username': author.name,
'display_name': author.display_name,
'bot': author.bot,
'roles': [
{'id': r.id, 'name': r.name} for r in author.roles
],
'top_role': {
'id': author.top_role.id,
'name': author.top_role.name
},
'joined_at': author.joined_at
}
def set_server(self, server):
"""Set server."""
self.server = {
'id': server.id,
'name': server.name
}
def set_channel(self, channel):
"""Set channel."""
self.channel = {
'id': channel.id,
'name': channel.name,
'is_default': channel.is_default,
'position': channel.position
}
def save(self, **kwargs):
return super(DiscordMessage, self).save(**kwargs)
In particular, you see that I am setting properties using methods and I felt that I am setting them both in the definition and the method and it seems excessive. Or is that in fact necessary?