Recently, I had to work on a client request to add autocomplete to their site's search based not just on a specific list but on related keywords.

For example, suppose you had a list of TV series like Simpsons, Futurama, Tom and Jerry etc. The autosearch had to suggest the series based on character names like Homer, Thomas etc as well.

The project piggy-backed on the elasticsearch-dsl python library.

The list of objects that had to be displayed in the autocomplete were saved in a django model and the related keywords in a column (as JSON) in the same db table.

So every time the django model was saved, the autocomplete index would be built afresh.

Here's a snippet of this:

from elasticsearch_dsl import Index
from elasticsearch_dsl.connections import connections
from elasticsearch_dsl.mapping import Mapping

from .models import TVSeries


@receiver(post_save, sender=TVSeries)
def update_category_autosuggest(sender, **kwargs):
    try:
        es = connections.get_connection('default')
    except KeyError:
        es = connections.create_connection(
            'default',
            hosts=settings.ELASTICSEARCH_HOSTS
        )
     # settings.AUTOSUGGEST_INDEX is the name of the index
     index = Index(settings.AUTOSUGGEST_INDEX).delete(ignore=404)

     mm = Mapping('some_mapping_name')
     # Creating an Elasticsearch schema with the fields and data types
     mm.field("series", "string")
     mm.field("keywords", "string", multi=True)
     mm.field(
         "autosuggest",
         "completion",
         index_analyzer="simple",
         search_analyzer="simple",
         payloads=True
     )
     index = settings.AUTOSUGGEST_INDEX
     mm.save(index)

     for series in TVSeries.objects.filter(is_active=True):
         # Example: {"keywords": ["homer simpson", "thomas", "tom", "bart simpson"]}
         keywords = json.loads(series.json_data).get("keywords", [])
         if not keywords:
             continue
         new_kwargs = keywords
         # Add the name of the TV series as well along
         # with the keywords
         for cat in series.name.split(" "):
             if len(cat) < 2:
                 continue
             new_kwargs.append(cat.lower())
         data = {
             "series": series.name,
             "keywords": keywords,
             "autosuggest": {
                 "input": new_kwargs,
                 "output": series.name,
                 # There's a slug field for the TV Series which
                 # references the correct URL of the series
                 "payload": series.slug
             }
         }
         meta = es.index(
             index=index,
             doc_type=mm.doc_type,
             body=data)

After the index is built, the search query needs to be handled. In the view:

from elasticsearch_dsl.connections import connections
from elasticsearch_dsl import Search

def series_autocomplete(request):
    query = request.GET.get("q", "").strip()
    try:
        CONNECTION = connections.get_connection("default")
    except KeyError:
        CONNECTION = connections.create_connection(
            "default",
            hosts=settings.ELASTICSEARCH_HOSTS
        )
    ss = Search(CONNECTION, index=settings.AUTOSUGGEST_INDEX)
    results = []
    for qq in query.split(" "):
        response = ss.suggest(
            "suggestions",
            qq,
            completion={
                "field": "autosuggest",
                "fuzzy": True
            }
        ).execute().to_dict()
        results.extend(response["suggest"]["suggestions"])
    return HttpResponse(
        json.dumps(results),
        content_type="application/json")

Simple Command Line Dropbox Uploader in Python

Sun 10 April 2016 by Thejaswi Puthraya

I don't use Dropbox frequently. In fact, I would prefer not to use it but work requires me to use it from time to time. My only use case was to upload files onto a shared folder. So for my usage, it makes no sense to run the desktop ...

read more

Micro

Mon 21 March 2016 by Thejaswi Puthraya

"Micro" by Michael Crichton (author of "Jurassic Park") is the story of a group of research students who get miniaturized by a company when they get interested in a murder's leads. Does the team survive the world where even insects appear like giants and against time when their bodies ...

read more

Heart of Darkness

Tue 23 February 2016 by Thejaswi Puthraya

What image would the word 'sailor' invoke for you? For me it would be Haddock from Tintin, an alcoholic captain who mouths expletives when sober but a damn good seafarer. But after reading the "Heart of Darkness" by Joseph Conrad, it has changed to Charles Marlow.

Set in the early ...

read more

Don't make me think!

Mon 15 February 2016 by Thejaswi Puthraya

Steve Krug is renowned user experience professional and is the author of several popular books in this area. "Don't make me think!" is his most widely read book. I was recommended this book by a designer in response to my query on designing better online experiences.

I picked up ...

read more

Walden

Fri 22 January 2016 by Thejaswi Puthraya

If you've ever harboured thoughts of retiring to the country side and living off your own small farm, Walden is a book for you. Henry Thoreau, an American philosopher documents his experiences of living in solitude at the Walden lake.

The book lacks a structure but generally discusses a ...

read more

The Element

Tue 24 November 2015 by Thejaswi Puthraya

What does being in one's 'element' mean? How does one get to that state of mind? Before answering these questions, I saw Ken Robinson's TED talk on "Do schools kill creativity?" and resolved to read atleast one book by him to learn more about his ideas.

Now to ...

read more

The suffrage of Elvira

Thu 17 September 2015 by Thejaswi Puthraya

This was my first VS Naipaul novel and I picked it up with a lot of reluctance especially over his chauvinistic views. But boy, this book lived up to it's expectations as a super comedy.

This book describes the first election at Trinidad. The electorate is multi-cultural with Hindus ...

read more

The Little Black book of Innovation

Sun 28 June 2015 by Thejaswi Puthraya

Can innovation be successfully instilled and nurtured within a company? Scott Anthony pulls out quite a few case studies from his career as an "Innovation Expert" and analyzes them for the reader.

This is a book from the HBR stable and is a passable read. The main reason for this ...

read more

The Fist of God

Wed 24 June 2015 by Thejaswi Puthraya

Another classic Frederick Forsyth novel that is set during the First Persian Gulf war. A British secret agent is sent into Iraq to locate and destroy a nuclear weapon codenamed "The Fist of God".

He needs to contact an Iraqi insider who is willing to provide this top-secret information for ...

read more