I need to text-search across my model and filter with db queries at the same time.
For example:
class MyModel(models.Model):
text = models.TextField()
users = models.ManyToMany(User)
class MyModelIndexIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, model_attr='text')
def get_model(self):
return MyModel
So I want to filter all MyModel objects by user AND by some text via full-text search. Smth like these:
qs = MyModel.objects.filter(users=request.user)
sqs = MyModelIndex.objects.filter(text=request.GET['q'])
intersection = some_magic_function(qs, sqs)
or
intersection = some_other_magic_function(
qs_kwargs={'users': request.user},
sqs_kwargs={'text': request.GET['q']}
)
Of course desired db queries could be much more complicated.
I see some possible solutions, all with major flaws:
Make intersection in django: extract ids from qs and use them in sqs filter or vice versa. Problem: performance. We can workaround itby using pagination and do intersection only for given page and its predecessors. In this case we lose total count (
Index all m2m related fields. Problem: performance, duplicate functionality (I believe db will do such queries much better), db-features such as annotations etc.
Do not use haystack ( Go for mysql or posgresql built-in full-text search.
I believe I miss something obvious. Case seems to be quite common. Is there a conventional solution?