Search code examples
elasticsearchelasticsearch-dsl

How to use index in search API in elasticsearch-dsl(5.4.0)


I'm using elasticsearch-dsl(5.4.0) and elasticsearch(5.5.3). I defined EsTask class with an inner class named Meta class as follows in model layer file:

/task_models.py

class EsTask(DocType):
    id = Keyword()
    catagory_id = Integer()
    name = Text(analyzer='ik_max_word', search_analyzer='ik_smart')
    priority_level = Integer()
    related_id = Keyword()
    parent_id = Keyword()
    creator_id = Keyword()
    created_at = Date()
    deleted_at = Date()

    class Meta:
        index = 'task_es'
        doc_type = 'main'

I call search API in controller layer file:

/task.py

s = EsTask.search().filter('bool', must_not=elasticsearch_dsl.Q('exists', field='deleted_at'))

May I use keyword argument calling search() like search(index='task_es', doc_type='main') as expected if I want to ensure the search API use the indexes I defined in Meta Class above? Or that is necessarily and I can just leave search API without any argument?


Solution

  • You can leave it without any arguments as it will do it automatically.

    Hope this helps!