Elasticsearch: Cannot update parameter [analyzer] from [default] to [autocomplete]

So folks, I started my studies with elasticsearch and started trying to build an autocomplete with edge - ngram tokenizer on my machine and found a problem when trying to follow the documentation for such a configuration.

I followed the following steps:

  1. I created my index with a standard configuration as the documentation requested:

     {
        "settings": {
           "analysis": {
              "analyzer": {
                 "autocomplete": {
                    "tokenizer": "autocomplete",
                      "filter": [
                        "lowercase"
                      ]
                  },
                  "autocomplete_search": {
                     "tokenizer": "lowercase"
                  }
               },
               "tokenizer": {
                  "autocomplete": {
                     "type": "edge_ngram",
                     "min_gram": 2,
                     "max_gram": 10,
                     "token_chars": [
                        "letter"
                     ]
                  }
               }
            }
         }
     }
    
  2. I added an item to the index via bulk:

     {"index":{"_index":"products","_type":"products"}}{"sku":"73792589","name":"Bruno's test","img":"73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg","avaliacao":0,"preco_de":159,"preco_por":143.1}
    
  3. I tried to update the field mapping to reference my custom analyzer

Problem: When trying to proceed with step 3, the following error is returned to me:

[REQUEST] _mapping
{
  "properties": {
    "name": {
      "type": "text",
      "analyzer": "autocomplete",
      "search_analyzer": "autocomplete_search",
      "fields": {
        "keyword": {
            "type": "keyword",
            "ignore_above": 256
        }  
      }
    },
    "sku": {
      "type": "text",
      "analyzer": "autocomplete",
      "search_analyzer": "autocomplete_search",
      "fields": {
        "keyword": {
            "type": "keyword",
            "ignore_above": 256
        }  
      }
    }
  }
}

[RESPONSE]
{
  "error": {
    "root_cause": [
        {
            "type": "illegal_argument_exception",
            "reason": "Mapper for [name] conflicts with existing mapper:\n\tCannot update parameter [analyzer] from [default] to [autocomplete]"
        }
    ],
    "type": "illegal_argument_exception",
    "reason": "Mapper for [name] conflicts with existing mapper:\n\tCannot update parameter [analyzer] from [default] to [autocomplete]"
  },
  "status": 400
}

Upvotes: 3

Views: 4668

Answers (1)

Bhavya
Bhavya

Reputation: 16172

After adding the settings for the products index, you have indexed the data without any explicit mapping defined. So, the dynamic mapping is generated.

The mapping of your index is

{
  "products": {
    "mappings": {
      "properties": {
        "avaliacao": {
          "type": "long"
        },
        "img": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "name": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "preco_de": {
          "type": "long"
        },
        "preco_por": {
          "type": "float"
        },
        "sku": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }
  }
}

Now you are trying to update the mapping of the index (in the 3rd step). You want to add the analyzer parameter along with the fields, but this cannot be done. This is because when updating the index mapping you can -

  1. Add new properties to an existing object field
  2. Add multi-fields to an existing field
  3. Change the mapping of an existing field
  4. Rename a field

To know more about the update mapping API, refer to this documentation


(You need to first delete the products index) Follow the below steps to index the bulk data (with proper index mapping)

1. First Step: Index Mapping

{
  "settings": {
    "analysis": {
      "analyzer": {
        "autocomplete": {
          "tokenizer": "autocomplete",
          "filter": [
            "lowercase"
          ]
        },
        "autocomplete_search": {
          "tokenizer": "lowercase"
        }
      },
      "tokenizer": {
        "autocomplete": {
          "type": "edge_ngram",
          "min_gram": 2,
          "max_gram": 10,
          "token_chars": [
            "letter"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "analyzer": "autocomplete",
        "search_analyzer": "autocomplete_search",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "sku": {
        "type": "text",
        "analyzer": "autocomplete",
        "search_analyzer": "autocomplete_search",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

Second Step: Index Bulk data

POST/ _bulk

{"index":{"_index":"products","_type":"products"}}
{"sku":"73792589","name":"Bruno's test","img":"73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg","avaliacao":0,"preco_de":159,"preco_por":143.1}

Upvotes: 3

Related Questions