4

I am using spring-data-elasticsearch for my application. I found spring automatically creates dynamic field "_class" in all my documents. I configured my index as a strict dynmaic mapping. I expecting some properties to exclude this field from my document/mapping dynamically.

Is there any way to do this?

Achaius
  • 5,904
  • 21
  • 65
  • 122

2 Answers2

2

Currently this can not be deactivated. There is an open issue to add this behaviour.

But from the next version on (4.2.0.M5) the _class property will be defined in the index mapping when the index and its mapping are created by Spring Data Elasticsearch issue #1711.

P.J.Meisch
  • 18,013
  • 6
  • 50
  • 66
  • Looking forward for the next release – Achaius Mar 12 '21 at 14:07
  • 1
    But is this "_class" field is really needed? – Achaius Mar 12 '21 at 14:08
  • Let's say you have a class `Entity` with `@Document` and properties. If you only were saving `Entity` instances it would not be needed. But now define a derived class `SubEntity` which might not even add properties but behaviour. You can pass an instance of this subclass to the `ElasticsearchOperations.save(Entity)` method and it will be happily be saved. But when reading it back, you want to have a `SubEntity` and not an `Entity`. And that's where this information is needed. The same applies to properties which might be declared to have a type but the actual instances can be some subtype. – P.J.Meisch Mar 12 '21 at 14:32
0

Disable writeTypeHint in the mapping pojo.

@Document(indexName = "index", writeTypeHint = WriteTypeHint.FALSE)
public class Test{}

Spring document reference

https://docs.spring.io/spring-data/elasticsearch/docs/4.3.0-M1/reference/html/#elasticsearch.mapping.meta-model.rules

bpetlur
  • 333
  • 5
  • 16