1

How to understand a spring-doc? The example provided doesn't work.

I am unable to bind together the pieces to form a basic example of saving and querying elasticsearch using Spring-Data-Elastic search. Can someone, point to some resource which actually works?

All classes are in the package elasticsearch. Anyhow here is my code:

@SpringBootApplication
public class Application {

    @Autowired
    private MovieRepository repository;

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

    public void run(String... args) throws Exception {

        // save a couple of movies
        repository.save(new Movie("tt1979320", "Rush", 2013, Arrays.asList("Action", "Biography", "Drama")));
        repository.save(new Movie("tt0111161", "The Shawshank Redemption", 1994, Arrays.asList("Crime", "Drama")));


    }
}

Pojo

@Document(indexName = "moviedb", type = "movie")
public class Movie {

    @Id
    private String id;

    private String name;

    private Integer year;

    private List<String> genre;

    public Movie() {
    }

    public Movie(String id, String name, Integer year, List<String> genre) {
        super();
        this.id = id;
        this.name = name;
        this.year = year;
        this.genre = genre;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getYear() {
        return year;
    }

    public void setYear(Integer year) {
        this.year = year;
    }

    public List<String> getGenre() {
        return genre;
    }

    public void setGenre(List<String> genre) {
        this.genre = genre;
    }

    @Override
    public String toString() {
        return "Movie [id=" + id + ", name=" + name + ", year=" + year + ", genre=" + genre + "]";
    }

}

Spring Data repo for ElasticSearch

@Repository
public interface MovieRepository extends ElasticsearchRepository<Movie, String>{

    List<Movie> findByName(String name);
    List<Movie> findByYear(Integer year);
    List<Movie> findByGenre(List<String> genre);

}

Service class

@Service
public class MovieService{

    @Autowired
    private MovieRepository repository;

    public List<Movie> findByName(String name) {
        return repository.findByName(name);
    }

    public List<Movie> findByYear(Integer year) {
        return repository.findByYear(year);
    }

    public List<Movie> findByGenre(List<String> genre) {
        return repository.findByGenre(genre);
    }

}

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>elasticsearch</groupId>
    <artifactId>spring-data-guide</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>gs-accessing-data-elasticsearch</name>
    <description>Spring Data for Elasticsearch guide</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

Some examples have created an Elasticsearch template with this. I haven't quite understood why is the Elasticsearch template required and how to create one.

I used elasticsearch 1.7 with this and received the following error:

java.lang.NullPointerException: null
    at org.elasticsearch.transport.netty.MessageChannelHandler.handleException(MessageChannelHandler.java:179) ~[elasticsearch-2.4.4.jar:2.4.4]
    at org.elasticsearch.transport.netty.MessageChannelHandler.handlerResponseError(MessageChannelHandler.java:174) ~[elasticsearch-2.4.4.jar:2.4.4]
    at org.elasticsearch.transport.netty.MessageChannelHandler.messageReceived(MessageChannelHandler.java:122) ~[elasticsearch-2.4.4.jar:2.4.4]
    at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70) [netty-3.10.6.Final.jar:na]
    at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564) [netty-3.10.6.Final.jar:na]
    at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791) [netty-3.10.6.Final.jar:na]
    at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296) [netty-3.10.6.Final.jar:na]
    at org.jboss.netty.handler.codec.frame.FrameDecoder.unfoldAndFireMessageReceived(FrameDecoder.java:462) [netty-3.10.6.Final.jar:na]
    at org.jboss.netty.handler.codec.frame.FrameDecoder.callDecode(FrameDecoder.java:443) [netty-3.10.6.Final.jar:na]
    at org.jboss.netty.handler.codec.frame.FrameDecoder.messageReceived(FrameDecoder.java:303) [netty-3.10.6.Final.jar:na]
    at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70) [netty-3.10.6.Final.jar:na]
    at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564) [netty-3.10.6.Final.jar:na]
    at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:559) [netty-3.10.6.Final.jar:na]
    at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268) [netty-3.10.6.Final.jar:na]
    at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255) [netty-3.10.6.Final.jar:na]
    at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:88) [netty-3.10.6.Final.jar:na]
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:108) [netty-3.10.6.Final.jar:na]
    at org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:337) [netty-3.10.6.Final.jar:na]
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:89) [netty-3.10.6.Final.jar:na]
    at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:178) [netty-3.10.6.Final.jar:na]
    at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108) [netty-3.10.6.Final.jar:na]
    at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42) [netty-3.10.6.Final.jar:na]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_112]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_112]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_112]

Running with elasticsearch 2.3, I am getting the following error:

2017-02-17 17:52:00.147  WARN 16795 --- [           main] org.elasticsearch.client.transport       : [Astron] node {#transport#-1}{127.0.0.1}{127.0.0.1:9300} not part of the cluster Cluster [elasticsearch], ignoring...
2017-02-17 17:52:00.415 ERROR 16795 --- [           main] .d.e.r.s.AbstractElasticsearchRepository : failed to load elasticsearch nodes : org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}{127.0.0.1}{127.0.0.1:9300}]

1 Answer 1

2

To me it seems like the clusternames do not match. Try this parameters:

Properties in YAML Syntax:

### elastic properties
spring.data:
  elasticsearch:
    cluster-name: yourclustername
    cluster-nodes: localhost:9300
    repositories.enabled: true

youclustername has to match the clustername you gave in the elasticsearch.yml config on paramaeter "cluster.name:".

The cluster-nodes must match the "network.host" from elasticsearch.yml (+ the port which is default 9300)

Next you need to scan for the repositories like this:

@SpringBootApplication
@EnableElasticsearchRepositories(basePackages = "elasticsearch")
public class Application {
...
Sign up to request clarification or add additional context in comments.

4 Comments

I have put my elasticsearch properties in application.properties. How can I specify my clustername in application.properties?
Just concat the propertypaths like this: spring.data.elasticsearch.cluster-name=yourclustername
Your solution is successful and I am able to register index in elasticsearch. But I see no data or mapping on http://localhost:9200/moviedb/_mappin/movie I see the following data {"moviedb_test":{"mappings":{"movie":{}}}}
The mappings are created when the first data comes in. So You have to index stuff first. Alternatively you can specify the mappings yourself (@Mapping(mappingPath = "/mappings/movie.json") on document Level)

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.