Hamed Sanaei
Hamed Sanaei

Reputation: 131

Elasticsearch problem with pre-defined mapping and then index docs

I am trynig to index stackoverflow data. First of all I create an index with specified mapping and setting.

    @classmethod
    def create_index_with_set_map(cls, name, elasticsearch):
        """
        create index with default mappings and settings(and analyzer).

    Argument:
    name -- The name of the index.
    elasticsearch -- Elasticsearch instance for connection.
        """
     
        mappings = "mappings": {
            "properties": {
                "Body": {
                    "type": "text",
                    "analyzer": "whitespace",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }}}
       
        settings = {
            "analysis": {
                "analyzer": {
                    "default": {
                        "type": "whitespace"
                    }
                }
            }
        }

        body = {
            "settings": settings,
            "mappings": mappings

        }
        res = elasticsearch.indices.create(index=name, body=body)
        print(res)

Then I try to bulk index my docs:

@classmethod
    def start_index(cls, index_name, index_path, elasticsearch, doc_type):
        """
    This function is using bulk index.

    Argument:
    index_name -- the name of index
    index_path -- the path of xml file to index
    elasticsearch -- Elasticsearch instance for connection
    doc_type -- doc type 

    Returns:
    """

        for lines in Parser.xml_reader(index_path):
            actions = [
                {
                    "_index": index_name,
                    "_type": doc_type,
                    "_id": Parser.post_parser(line)['Id'],
                    "_source":  Parser.post_parser(line)


                }
                for line in lines if Parser.post_parser(line) is not None
            ]

            helpers.bulk(elasticsearch, actions)

Given Error: ('500 document(s) failed to index.', [{'index': {'_index': 'sof-question-answer2', '_type': 'Stackoverflow', '_id': 1', 'status': 400, 'error': {'type': 'illegal_argument_exception', 'reason': 'Mapper for [Body] conflicts with existing mapping:\n[mapper [Body] has different [analyzer]]'}, 'data': ...}

Upvotes: 0

Views: 333

Answers (1)

Assael Azran
Assael Azran

Reputation: 2993

It looks like sof-question-answer2 index has already been created with a different analyzer, probably with the default one standard analyzer.

If you run the command GET sof-question-answer2/_mapping via kibana you will see that Body field doesn't have the whitespace analyzer.

I order to resolve this issue you will have to delete your index, update your mapping, and reindexing your data (if you have any).

Upvotes: 1

Related Questions