Elasticsearch and Django

18 cards   |   Total Attempts: 188
  

Cards In This Set

Front Back
Step 1: how to create a connection from django app to elasticsearch.
  • create new search.py in app folder
  • from elasticsearch_dsl.connections import connections
  • connections.create_connection()
Step 2: how to set up index for a model? (Define a mapping from model_index to model data)
Answer 2
  • create a new model_index based on DocType
  • create individual fields in similar way like model
  • class_Meta:
  • ______ index = ""______ doc_type = ""
Step 3: how to create indexing method for each model?
Answer 3
  • inside each model class, create an indexing()
Step 4: how to run indexing for a model? ( the bulk command)
Answer 4
  • model_init()
  • es = Elasticsearch()
  • bulk(client=es, actions=(loop to index each row in the model)
Step 5: Create a search method
Answer 5
  • in search.py create a new method to search by a criteria to filter
  • create a search object = Search().filter(...)
  • return the response
A. Tell the workflow of automating indexing of any newly saved instance of model?
  1. Add a signal that fires the .indexing( ) on each new instance
  2. Register Django that we are using signals.
  3. Tell Django that we want to apply this signal to the app
A1. How to create a signal to fire a model method?
Answer 7
  • create a signals.py
  • create a method decorated with @receiver(post_save, sender=your_modelname)
A2. How to register Django that we are using signals
Answer 8
  • open the apps.py
  • create a new Config class based on the AppConfig
  • in the new class, create a name and method ready(self)
A3. How to tell Django that we want to apply this signal to the app?
Answer 9
1. open the __init__.py in the app folder2. register the new AppConfig we just created in step A2
When to run model_index.init(index="")?
Whenere we update a mapping, thisfunction would have configured Elasticsearch with the new document type.
How to create django command to run the bulk indexing?
  • Create main/management/commands/index_all_data.py
  • create a new command class based on BaseCommand
  • def handle()
  • run: python manage.py index_all_data
B. How to add elasticsearch to dajngo app?
  1. Create a search form for a template
  2. Modify a class-based view to handle elasticsearch
B2. How to modify a class-based view to handle elasticsearch?
  • Open the view class
  • add get()
  • in get(), create elasticsearch connection
  • create a search object = Search(index="").query("match", name="")
  • result = object.execute()
  • ctx['something"] = result.hits
How to handle elasticsearch connection in one place?
  • create a AppConfig object in apps.py
  • in the ready() mothod, create elasticsearch connection
How to create a search for a range of values?
Answer 15
  1. create dict variable
  2. s = Search(index="")
  3. s = s.query('range", price= dict_varibale)
  4. result = s.execute()
  5. ctx['name'] = result.hits
  6. return render(request, "template.html", ctx)