简体   繁体   中英

Can't update mapping in elasticsearch

When putting an anaylzer into mapping using PUT /job/_mapping/doc/ but get conflicts. But there isn't a anaylzer in mappings.

PUT /job/_mapping/doc/
{
    "properties":{
        "title": {
            "type": "text",
            "analyzer":"ik_smart",
            "search_analyzer":"ik_smart"
        }
    }
}
{
    "error": {
        "root_cause": [
            {
                "type": "illegal_argument_exception",
                "reason": "Mapper for [title] conflicts with existing mapping in other types:\n[mapper [title] has different [analyzer]]"
            }
        ],
        "type": "illegal_argument_exception",
        "reason": "Mapper for [title] conflicts with existing mapping in other types:\n[mapper [title] has different [analyzer]]"
    },
    "status": 400
}
                    "title": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        },
                        "fielddata": true
                    },

The output config is like this.

output {
  elasticsearch {
    hosts => ["<Elasticsearch Hosts>"]
    user => "<user>"
    password => "<password>"
    index => "<table>"
    document_id => "%{<MySQL_PRIMARY_KEY>}"
  }
}

You cant update mapping in elasticsearch , you can add mapping but not update mapping. Elasticsearch use mapping at the indexation time, that s why you cant update mapping of an existing field. Analyzer is part of the mapping, in fact if you don't specify one es a default one, analyzer tell elastic how to index the documents.

  1. create a new index with your new mappings (include analyzer)
  2. reindex your documents from your existing index to the new one ( https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html )

Updating Mapping:

Once a document is indexed to an index ie the mapping is generated under a given type as like in our case Mapping of EmployeeCode , EmployeeName & isDevelopers ' is generated under type " customtype ", we cannot modify it afterwards. In case if we want to modify it, we need to delete the index first and then apply the modified mapping manually and then re-index the data. But If you want to add an a new property under a given type, then it is feasible. For example, our document attached our index "inkashyap-1002" under type "customtype" is as follows:

{
  "inkashyap-1002": {
    "mappings": {
      "customtype": {
        "properties": {
          "EmployeeCode": {
            "type": "long"
          },
          "isDeveloper": {
            "type": "boolean"
          },
          "EmployeeName": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

now let's add another property "Grade" :

curl -XPUT localhost:9200/inkashyap-1002(IndexName)/customtype(TypeName)/2 — d '{
 "EmployeeName": "Vaibhav Kashyap",
 "EmployeeCode": 13629,
 "isDeveloper": true,
 "Grade": 5
}'

Now hit the GET mapping API. In the results, you can see there is another field added called "Grade".

Common Error:

In the index "inkashyap-1002", so far we have indexed 2 documents. Both the documents had the same type for the field "EmployeeCode" and the type was "Long". Now let us try to index a document like below:

curl -XPUT localhost:9200/inkashyap-1002/customtype/3 -d '{
"EmployeeName": "Vaibhav Kashyap",
 "EmployeeCode": "onethreesixtwonine",
 "isDeveloper": true,
 "Grade": 5
}'

Note that here the "EmployeeCode" is given in string type, which indicates that it is a string field. The response to the above request will be like below:

{
  "error": {
    "root_cause": [
      {
        "type": "mapper_parsing_exception",
        "reason": "failedtoparse[
          EmployeeCode
        ]"
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "failedtoparse[
      EmployeeCode
    ]",
    "caused_by": {
      "type": "number_format_exception",
      "reason": "Forinputstring: \"onethreesixtwonine\""
    }
  },
  "status": 400
}

In the above response, we can see the error "mapper_parsing_exception" on the field "EmployeeCode". This indicates that the expected field here was of another type and not string. In such cases re-index the document with the appropriate type

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM