In Django, I have 2 models Keyword and KeywordPopularity.
class Keyword(models.Model):
name = models.CharField(max_length=50)
class KeywordPopularity(models.Model):
keyword = models.ForeignKey(Keyword, on_delete=models.CASCADE, related_name='popularities')
date = models.DateField(auto_now_add=True)
popularity = models.IntegerField()
I run a script to detect the popularity of each keyword once a month, so a keyword has multiple popularities (with different date of couse).
I set up elasticsearch dsl to search keywords by query and it worked fine.
@registry.register_document
class KeywordDocument(Document):
class Index:
name = 'keywords'
class Django:
model = Keyword
fields = ['name']
when I search for a query, i used this command and it works:
KeywordDocument.search().query('match', name=query)[fromOffset:toOffset]
My question is: how to search for a query then sort by popularity in a given date range. What I'm doing right now is to get all keywords that match the query then find each one popularity then sort. But it is very slow:
keywords = KeywordDocument.search().extra(size=10000).query('match', name=query).to_queryset()
result = []
for keyword in keywords:
popularities = keyword.popularities.filter(date__gte=startDate, date__lte=endDate).order_by('date')
data = KeywordSerializer(keyword).data
data['popularity'] = popularities.last().popularity if popularities.count() > 0 else -1
result.append(data)
result = sorted(result, key=lambda keyword: -keyword['popularity'])
Could you please help me optimize the above snippet?
Thank you very much!