0

I am trying to extend ElasticsearchRepository in my project but I am unable to due to the following error

Caused by: java.lang.IllegalStateException: No suitable constructor found on interface com.example.elasticSearchDemo.Repository.ESDemoRepository to match the given arguments: [class org.springframework.data.elasticsearch.repository.support.MappingElasticsearchEntityInformation, class org.springframework.data.elasticsearch.core.ElasticsearchTemplate]. Make sure you implement a constructor taking these

//My interface that extends ElasticSearchRepository:

public interface ESDemoRepository extends ElasticsearchRepository<Data, String>{
    Page<Data> findByEsblog_flow_name(String flow_name, Pageable pageable);

    List<Data> findByEsblog_type(String type);
}

//Data class
@Document(indexName = "logdata", type="doc")
public class Data {
 @Id
 private String id;
 private String esblog_time;
 private String esblog_appl_name;
 private String esblog_host_name;
 private String esblog_iib_name;
 private String esblog_flow_name;
 private String esblog_payload;
 private String esblog_type;
 private Integer esblog_retention;
 private String esblog_tansaction_id;

@Override
public String toString() {
    return "Data{" +
            "id='" + id + '\'' +
            ", time='" + esblog_time + '\'' +
            ", payload='" + esblog_payload + '\'' +
            ", type='" + esblog_type + '\'' +
            ", retention='" + esblog_retention + '\'' +
            '}';
}

}

@Configuration
@EnableElasticsearchRepositories(repositoryBaseClass = 
ESDemoRepository.class,basePackages="com.example.elasticSearchDemo")
public class ESConfiguration {
@Value("${elasticsearch.host}")
private String EsHost;

@Value("${elasticsearch.port}")
private int EsPort;

@Value("${elasticsearch.clustername}")
private String EsClusterName;


@Bean
public Client client() throws Exception {

    Settings settings = Settings.builder()
            .put("cluster.name", EsClusterName).build();
 return  new PreBuiltTransportClient(settings)
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(EsHost), EsPort));
}

@Bean
public ElasticsearchOperations elasticsearchTemplate() throws Exception {
    return new ElasticsearchTemplate(client());
}

}

@Service
public class DemoServiceImpl implements DemoService {



private ESDemoRepository demoRepository;
@Autowired
public void setDemoRepository(ESDemoRepository demoRepository){this.demoRepository=demoRepository;}

private ESDemoDAO esDemoDAO;
@Autowired
public void setEsDemoDAO(ESDemoDAO demoDAO){this.esDemoDAO=demoDAO;}

public String save(Data data) throws Exception {
    return esDemoDAO.esQueryDemo(data);
}

public void delete(Data data) {
    demoRepository.delete(data);
}

public Data findOne(String id) throws Exception {
    return esDemoDAO.esGetQuery(id);
}

public Iterable<Data> findAll() {
    return demoRepository.findAll();
}


public List<Data> findByType(String type) {
    return demoRepository.findByEsblog_type(type);
}
4
  • Put you Data class and pom.xml please Commented Jun 28, 2018 at 9:36
  • I am using Gradle build. Added Data Class Commented Jun 28, 2018 at 9:38
  • Do you have getters and setters and constructor in Data class ? If not try to add them and check Commented Jun 28, 2018 at 9:39
  • I do have getters and setters and a constructor in the Data class. I need to use this interface as a bean in my service class , but the bean cannot be created due to the error mentioned Commented Jun 28, 2018 at 9:42

1 Answer 1

1

In your configuration class:

@Configuration
@EnableElasticsearchRepositories(repositoryBaseClass = 
ESDemoRepository.class,basePackages="com.example.elasticSearchDemo")
public class ESConfiguration {
@Value("${elasticsearch.host}")
private String EsHost;

@Value("${elasticsearch.port}")
private int EsPort;

@Value("${elasticsearch.clustername}")
private String EsClusterName;


@Bean
public Client client() throws Exception {

    Settings settings = Settings.builder()
            .put("cluster.name", EsClusterName).build();
 return  new PreBuiltTransportClient(settings)
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(EsHost), EsPort));
}

@Bean
public ElasticsearchOperations elasticsearchTemplate() throws Exception {
    return new ElasticsearchTemplate(client());
}

Replace:

 @EnableElasticsearchRepositories(repositoryBaseClass = 
    ESDemoRepository.class,basePackages="com.example.elasticSearchDemo")

With this:

 @EnableElasticsearchRepositories(basePackages="com.example.elasticSearchDemo")

EDIT

After this changes there is exception :

error:Caused by: org.springframework.data.mapping.PropertyReferenceException: No property esblog found for type Data!

It's because of bad name convention, because the underscore _ is a reserved character in Spring Data. According to:

https://stackoverflow.com/a/23475349/6003541

There are two solutions:

  1. Use camel case instead of using underscore.
  2. Use double underscore in repository class
Sign up to request clarification or add additional context in comments.

2 Comments

Now i am getting a different error:Caused by: org.springframework.data.mapping.PropertyReferenceException: No property esblog found for type Data!
I think your name convension is bad. Try to use camelCase because I'm not sure but Spring probably doesn't recognize your attributes names with _

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.