import json
import os

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Index
from model import APIDoc


def exists():
    return Index(APIDoc.Index.name).exists()


def setup():
    """
    Setup Elasticsearch Index.
    Primary index with dynamic template.
    Secondary index with static mappings.
    """
    _dirname = os.path.dirname(__file__)
    with open(os.path.join(_dirname, 'mapping.json'), 'r') as file:
        mapping = json.load(file)

    if not exists():
        APIDoc.init()
        elastic = Elasticsearch()
        elastic.indices.put_mapping(
            index=APIDoc.Index.name,
            body=mapping
        )


def delete():
    Index(APIDoc.Index.name).delete()


def reset():

    if exists():
        delete()

    setup()


def refresh():

    index = Index(APIDoc.Index.name)
    index.refresh()

import json
import os

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Index
from model import APIDoc


def exists():
    return Index(APIDoc.Index.name).exists()


def setup():
    """
    Setup Elasticsearch Index with dynamic template.
    Run it on an open index to update dynamic mapping.
    """
    _dirname = os.path.dirname(__file__)
    with open(os.path.join(_dirname, 'mapping.json'), 'r') as file:
        mapping = json.load(file)

    if not exists():
        APIDoc.init()

    elastic = Elasticsearch()
    elastic.indices.put_mapping(
        index=APIDoc.Index.name,
        body=mapping
    )


def delete():
    Index(APIDoc.Index.name).delete()


def reset():

    if exists():
        delete()

    setup()


def refresh():

    index = Index(APIDoc.Index.name)
    index.refresh()

Allow setup function to update dynamic mapping
Allow setup function to update dynamic mapping

  import json
  import os
  
  from elasticsearch import Elasticsearch
  from elasticsearch_dsl import Index
  from model import APIDoc
  
  
  def exists():
      return Index(APIDoc.Index.name).exists()
  
  
  def setup():
      """
+     Setup Elasticsearch Index with dynamic template.
+     Run it on an open index to update dynamic mapping.
-     Setup Elasticsearch Index.
-     Primary index with dynamic template.
-     Secondary index with static mappings.
      """
      _dirname = os.path.dirname(__file__)
      with open(os.path.join(_dirname, 'mapping.json'), 'r') as file:
          mapping = json.load(file)
  
      if not exists():
          APIDoc.init()
+ 
-         elastic = Elasticsearch()
+     elastic = Elasticsearch()
-         elastic.indices.put_mapping(
+     elastic.indices.put_mapping(
-             index=APIDoc.Index.name,
+         index=APIDoc.Index.name,
-             body=mapping
+         body=mapping
-         )
+     )
  
  
  def delete():
      Index(APIDoc.Index.name).delete()
  
  
  def reset():
  
      if exists():
          delete()
  
      setup()
  
  
  def refresh():
  
      index = Index(APIDoc.Index.name)
      index.refresh()
  
Allow setup function to update dynamic mapping
## Code Before:
import json
import os

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Index
from model import APIDoc


def exists():
    return Index(APIDoc.Index.name).exists()


def setup():
    """
    Setup Elasticsearch Index.
    Primary index with dynamic template.
    Secondary index with static mappings.
    """
    _dirname = os.path.dirname(__file__)
    with open(os.path.join(_dirname, 'mapping.json'), 'r') as file:
        mapping = json.load(file)

    if not exists():
        APIDoc.init()
        elastic = Elasticsearch()
        elastic.indices.put_mapping(
            index=APIDoc.Index.name,
            body=mapping
        )


def delete():
    Index(APIDoc.Index.name).delete()


def reset():

    if exists():
        delete()

    setup()


def refresh():

    index = Index(APIDoc.Index.name)
    index.refresh()

## Instruction:
Allow setup function to update dynamic mapping
## Code After:
import json
import os

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Index
from model import APIDoc


def exists():
    return Index(APIDoc.Index.name).exists()


def setup():
    """
    Setup Elasticsearch Index with dynamic template.
    Run it on an open index to update dynamic mapping.
    """
    _dirname = os.path.dirname(__file__)
    with open(os.path.join(_dirname, 'mapping.json'), 'r') as file:
        mapping = json.load(file)

    if not exists():
        APIDoc.init()

    elastic = Elasticsearch()
    elastic.indices.put_mapping(
        index=APIDoc.Index.name,
        body=mapping
    )


def delete():
    Index(APIDoc.Index.name).delete()


def reset():

    if exists():
        delete()

    setup()


def refresh():

    index = Index(APIDoc.Index.name)
    index.refresh()
