1

I'm using elasticsearch java-api in combination with spring-data and have a problem with indexing a document. I want a different name for an indexed field. That means not the same as in the java code:

Domainobject:

@Document(indexName = "testindex", type = "message")
public class MessageObject {

@Id
private String  unid;

@FieldNameInElasticIndex(value = "javaMessage")  // I want anything like that
private String message;

private String secondMessage;

private String thirdMessage;

...

getters & setters

...

}

Interface:

public interface MessageObjectRepository extends ElasticsearchRepository<MessageObject, Long> {


}

Service:

@Component
public class MessageService {

    @Autowired
    private MessageObjectRepository repository;


    public void addRegistrationObject(MessageObject msg) {

        repository.save(msg);
    }
}

So....

is it possible to change the name so the index would look like:

"hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "testindex",
        "_type": "message",
        "_id": "00113B325ED357B7C1257E2D001D5B4B",
        "_score": 1,
        "_source": {
          "unid": "00113B325ED357B7C1257E2D001D5B4B",
          "javaMessage": "Hello",  // <--- this is what I want (javaMessage)
          "secondMessage": null,
          "thirdMessage": "Third",

instead of

"hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "testindex",
        "_type": "message",
        "_id": "00113B325ED357B7C1257E2D001D5B4B",
        "_score": 1,
        "_source": {
          "unid": "00113B325ED357B7C1257E2D001D5B4B",
          "message": "Hello",  // <--- this is NOT what I want (java name: message)
          "secondMessage": null,
          "thirdMessage": "Third",

?

2
  • Have you tried @JsonProperty("javaMessage") ? Commented Jan 5, 2016 at 17:56
  • It works, thank you :) Commented Jan 6, 2016 at 14:41

1 Answer 1

2

The idea is simply to use the @JsonProperty annotation on the field and give the name you want to use during the JSON serialization:

@JsonProperty("javaMessage")
private String message;
Sign up to request clarification or add additional context in comments.

1 Comment

There is not a single mention of @JsonProperty in docs.spring.io/spring-data/elasticsearch/docs/current/reference/… ! How can this be overlooked in the documentation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.