Consider defining a bean named 'elasticsearchTemplate' in your configuration

不打扰是莪最后的温柔 提交于 2021-01-27 05:20:11

问题


I have just started springboot and tried to implement elastic search with spring-boot but I am getting this type of error while running spring-boot app

Consider defining a bean named 'elasticsearchTemplate' in your configuration.

POM.XML

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>4.0.0</version>
        </dependency>
         <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.6.10</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        
        
    </dependencies>

Repository

@Repository
public interface StudentRepository extends ElasticsearchRepository<Student, Integer>{}

Controller

@RestController
public class Controller {
    
    @Autowired
    StudentRepository studentRepo;
    
    @GetMapping(value="/student/all")
    List<Student> getAllStudent() {
        
        Iterator<Student> studentList = studentRepo.findAll().iterator();
        List<Student> students = new ArrayList<>();
        if(studentList.hasNext()) {
            students.add(studentList.next());
        }
        return students;
    }
    
    @PostMapping(value="/student/add")
    String addStudent(@RequestBody Student student) {
        
        studentRepo.save(student);
        return "Record Added Successfully";
    } 
    
    @DeleteMapping(value="/student/delete/{id}")
    String deleteStudent(@PathVariable int id) {
        
        studentRepo.deleteById(id);
        return "Record Deleted Successfully";
    }
    
    //@GetMapping(value="/student/findById/{id}")
    
}

Can Anyone help me to resolve this error

Consider defining a bean named 'elasticsearchTemplate' in your configuration.


回答1:


You need to define some elastic search properties in your application.properties file such as cluster-nodes, cluster-names which are used by ElasticsearchTemplate and ElasticsearchRepository to connect to the Elasticsearch engine.

You can refer below mentioned link :

https://dzone.com/articles/elasticsearch-with-spring-boot-application




回答2:


Note: Please refer to the spring-data-elasticsearch-versions or Spring Data Elasticsearch Changelog (check Elasticsearch version of desired release) to check version compatibility.

Solution(1):


If you want to use spring boot 1.x, simply create a @Configuration class and add a ElasticsearchOperations Bean.
Please note than spring boot 1.x does not support the latest versions of ElasticSearch 5.x and higher.
cluster.name: make sure the cluster name you set in the code is the same as the cluster.name you set in $ES_HOME/config/elasticsearch.yml
@Configuration
public class ElasticSearchConfig {

    @Bean
    public ElasticsearchOperations elasticsearchTemplate() throws UnknownHostException {
        return new ElasticsearchTemplate(getClient());
    }

    @Bean
    public Client getClient() throws UnknownHostException {
        Settings setting = Settings
            .builder()
            .put("client.transport.sniff", true)
            .put("path.home", "/usr/share/elasticsearch") //elasticsearch home path
            .put("cluster.name", "elasticsearch")
            .build();
        //please note that client port here is 9300 not 9200! 
        TransportClient client = new PreBuiltTransportClient(setting)
            .addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); 
        return client;
    }
}

Solution (2):


Also, you can refer to this spring boot issue that shows automatic configuration of the Elasticsearch in the spring data from spring boot 2.2.0.
Therefore, using spring boot 2.2 and spring-boot-starter-elasticserach you don't need to configure the Elasticsearch manually.

Sample working project:
Versions:
spring boot : 2.2.0.RELEASE
Elasticsearch: 6.6.2

Pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>spring-boot-elasticsearch</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

application.properties:

spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=localhost:9300
spring.elasticsearch.jest.uris=http://localhost:9200

Main Application class:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Model class:

import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

@Document(indexName = "your_index", type = "books")
public class Book {
    @Id
    private String id;
    private String title;
    private String author;
    private String releaseDate;
    //getter, setter/constructors
}

Repository class:

import com.example.model.Book;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public interface BookRepository extends ElasticsearchRepository<Book, String> {

    Page<Book> findByAuthor(String author, Pageable pageable);
    List<Book> findByTitle(String title);
}

Service class: some methods to test:

import com.example.model.Book;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;

public interface BookService {
    Book save(Book book);
    void delete(Book book);
    Book findOne(String id);
    Iterable<Book> findAll();
    Page<Book> findByAuthor(String author, Pageable pageable);
    List<Book> findByTitle(String title);
}

Service implementation:

import com.example.model.Book;
import com.example.repository.BookRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    private BookRepository bookRepository;

    public BookServiceImpl(BookRepository bookRepository) {
        this.bookRepository = bookRepository;
    }

    @Override
    public Book save(Book book) {
        return bookRepository.save(book);
    }

    @Override
    public void delete(Book book) {
        bookRepository.delete(book);
    }

    @Override
    public Book findOne(String id) {
        return bookRepository.findById(id).orElse(null);
    }

    @Override
    public Iterable<Book> findAll() {
        return bookRepository.findAll();
    }

    @Override
    public Page<Book> findByAuthor(String author, Pageable pageable) {
        return bookRepository.findByAuthor(author, pageable);
    }

    @Override
    public List<Book> findByTitle(String title) {
        return bookRepository.findByTitle(title);
    }
}

Test class:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class BookTest {

    @Autowired
    private BookService bookService;

    @Autowired
    private ElasticsearchTemplate esTemplate;

    @Before
    public void before(){
        esTemplate.deleteIndex(Book.class);
        esTemplate.createIndex(Book.class);
        esTemplate.putMapping(Book.class);
        esTemplate.refresh(Book.class);
    }

    @Test
    public void testSave(){
        Book book = new Book("1001", "Elasticsearch", "title", "23-FEB-2017");
        Book testBook = bookService.save(book);
        assertNotNull(testBook.getId());
        assertEquals(testBook.getTitle(), book.getTitle());
        assertEquals(testBook.getAuthor(), book.getAuthor());
        assertEquals(testBook.getReleaseDate(), book.getReleaseDate());
    }
}


来源:https://stackoverflow.com/questions/56489593/consider-defining-a-bean-named-elasticsearchtemplate-in-your-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!