Partial update with Spring Data Elasticsearch repository

Instead of having both ElasticsearchTemplate and UserElasticsearchRepository injected into your UserServiceClass, you can implement your own custom repository and let your existing UserElasticsearchRepository extend it.

I assume that your existing UserElasticsearchRepository look something like this.

public interface UserElasticsearchRepository extends ElasticsearchRepository<User, String> {
   ....
}

You have to create new interface name UserElasticsearchRepositoryCustom. Inside this interface you can list your custom query method.

public interface UserElasticsearchRepositoryCustom {

    public void updateAddress(User user, String updatedAddress);

}

Then implement your UserElasticsearchRepositoryCustom by create a class called UserElasticsearchRepositoryImpl and implement your custom method inside with injected ElasticsearchTemplate

public class UserElasticsearchRepositoryImpl implements UserElasticsearchRepositoryCustom {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Override
    public void updateAddress(User user, String updatedAddress){
        IndexRequest indexRequest = new IndexRequest();
        indexRequest.source("address", updatedAddress);
        UpdateQuery updateQuery = new UpdateQueryBuilder().withId(user.getId()).withClass(User.class).withIndexRequest(indexRequest).build();
        elasticsearchTemplate.update(updateQuery);
    }
}

After that, just extends your UserElasticsearchRepository with UserElasticsearchRepositoryCustom so it should look like this.

public interface UserElasticsearchRepository extends ElasticsearchRepository<User, String>, UserElasticsearchRepositoryCustom {
   ....
}

Finally, you service code should look like this.

public class UserServiceClass {

  @Autowired
  private UserElasticsearchRepository userElasticsearchRepository;

  public void updateAddress(int id, String updatedAddress) {
    User user = userElasticsearchRepository.findOne(id);
    if (user.getUsername().equals("system")) {
      return;
    }
    userElasticsearchRepository.updateAddress(user,updatedAddress);
  } 
}

You can also move your user finding logic into the custom repository logic as well so that you can passing only user id and address in the method. Hope this is helpful.