Skip to content

Commit

Permalink
handle index creating with mapping in single step (#117)
Browse files Browse the repository at this point in the history
atm we are getting errors on test server when putting
mapping to non existing index, so avoid creation of
indices without mapping.
  • Loading branch information
petrjasek authored Dec 20, 2024
1 parent b1d7bba commit 244a112
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
40 changes: 24 additions & 16 deletions eve_elastic/elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,13 +415,16 @@ def init_index(self, resource=None, raise_on_mapping_error=False):
"mapping error, updating settings resource=%s", _resource
)

def _init_index(self, es, index, settings=None, mapping=None):
def _init_index(
self, es: Elasticsearch, index: str, settings=None, mappings=None
):
if not es.indices.exists(index):
self._create_index_from_alias(es, index, settings)
elif settings:
self._put_settings(es, index, settings)
if mapping:
self._put_mapping(es, index, mapping)
self._create_index_from_alias(es, index, settings, mappings)
else:
if settings:
self._put_settings(es, index, settings)
if mappings:
self._put_mappings(es, index, mappings)

def get_datasource(self, resource):
return getattr(self, "_datasource", self.datasource)(resource)
Expand Down Expand Up @@ -458,17 +461,22 @@ def _get_field_mapping(self, schema):
elif schema["type"] == "integer":
return {"type": "integer"}

def _create_index_from_alias(self, es, alias, settings=None):
def _create_index_from_alias(
self, es: Elasticsearch, alias: str, settings=None, mappings=None
) -> None:
"""Create new index and ignore if it exists already."""
try:
index = generate_index_name(alias)
self._create_index(es, index, settings)
es.indices.put_alias(index, alias)
logger.info("created index alias=%s index=%s" % (alias, index))
except elasticsearch.TransportError: # index exists
pass
index = generate_index_name(alias)
es.indices.create(
index=index,
body={
"aliases": {alias: {}},
"settings": {"index": settings["settings"]} if settings else {},
"mappings": fix_mapping(mappings) if mappings else {},
},
)
logger.info("created index index=%s alias=%s", index, alias)

def _create_index(self, es, index, settings=None):
def _create_index(self, es: Elasticsearch, index, settings=None):
args = {"index": index, "body": {}}
if settings:
args["body"].update(settings)
Expand Down Expand Up @@ -520,7 +528,7 @@ def _get_mapping_properties(self, resource_config, parent=None):
properties["properties"].pop("_id", None)
return properties

def _put_mapping(self, es, index, mapping=None):
def _put_mappings(self, es, index, mapping=None):
if mapping:
es.indices.put_mapping(index=index, body=fix_mapping(mapping))

Expand Down
4 changes: 4 additions & 0 deletions test/test_elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import eve
import time
import pytest
import logging
import elasticsearch

from unittest import TestCase, skip
Expand All @@ -15,6 +16,9 @@
from unittest.mock import MagicMock, patch


logging.getLogger("elasticsearch").setLevel(logging.DEBUG)


def highlight_callback(query_string):
elastic_highlight_query = {
"pre_tags": ['<span class="es-highlight">'],
Expand Down

0 comments on commit 244a112

Please sign in to comment.