# How to create indexes in bulk using Elasticseach\_dsl?

**URL:** <https://discuss.elastic.co/t/how-to-create-indexes-in-bulk-using-elasticseach-dsl/225525>\
**Category:** Elasticsearch\
**Created:** [March 29, 2020, 1:14am UTC](https://discuss.elastic.co/t/how-to-create-indexes-in-bulk-using-elasticseach-dsl/225525 "2020-03-29T01:14:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lisahtwy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lisahtwy/32/53910_2.png) [@Lisahtwy](https://discuss.elastic.co/u/Lisahtwy)\
**Post date:** [March 29, 2020, 1:14am UTC](https://discuss.elastic.co/t/how-to-create-indexes-in-bulk-using-elasticseach-dsl/225525/1 "2020-03-29T01:14:12Z")

</div>

Hi,

I was able to create a single index using below code:  
`Single index code`

```auto
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Index,Search,Document, analyzer, tokenizer,\
                           connections,Mapping, Nested, Text, Keyword, InnerDoc
from elasticsearch_dsl.field import Text

connections.create_connection(hosts=['localhost'])

my_analyzer = analyzer( \
    "my_analyzer", \
    type = "custom", \
    tokenizer=tokenizer("whitespace"),\
    filter=['lowercase'])

index1 = Index('myindex')
index1.analyzer(my_analyzer)
index1.create()

class File(InnerDoc):
    filename = Keyword(store=True)

class ResumeIndex(Document):
    content = Text(analyzer = my_analyzer)
    file = Nested()

    class Index:
        name = "myindex"

ResumeIndex.init()
response = ResumeIndex._index.analyze(body={'analyzer':'my_analyzer', 'text': 'c# developer'})
print(response)

```

The mapping output for single index code is: (BEFORE REINDEXING USING FSCRAWLER)

```auto
{
  "myindex" : {
    "mappings" : {
      "properties" : {
        "content" : {
          "type" : "text",
          "analyzer" : "my_analyzer"
        },
        "file" : {
          "type" : "nested",
          "properties" : {
            "filename" : {
              "type" : "keyword",
              "store" : true
            }
          }
        }
      }
    }
  }
}

```

After this i am re-indexing using FSCrawler.  
But I need to create multiple indexes for different resources.

I am trying the below code but when I check the mapping, the index is not quite same as with the single index code.

```auto
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Index,Search,Document, analyzer, tokenizer,\
                           connections,Mapping, Nested, Text, Keyword, InnerDoc
from elasticsearch_dsl.field import Text
from pprint import pprint

connections.create_connection(hosts=['localhost'])

my_analyzer = analyzer( \
    "my_analyzer", \
    type = "custom", \
    tokenizer=tokenizer("whitespace"),\
    filter=['lowercase'])

index = ["index_trial1", "index_trial2"]

class File(InnerDoc):
    filename = Keyword(store=True)

class ResumeIndex(Document):
    content = Text(analyzer = my_analyzer)
    file = Nested(File)

    class Index:
        name = "myindex"

def createIndex():
    indeces = []
    #global my_analyzer
    for ind in index:
        index_dummy = Index(ind)
        index_dummy.analyzer(my_analyzer)
        index_dummy.create()

        R = ResumeIndex()
        R.Index.name = ind
        R.init()
        #indeces.append(R.Index.name)
        #print("indeces", indeces)
        #response = R._index.analyze(body={'analyzer':'my_analyzer', 'text': 'c# developer'})
        #print(response)

if __name__ == " __main__":
    createIndex()

```

It is creating index\_trial1, and index\_trial2 but the mappings are empty.  
the mapping output for the multiple index code:(BEFORE REINDEXING)

```auto
`GET /index_trial1/_mapping` 
{
  "index_trial1" : {
    "mappings" : { }
  }
}

```

Could anyone tell me how to create multiple indexes?

-Lisa

---

<div class="post-metadata">

**Author:** ![Lisahtwy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lisahtwy/32/53910_2.png) [@Lisahtwy](https://discuss.elastic.co/u/Lisahtwy)\
**Post date:** [March 30, 2020, 4:20pm UTC](https://discuss.elastic.co/t/how-to-create-indexes-in-bulk-using-elasticseach-dsl/225525/2 "2020-03-30T16:20:21Z")

</div>

Hello,

Below worked for me!

```auto
    from elasticsearch import Elasticsearch
    from elasticsearch_dsl import Index, Search, Document, analyzer, tokenizer, \
    connections, Mapping, Nested, Text, Keyword, InnerDoc
    from elasticsearch_dsl.field import Text
    from pprint import pprint

    es =connections.create_connection(hosts=['localhost'])

    my_analyzer = analyzer( \
    "my_analyzer", \
    type="custom", \
    tokenizer=tokenizer("whitespace"), \
    filter=['lowercase'])

    #N = 2 # no.of indexes to create
    index = ["index_trial1", "index_trial2", "index_trial3", "index_trial4"]

    def createIndex():

    m = Mapping()
    m.field('content', 'text', analyzer=my_analyzer)
    file = Nested( properties={ \
                       'filename': Keyword(store= True)})
    m.field('file', file)

    for ind in index:
        if es.indices.exists(index=ind):
            print(str(ind)+" exists already!!")
        else:
            m.save(ind)

    if __name__ == " __main__":
    createIndex()

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 27, 2020, 4:20pm UTC](https://discuss.elastic.co/t/how-to-create-indexes-in-bulk-using-elasticseach-dsl/225525/3 "2020-04-27T16:20:31Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
