Django - fulltext search with PostgreSQL and Elasticsearch Django - fulltext search with PostgreSQL and Elasticsearch postgresql postgresql

Django - fulltext search with PostgreSQL and Elasticsearch


I would suggest you consider using PostgreSQL only to do what you asked for.

In my opinion it is the best solution because you will have the data and the search indexes directly inside PostgreSQL and you will not be forced to install and maintain additional software (such as Elasticsearch) and keep the data and indexes in sync.

This is the simplest code example you can have to perform a full-text search in Django with PostgreSQL:

Entry.objects.filter(body_text__search='Cheese')

For all the basic documentation on using the full-text search in Django with PostgreSQL you can use the official documentation: "Full text search"

If you want to deepen further you can read an article that I wrote on the subject:

"Full-Text Search in Django with PostgreSQL"


Your question is too broad to be answered with code, but it's definitely possible.

You can easily search your elasticsearch for rows matching your full-text criteria.

Then get those rows' PK fields (or any other candidate key, used to uniquely identify rows in your PostgreSQL dB), and filter your django ORM-backed models for PKs matching those you found from your Elasticsearch.

Pseudocode would be:

def get_chunk(l, length):    for i in xrange(0, len(l), length):        yield l[i:i + length]res = es.search(index="index", body={"query": {"match": ...}})pks = []for hit in res['hits']:    pks.append(hit['pk'])for chunk_10k in get_chunk(pks, 10000):    DjangoModel.objects.filter(pk__in=chunk_10k, **the_rest_of_your_api_filters)

EDIT
To resolve a case in which lots and lots of PKs might be found with your elastic query, you can define a generator that yields successive 10K rows of the results, so you won't step over your DB query limit and to ensure best update performance. I've defined it above with a function called get_chunk.

Something like that would work for alternatives like redis, mongodb, etc ...