Cannot change similarity with put_mapping: conflict with existing mapping

Hello,

I recently set up an elastic search cluster. I have created an index with some text field. I managed to add a new text field using the put_mapping API.

But I cannot update the related similarity. I get the following error message, regarding of the filed I try to work on:
RequestError(400, 'illegal_argument_exception', 'Mapper for [test] conflicts with existing mapping:\n[mapper [test] has different [similarity]]')

I use elasticsearch 7.4.1and elasticsearch-py 7.0.5 (but I get the same error if I do it with a curl -XPUT).

Here is my index structure after creation

and after adding a test field and defining a similarity that I want to use

I did the following code to insert the field and define the similarity:

request_test = {
    "properties": 
    {"test": {'type': 'text'}
    }
}

request_settings = {
  "settings" : {
        "index" : {
            "similarity" : {
              "my_similarity" : {
                "type" : "DFI",
                "independence_measure" : "saturated"
              }
            }
        }
    }
}

es.indices.close('test_index2')
es.indices.put_settings(request_settings,'test_index2')
es.indices.put_mapping(body=request_test,index='test_index2')
es.indices.open('test_index2')

So this works just fine. But when I try to force my_similarity for a field (any of them), I get the error, even though "similarity" is supposes to be a mapping parameter (https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-params.html)

Here is the code I use:

request_mapping = {
    "properties": {'test': {'type': 'text', 'similarity': 'my_similarity'}}
} 
es.indices.close('test_index2')
es.indices.put_mapping(body=request_mapping,index='test_index2')
es.indices.open('test_index2')

(and it is actually the same if I use BM25)

I get the following error:

RequestError(400, 'illegal_argument_exception', 'Mapper for [test] conflicts with existing mapping:\n[mapper [test] has different [similarity]]')

And I get the same error if I use the following command in a term:

curl -H "Content-Type: application/json" -XPUT http://127.0.0.1:9200/test_index2/_mapping -d '{"propertie...'

So there is obviously something that I don't understand, since it is said that is it possible to update the similarity, but I get an error saying it can't…

Many thanks for all the help you can provide.

Hello all,

Interesting thing that I just tested:

If I create my test field with

request_test = {
    "properties": 
    {"test": {'type': 'text', 'similarity': 'my_similarity'}
    }
}

it just works fine (at least there is no conflict). Which leads me to believe that similarity cannot be changed once fixed on a field in an index, thus requiring a new index/reindexing.

Am I correct ?

Many thanks

I also tried by reindexing (creating new index and explicitly set the similarity to what I want in some fields and then importing data with reindex API), and it obviously worked.

So I just need a confirmation that we can't change a similarity once the field is created.

Thanks

Hello everyone, no one for this issue?

Thanks

Hello,

You are correct. You can only configure the similarity for a field when creating the field.

Hello,

Many thanks for your answer. I managed to change it by reindexing, but also I figured out how to change the default similarity on all fields of an existing index. Wich is fine for me.

I had some trooble when I wanted to change multiple times the similarity: in the put_settings request, I had to set to "None" (as coding in Python) the options of the previous similarity in order to erase them, otherwise it would'nt change and return an error code (saying the previous options where irregular for the new similarity).

Regards

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.