[Python] Elasticsearch index creation from Python, results in error, for known mapping

Discussão em 'Python' iniciado por Stack, Agosto 22, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following code:

    from elasticsearch import Elasticsearch

    client = Elasticsearch(
    "https://myhost",
    api_key="mykey",
    request_timeout=30, # Increase timeout duration
    max_retries=10,
    retry_on_timeout=True
    )
    dest_index = "course_info_eng_v3d6_destination"
    client.indices.create(
    index=dest_index,
    mappings={
    "dynamic": "true",
    "dynamic_templates": [
    {
    "all_text_fields": {
    "match_mapping_type": "string",
    "mapping": {
    "analyzer": "iq_text_base",
    "fields": {
    "delimiter": {
    "analyzer": "iq_text_delimiter",
    "type": "text",
    "index_options": "freqs"
    },
    "joined": {
    "search_analyzer": "q_text_bigram",
    "analyzer": "i_text_bigram",
    "type": "text",
    "index_options": "freqs"
    },
    "prefix": {
    "search_analyzer": "q_prefix",
    "analyzer": "i_prefix",
    "type": "text",
    "index_options": "docs"
    },
    "enum": {
    "ignore_above": 2048,
    "type": "keyword"
    },
    "stem": {
    "analyzer": "iq_text_stem",
    "type": "text"
    }
    }
    }
    }
    }
    ],
    "properties": {
    "course_id": {
    "type": "text",
    "fields": {
    "delimiter": {
    "type": "text",
    "index_options": "freqs",
    "analyzer": "iq_text_delimiter"
    },
    "enum": {
    "type": "keyword",
    "ignore_above": 2048
    },
    "joined": {
    "type": "text",
    "index_options": "freqs",
    "analyzer": "i_text_bigram",
    "search_analyzer": "q_text_bigram"
    },
    "prefix": {
    "type": "text",
    "index_options": "docs",
    "analyzer": "i_prefix",
    "search_analyzer": "q_prefix"
    },
    "stem": {
    "type": "text",
    "analyzer": "iq_text_stem"
    }
    },
    "analyzer": "iq_text_base"
    },
    "course_name_backup": {
    "type": "text",
    "fields": {
    "delimiter": {
    "type": "text",
    "index_options": "freqs",
    "analyzer": "iq_text_delimiter"
    },
    "enum": {
    "type": "keyword",
    "ignore_above": 2048
    },
    "joined": {
    "type": "text",
    "index_options": "freqs",
    "analyzer": "i_text_bigram",
    "search_analyzer": "q_text_bigram"
    },
    "prefix": {
    "type": "text",
    "index_options": "docs",
    "analyzer": "i_prefix",
    "search_analyzer": "q_prefix"
    },
    "stem": {
    "type": "text",
    "analyzer": "iq_text_stem"
    }
    },
    "analyzer": "iq_text_base"
    },
    "embedding": {
    "type": "dense_vector",
    "dims": 768,
    "index": "true",
    "similarity": "cosine",
    "index_options": {
    "type": "int8_hnsw",
    "m": 16,
    "ef_construction": 100
    }
    },
    "language": {
    "type": "text",
    "fields": {
    "delimiter": {
    "type": "text",
    "index_options": "freqs",
    "analyzer": "iq_text_delimiter"
    },
    "enum": {
    "type": "keyword",
    "ignore_above": 2048
    },
    "joined": {
    "type": "text",
    "index_options": "freqs",
    "analyzer": "i_text_bigram",
    "search_analyzer": "q_text_bigram"
    },
    "prefix": {
    "type": "text",
    "index_options": "docs",
    "analyzer": "i_prefix",
    "search_analyzer": "q_prefix"
    },
    "stem": {
    "type": "text",
    "analyzer": "iq_text_stem"
    }
    },
    "analyzer": "iq_text_base"
    },
    "provider": {
    "type": "text",
    "fields": {
    "delimiter": {
    "type": "text",
    "index_options": "freqs",
    "analyzer": "iq_text_delimiter"
    },
    "enum": {
    "type": "keyword",
    "ignore_above": 2048
    },
    "joined": {
    "type": "text",
    "index_options": "freqs",
    "analyzer": "i_text_bigram",
    "search_analyzer": "q_text_bigram"
    },
    "prefix": {
    "type": "text",
    "index_options": "docs",
    "analyzer": "i_prefix",
    "search_analyzer": "q_prefix"
    },
    "stem": {
    "type": "text",
    "analyzer": "iq_text_stem"
    }
    },
    "analyzer": "iq_text_base"
    },
    "related_skills": {
    "type": "text",
    "fields": {
    "delimiter": {
    "type": "text",
    "index_options": "freqs",
    "analyzer": "iq_text_delimiter"
    },
    "enum": {
    "type": "keyword",
    "ignore_above": 2048
    },
    "joined": {
    "type": "text",
    "index_options": "freqs",
    "analyzer": "i_text_bigram",
    "search_analyzer": "q_text_bigram"
    },
    "prefix": {
    "type": "text",
    "index_options": "docs",
    "analyzer": "i_prefix",
    "search_analyzer": "q_prefix"
    },
    "stem": {
    "type": "text",
    "analyzer": "iq_text_stem"
    }
    },
    "analyzer": "iq_text_base"
    },
    "timestamp": {
    "type": "text",
    "fields": {
    "delimiter": {
    "type": "text",
    "index_options": "freqs",
    "analyzer": "iq_text_delimiter"
    },
    "enum": {
    "type": "keyword",
    "ignore_above": 2048
    },
    "joined": {
    "type": "text",
    "index_options": "freqs",
    "analyzer": "i_text_bigram",
    "search_analyzer": "q_text_bigram"
    },
    "prefix": {
    "type": "text",
    "index_options": "docs",
    "analyzer": "i_prefix",
    "search_analyzer": "q_prefix"
    },
    "stem": {
    "type": "text",
    "analyzer": "iq_text_stem"
    }
    },
    "analyzer": "iq_text_base"
    },
    "user": {
    "type": "text",
    "fields": {
    "delimiter": {
    "type": "text",
    "index_options": "freqs",
    "analyzer": "iq_text_delimiter"
    },
    "enum": {
    "type": "keyword",
    "ignore_above": 2048
    },
    "joined": {
    "type": "text",
    "index_options": "freqs",
    "analyzer": "i_text_bigram",
    "search_analyzer": "q_text_bigram"
    },
    "prefix": {
    "type": "text",
    "index_options": "docs",
    "analyzer": "i_prefix",
    "search_analyzer": "q_prefix"
    },
    "stem": {
    "type": "text",
    "analyzer": "iq_text_stem"
    }
    },
    "analyzer": "iq_text_base"
    }
    }
    }
    )


    But when running it, results in the following error:

    Traceback (most recent call last):
    File "C:\Users\david\Desktop\courserecsVDBs\test.py", line 62, in <module>
    client.indices.create(
    File "C:\Users\david\Desktop\courserecsVDBs\venv\lib\site-packages\elasticsearch\_sync\client\utils.py", line 452, in wrapped
    return api(*args, **kwargs)
    File "C:\Users\david\Desktop\courserecsVDBs\venv\lib\site-packages\elasticsearch\_sync\client\indices.py", line 705, in create
    return self.perform_request( # type: ignore[return-value]
    File "C:\Users\david\Desktop\courserecsVDBs\venv\lib\site-packages\elasticsearch\_sync\client\_base.py", line 422, in perform_request
    return self._client.perform_request(
    File "C:\Users\david\Desktop\courserecsVDBs\venv\lib\site-packages\elasticsearch\_sync\client\_base.py", line 271, in perform_request
    response = self._perform_request(
    File "C:\Users\david\Desktop\courserecsVDBs\venv\lib\site-packages\elasticsearch\_sync\client\_base.py", line 351, in _perform_request
    return api(*args, **kwargs)
    File "C:\Users\david\Desktop\courserecsVDBs\venv\lib\site-packages\elasticsearch\_sync\client\indices.py", line 705, in create
    return self.perform_request( # type: ignore[return-value]
    File "C:\Users\david\Desktop\courserecsVDBs\venv\lib\site-packages\elasticsearch\_sync\client\_base.py", line 422, in perform_request
    return self._client.perform_request(
    File "C:\Users\david\Desktop\courserecsVDBs\venv\lib\site-packages\elasticsearch\_sync\client\_base.py", line 271, in perform_request
    response = self._perform_request(
    File "C:\Users\david\Desktop\courserecsVDBs\venv\lib\site-packages\elasticsearch\_sync\client\_base.py", line 351, in _perform_request
    return self.perform_request( # type: ignore[return-value]
    File "C:\Users\david\Desktop\courserecsVDBs\venv\lib\site-packages\elasticsearch\_sync\client\_base.py", line 422, in perform_request
    return self._client.perform_request(
    File "C:\Users\david\Desktop\courserecsVDBs\venv\lib\site-packages\elasticsearch\_sync\client\_base.py", line 271, in perform_request
    response = self._perform_request(
    File "C:\Users\david\Desktop\courserecsVDBs\venv\lib\site-packages\elasticsearch\_sync\client\_base.py", line 351, in _perform_request
    raise HTTP_EXCEPTIONS.get(meta.status, ApiError)(
    elasticsearch.BadRequestError: BadRequestError(400, 'mapper_parsing_exception', 'Failed to parse mapping: dynamic template [all_text_fields] has invalid content [{"match_mapping_type":"string","mapping":{"analyzer":"iq_text_base","fields":{"delimiter":{"analyzer":"iq_text_delimiter","type":"text","index_options":"freqs"},"joined":{"search_analyzer":"q_text_bigram","analyzer":"i_text_bigram","type":"text","index_options":"freqs"},"prefix":{"search_analyzer":"q_prefix","analyzer":"i_prefix","type":"text","index_options":"docs"},"enum":{"ignore_above":elasticsearch.BadRequestError: BadRequestError(400, 'mapper_parsing_exception', 'Failed to parse mapping: dynamic template [all_text_fields] has invalid content [{"match_mapping_type":"string","mapping":{"analyzer":"iq_text_base","fields":{"delimiter":{"analyzer":"iq_text_delimiter","type":"text","index_options":"freqs"},"joined":{"search_analyzer":"q_text_bigram","analyzer":"i_text_bigram","type":"text","index_options":"freqs"},"prefix":{"search_analyzer":"q_prefix","analyzer":"i_prefix","type":"text","index_options":"docs"},"enum":{"ignore_above":string","mapping":{"analyzer":"iq_text_base","fields":{"delimiter":{"analyzer":"iq_text_delimiter","type":"text","index_options":"freqs"},"joined":{"search_analyzer":"q_text_bigram","analyzer":"i_text_bigram","type":"text","index_options":"freqs"},"prefix":{"search_analyzer":"q_prefix","analyzer":"i_prefix","type":"text","index_options":"docs"},"enum":{"ignore_above":alyzer":"i_text_bigram","type":"text","index_options":"freqs"},"prefix":{"search_analyzer":"q_prefix","analyzer":"i_prefix","type":"text","index_options":"docs"},"enum":{"ignore_above":2048,"type":"keyword"},"stem":{"analyzer":"iq_text_stem","type":"text"}}}}], attempted to validate it with the following match_mapping_type: [string]')


    It is important to notice that:

    1. I have copied and pasted the mapping, from an already existing, functional ES data store, with the ultimate aim to reindex the contents of that data store, into my new data store dest_index (see screen capture after this paragraph)
    2. I can connect succesfully to the client, as I have other scripts involving it; however this is the very first time I attempt to create an ES index from a Python script.
    3. The ES version I am using (both in the host and the Python package) is 8.18.1.

    [​IMG]

    What am I doing wrong?

    Continue reading...

Compartilhe esta Página