-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Use a customized typehint attribute name #2029
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is not possible at the moment. What would be the use case to have a customized name for this field name, which is a field internally by Spring Data Elasticsearch ? |
i am working on a search only project which need to read a big index stores more than 10 years data. we already have a filed play similar document type role. it is really a overkill for me to re-index all old documents to add the new filed. either a customized filed name or a callback hook could create sub-classes both works for my case, but neither of them are available in current release. |
So you have an index that contains documents that correspond to different entity classes in your application, and you have a field in your index that contains the full qualified name of the respective class? Do this classes have a common base class that you will use in Spring Data Elasticsearch as entity class when reading the data? One thing to solve this would be to allow the configuration of the type hint field name as you suggest. A better way would be to have a lifecycle callback that is invoked after the data has been read from Elasticsearch and before it is mapped into an entity. In this callback you could add a There is an open issue to add that callback (#2009). |
@sothawo, yes we have a base entity class. i am using mapstruct with customized converter function to do this right now. but i think it will be great to have the native feature in this library. |
I think this should be solved with a callback that invoked just before the conversion; this is in #2009. If there is no feedback on that ticket in the next 7 days, I'll gonna implement that. |
PR #2039 that closed #2009 now adds the possiblity to use an I have tried this in the following setup: An abstract base class: @Document(indexName = "type-hints", writeTypeHint = WriteTypeHint.FALSE)
public abstract class BaseClass {
@Id
private String id;
@Field(type = FieldType.Keyword, name = "my-class-name-field")
private String myClassNameField;
public BaseClass() {
this.myClassNameField = getClass().getName();
}
// getter+setter ...
} I set the After saving two entities I have the following in my index:
{
"_shards": {
"failed": 0,
"skipped": 0,
"successful": 1,
"total": 1
},
"hits": {
"hits": [
{
"_id": "one",
"_index": "type-hints",
"_score": 1.0,
"_source": {
"base-text": "baseOne",
"derived-one": "derivedOne",
"id": "one",
"my-class-name-field": "com.sothawo.springdataelastictest.typehints.DerivedOne"
},
"_type": "_doc"
},
{
"_id": "two",
"_index": "type-hints",
"_score": 1.0,
"_source": {
"base-text": "baseTwo",
"derived-two": "derivedTwo",
"id": "two",
"my-class-name-field": "com.sothawo.springdataelastictest.typehints.DerivedTwo"
},
"_type": "_doc"
}
],
"max_score": 1.0,
"total": {
"relation": "eq",
"value": 2
}
},
"timed_out": false,
"took": 21
} So the class informaiton is stored in a custom field. To be able to read this in I have defined the following @Configuration
public class TypeHintConfiguration {
@Component
static class TypeHintAfterLoadCallback implements AfterLoadCallback<BaseClass> {
@Override
public Document onAfterLoad(Document document, Class<BaseClass> type, IndexCoordinates indexCoordinates) {
document.put(ElasticsearchTypeMapper.DEFAULT_TYPE_KEY, document.get("my-class-name-field"));
return document;
}
}
} This sets the value of the Note: You may have noticed that I am using the value
|
is there any way to customize the default type hit attributes filed?
The text was updated successfully, but these errors were encountered: