精华内容
下载资源
问答
  • Spring Data Elasticsearch Elasticsearch 2.4.x 4.1.x 7.9.3 2.3.x 4.0.x 7.6.2 2.2.x 3.2.x 6.8.12 2.1.x 3.1.x 6.2.2 2.0.x 3.0.x 5.5.0 1.5.x 2.1.x 2.4.0 参考链接: springboot官网

    版本对应关系

    Spring BootSpring Data ElasticsearchElasticsearch
    2.4.x4.1.x7.9.3
    2.3.x4.0.x7.6.2
    2.2.x3.2.x6.8.12
    2.1.x3.1.x6.2.2
    2.0.x3.0.x5.5.0
    1.5.x2.1.x2.4.0

    参考链接: springboot官网

    展开全文
  • 红框这栏指的是:Elasticsearch安装版本(windows安装版本或Linux按照版本) 最新因为项目需要elasticsearch,采用今天搞了好久,项目启动就报如下错误: org.elasticsearch.transport.NodeDisconnectedException:...

    首先来看官网给的版本要求:

    红框这栏指的是:Elasticsearch安装版本(windows安装版本或Linux按照版本)

    最新因为项目需要elasticsearch,采用今天搞了好久,项目启动就报如下错误:

    org.elasticsearch.transport.NodeDisconnectedException: [][127.0.0.1:9300][cluster:monitor/nodes/liveness] disconnected

    找了好久以为是配置问题,最后终于明白原来是版本不统一。

    我的项目用的spring boot 1.5.x

    所以安装了elasticsearch-2.3.2.zip

    解压进入bin目录双击elasticsearch.bat。启动服务。

    在浏览器输入http://localhost:9200/,返回json字符串证明启动成功。

    ###################################################################

    在springboot中对elasticsearch配置是默认有支持的。

    如配置:

     

    #默认 9300 是 Java 客户端的端口。9200 是支持 Restful HTTP 的接口。
    #  集群节点地址列表,用逗号分隔。如果没有指定,就启动一个客户端节点
    spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
    spring.data.elasticsearch.cluster-name=elasticsearch
    spring.data.elasticsearch.repositories.enabled=true

    以前就绪,操作就类似于Spring-Data-JPA,具体见官方文档http://projects.spring.io/spring-data-elasticsearch/

    安装Head插件
    E:\soft\elasticsearch-2.3.2\bin>plugin install mobz/elasticsearch-head
    -> Installing mobz/elasticsearch-head...
    Trying https://github.com/mobz/elasticsearch-head/archive/master.zip ...
    ERROR: failed to download out of all possible locations..., use --verbose to get
     detailed information

    E:\soft\elasticsearch-2.3.2\bin>plugin install mobz/elasticsearch-head
    -> Installing mobz/elasticsearch-head...
    Trying https://github.com/mobz/elasticsearch-head/archive/master.zip ...
    Downloading ....................................................................
    ....................................终止批处理操作吗(Y/N)? y

    E:\soft\elasticsearch-2.3.2\bin>plugin install mobz/elasticsearch-head
    -> Installing mobz/elasticsearch-head...
    Trying https://github.com/mobz/elasticsearch-head/archive/master.zip ...
    Downloading ....................................................................
    ................................................................................
    ................................................................................
    ................................................................................
    ................................................................................
    ................................................................................
    ................................................................................
    ................................................................................
    ................................................DONE
    Verifying https://github.com/mobz/elasticsearch-head/archive/master.zip checksum
    s if available ...
    NOTE: Unable to verify checksum for downloaded plugin (unable to find .sha1 or .
    md5 file to verify)
    Installed head into E:\soft\elasticsearch-2.3.2\plugins\head

    以上完成启动es.

    访问http://localhost:9200/_plugin/head/

                   

    微信公众号               

    展开全文
  • 基于spring bootelastic search 例子 以及基础配置
  • Spring Boot + Spring Data + Elasticsearch实例

    万次阅读 2017-11-08 20:04:00
    在本文中,我们将讨论“如何创建Spring Boot + Spring Data + Elasticsearch范例”。本文中使用的工具:Spring Boot 1.5.1.RELEASESpring Boot Starter Data Elasticsearch 1.5.1.RELEASESpring Data Elasticsearch ...



    在本文中,我们将讨论“如何创建Spring Boot + Spring Data + Elasticsearch范例”。


    本文中使用的工具:

    1. Spring Boot 1.5.1.RELEASE

    2. Spring Boot Starter Data Elasticsearch 1.5.1.RELEASE

    3. Spring Data Elasticsearch 2.10.RELEASE

    4. Elasticsearch 2.4.4

    5. Maven

    6. Java 8


    注意
    SpringBoot 1.5.1.RELEASE和Spring Data Elasticsearch 2.10.RELEASE仅支持ElasticSearch 2.4.0。他们不支持最新版本的ElasticSearch 5.x版本。

    另附:目前spring boot借助spring data可以操作es。但是版本上有严格的要求。(此句非原文,为减少踩坑)


    1.项目结构

    一个标准的Maven项目结构。



    2.项目依赖

    为Spring Data ElasticSearch application配置spring-boot-starter-data-elasticsearch。


    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>
    
        <artifactId>springboot-springdata-elasticsearch-example</artifactId>
        <packaging>jar</packaging>
        <url>https://www.mkyong.com</url>
        <version>1.0</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.1.RELEASE</version>
        </parent>
    
        <properties>
            <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>
    
            <!-- Runtime, for Embedded Elasticsearch,
                comment this if connect to external elastic search server-->
            <dependency>
                <groupId>net.java.dev.jna</groupId>
                <artifactId>jna</artifactId>
                <scope>runtime</scope>
            </dependency>
    
        </dependencies>
    
        <build>
            <plugins>
                <!-- Package as an executable jar/war -->
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    检查项目依赖关系:

    $ mvn dependency:tree
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building springboot-springdata-elasticsearch-example 1.0
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ springboot-springdata-elasticsearch-example ---
    [INFO] org.springframework.boot:springboot-springdata-elasticsearch-example:jar:1.0
    [INFO] +- org.springframework.boot:spring-boot-starter-data-elasticsearch:jar:1.5.1.RELEASE:compile
    [INFO] |  +- org.springframework.boot:spring-boot-starter:jar:1.5.1.RELEASE:compile
    [INFO] |  |  +- org.springframework.boot:spring-boot:jar:1.5.1.RELEASE:compile
    [INFO] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:1.5.1.RELEASE:compile
    [INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:1.5.1.RELEASE:compile
    [INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.1.9:compile
    [INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.1.9:compile
    [INFO] |  |  |  +- org.slf4j:jul-to-slf4j:jar:1.7.22:compile
    [INFO] |  |  |  \- org.slf4j:log4j-over-slf4j:jar:1.7.22:compile
    [INFO] |  |  \- org.yaml:snakeyaml:jar:1.17:compile
    [INFO] |  \- org.springframework.data:spring-data-elasticsearch:jar:2.1.0.RELEASE:compile
    [INFO] |     +- org.springframework:spring-context:jar:4.3.6.RELEASE:compile
    [INFO] |     |  +- org.springframework:spring-aop:jar:4.3.6.RELEASE:compile
    [INFO] |     |  +- org.springframework:spring-beans:jar:4.3.6.RELEASE:compile
    [INFO] |     |  \- org.springframework:spring-expression:jar:4.3.6.RELEASE:compile
    [INFO] |     +- org.springframework:spring-tx:jar:4.3.6.RELEASE:compile
    [INFO] |     +- org.springframework.data:spring-data-commons:jar:1.13.0.RELEASE:compile
    [INFO] |     +- commons-lang:commons-lang:jar:2.6:compile
    [INFO] |     +- joda-time:joda-time:jar:2.9.7:compile
    [INFO] |     +- org.elasticsearch:elasticsearch:jar:2.4.4:compile
    [INFO] |     |  +- org.apache.lucene:lucene-core:jar:5.5.2:compile
    [INFO] |     |  +- org.apache.lucene:lucene-backward-codecs:jar:5.5.2:compile
    [INFO] |     |  +- org.apache.lucene:lucene-analyzers-common:jar:5.5.2:compile
    [INFO] |     |  +- org.apache.lucene:lucene-queries:jar:5.5.2:compile
    [INFO] |     |  +- org.apache.lucene:lucene-memory:jar:5.5.2:compile
    [INFO] |     |  +- org.apache.lucene:lucene-highlighter:jar:5.5.2:compile
    [INFO] |     |  +- org.apache.lucene:lucene-queryparser:jar:5.5.2:compile
    [INFO] |     |  |  \- org.apache.lucene:lucene-sandbox:jar:5.5.2:compile
    [INFO] |     |  +- org.apache.lucene:lucene-suggest:jar:5.5.2:compile
    [INFO] |     |  |  \- org.apache.lucene:lucene-misc:jar:5.5.2:compile
    [INFO] |     |  +- org.apache.lucene:lucene-join:jar:5.5.2:compile
    [INFO] |     |  |  \- org.apache.lucene:lucene-grouping:jar:5.5.2:compile
    [INFO] |     |  +- org.apache.lucene:lucene-spatial:jar:5.5.2:compile
    [INFO] |     |  |  +- org.apache.lucene:lucene-spatial3d:jar:5.5.2:compile
    [INFO] |     |  |  \- com.spatial4j:spatial4j:jar:0.5:compile
    [INFO] |     |  +- com.google.guava:guava:jar:18.0:compile
    [INFO] |     |  +- org.elasticsearch:securesm:jar:1.0:compile
    [INFO] |     |  +- com.carrotsearch:hppc:jar:0.7.1:compile
    [INFO] |     |  +- com.fasterxml.jackson.dataformat:jackson-dataformat-smile:jar:2.8.6:compile
    [INFO] |     |  +- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:jar:2.8.6:compile
    [INFO] |     |  +- com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:jar:2.8.6:compile
    [INFO] |     |  +- io.netty:netty:jar:3.10.6.Final:compile
    [INFO] |     |  +- com.ning:compress-lzf:jar:1.0.2:compile
    [INFO] |     |  +- com.tdunning:t-digest:jar:3.0:compile
    [INFO] |     |  +- org.hdrhistogram:HdrHistogram:jar:2.1.6:compile
    [INFO] |     |  +- commons-cli:commons-cli:jar:1.3.1:compile
    [INFO] |     |  \- com.twitter:jsr166e:jar:1.1.0:compile
    [INFO] |     +- com.fasterxml.jackson.core:jackson-core:jar:2.8.6:compile
    [INFO] |     +- com.fasterxml.jackson.core:jackson-databind:jar:2.8.6:compile
    [INFO] |     |  \- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile
    [INFO] |     +- org.slf4j:slf4j-api:jar:1.7.22:compile
    [INFO] |     \- org.slf4j:jcl-over-slf4j:jar:1.7.22:compile
    [INFO] +- org.springframework.boot:spring-boot-starter-test:jar:1.5.1.RELEASE:test
    [INFO] |  +- org.springframework.boot:spring-boot-test:jar:1.5.1.RELEASE:test
    [INFO] |  +- org.springframework.boot:spring-boot-test-autoconfigure:jar:1.5.1.RELEASE:test
    [INFO] |  +- com.jayway.jsonpath:json-path:jar:2.2.0:test
    [INFO] |  |  \- net.minidev:json-smart:jar:2.2.1:test
    [INFO] |  |     \- net.minidev:accessors-smart:jar:1.1:test
    [INFO] |  |        \- org.ow2.asm:asm:jar:5.0.3:test
    [INFO] |  +- junit:junit:jar:4.12:test
    [INFO] |  +- org.assertj:assertj-core:jar:2.6.0:test
    [INFO] |  +- org.mockito:mockito-core:jar:1.10.19:test
    [INFO] |  |  \- org.objenesis:objenesis:jar:2.1:test
    [INFO] |  +- org.hamcrest:hamcrest-core:jar:1.3:test
    [INFO] |  +- org.hamcrest:hamcrest-library:jar:1.3:test
    [INFO] |  +- org.skyscreamer:jsonassert:jar:1.4.0:test
    [INFO] |  |  \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
    [INFO] |  +- org.springframework:spring-core:jar:4.3.6.RELEASE:compile
    [INFO] |  \- org.springframework:spring-test:jar:4.3.6.RELEASE:test
    [INFO] \- net.java.dev.jna:jna:jar:4.2.2:runtime
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 1.867 s
    [INFO] Finished at: 2017-03-14T19:55:41+08:00
    [INFO] Final Memory: 27M/437M
    [INFO] ------------------------------------------------------------------------
    

    3. Spring Data ElasticSearch应用程序

    现在开始我们的Spring Boot + Spring Data + Elasticsearch实例:


    3.1,为我们的项目开发model类


    Book.java

    package com.mkyong.book.model;
    
    import org.springframework.data.annotation.Id;
    import org.springframework.data.elasticsearch.annotations.Document;
    
    @Document(indexName = "mkyong", type = "books")
    public class Book {
    
        @Id
        private String id;
        private String title;
        private String author;
        private String releaseDate;
    
        public Book() {
        }
    
        public Book(String id, String title, String author, String releaseDate) {
            this.id = id;
            this.title = title;
            this.author = author;
            this.releaseDate = releaseDate;
        }
    
        //getters and setters
    
        @Override
        public String toString() {
            return "Book{" +
                    "id='" + id + '\'' +
                    ", title='" + title + '\'' +
                    ", author='" + author + '\'' +
                    ", releaseDate='" + releaseDate + '\'' +
                    '}';
        }
    }


    3.2,为我们的项目开发Elasticsearch存储库


    BookRepository.java

    package com.mkyong.book.repository;
    
    import com.mkyong.book.model.Book;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.Pageable;
    import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
    
    import java.util.List;
    
    public interface BookRepository extends ElasticsearchRepository<Book, String> {
    
        Page<Book> findByAuthor(String author, Pageable pageable);
    
        List<Book> findByTitle(String title);
    
    }


    3.3,为我们的项目开发service层

    BookService.java

    package com.mkyong.book.service;
    
    import com.mkyong.book.model.Book;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.PageRequest;
    
    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, PageRequest pageRequest);
    
        List<Book> findByTitle(String title);
    
    }


    BookServiceImpl.java

    package com.mkyong.book.service;
    
    import com.mkyong.book.model.Book;
    import com.mkyong.book.repository.BookRepository;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.PageRequest;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service
    public class BookServiceImpl implements BookService {
    
        private BookRepository bookRepository;
    
      @Autowired
        public void setBookRepository(BookRepository bookRepository) {
            this.bookRepository = bookRepository;
        }
    
    
        public Book save(Book book) {
            return bookRepository.save(book);
        }
    
        public void delete(Book book) {
            bookRepository.delete(book);
        }
    
        public Book findOne(String id) {
            return bookRepository.findOne(id);
        }
    
        public Iterable<Book> findAll() {
            return bookRepository.findAll();
        }
    
        public Page<Book> findByAuthor(String author, PageRequest pageRequest) {
            return bookRepository.findByAuthor(author, pageRequest);
        }
    
        public List<Book> findByTitle(String title) {
            return bookRepository.findByTitle(title);
        }
    
    }

    3.4,在application.properties中配置Elasticsearch属性


    application.properties

    elasticsearch.clustername = mkyong-cluster
    elasticsearch.host = localhost
    elasticsearch.port = 9300
    
    #
    # Home directory of the embedded Elasticsearch instance. Default to the
    # current working directory.
    #
    #spring.data.elasticsearch.properties.path.home=target/elastic-embedded
    #spring.data.elasticsearch.properties.transport.tcp.connect_timeout=60s

    3.5,为SpringBoot配置。TransportClient连接ElasticSearch


    MkyongElasticsearchConfiguration.java

    package com.mkyong;
    
    import org.elasticsearch.client.Client;
    import org.elasticsearch.client.transport.TransportClient;
    import org.elasticsearch.common.settings.Settings;
    import org.elasticsearch.common.transport.InetSocketTransportAddress;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
    import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
    import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
    
    import java.net.InetAddress;
    
    @Configuration
    @EnableElasticsearchRepositories(basePackages = "com.mkyong.book.repository")
    public class EsConfig {
    
        @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 esSettings = Settings.settingsBuilder()
                    .put("cluster.name", EsClusterName)
                    .build();
    
            //https://www.elastic.co/guide/en/elasticsearch/guide/current/_transport_client_versus_node_client.html
            return TransportClient.builder()
                    .settings(esSettings)
                    .build()
                    .addTransportAddress(
              new InetSocketTransportAddress(InetAddress.getByName(EsHost), EsPort));
        }
    
        @Bean
        public ElasticsearchOperations elasticsearchTemplate() throws Exception {
            return new ElasticsearchTemplate(client());
        }
    
        //Embedded Elasticsearch Server
        /*@Bean
        public ElasticsearchOperations elasticsearchTemplate() {
            return new ElasticsearchTemplate(nodeBuilder().local(true).node().client());
        }*/
    
    }

    4.开发测试应用程序

    让我们开发一个测试应用程序来测试我们的代码。


    BookServiceTest.java

    package com.mkyong;
    
    import com.mkyong.book.model.Book;
    import com.mkyong.book.service.BookService;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.PageRequest;
    import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import static org.hamcrest.CoreMatchers.is;
    import static org.hamcrest.MatcherAssert.assertThat;
    import static org.junit.Assert.*;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = Application.class)
    public class BookServiceTest {
    
        @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 Basics", "Rambabu Posa", "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());
    
        }
    
        @Test
        public void testFindOne() {
    
            Book book = new Book("1001", "Elasticsearch Basics", "Rambabu Posa", "23-FEB-2017");
            bookService.save(book);
    
            Book testBook = bookService.findOne(book.getId());
    
            assertNotNull(testBook.getId());
            assertEquals(testBook.getTitle(), book.getTitle());
            assertEquals(testBook.getAuthor(), book.getAuthor());
            assertEquals(testBook.getReleaseDate(), book.getReleaseDate());
    
        }
    
        @Test
        public void testFindByTitle() {
    
            Book book = new Book("1001", "Elasticsearch Basics", "Rambabu Posa", "23-FEB-2017");
            bookService.save(book);
    
            List<Book> byTitle = bookService.findByTitle(book.getTitle());
            assertThat(byTitle.size(), is(1));
        }
    
        @Test
        public void testFindByAuthor() {
    
            List<Book> bookList = new ArrayList<>();
    
            bookList.add(new Book("1001", "Elasticsearch Basics", "Rambabu Posa", "23-FEB-2017"));
            bookList.add(new Book("1002", "Apache Lucene Basics", "Rambabu Posa", "13-MAR-2017"));
            bookList.add(new Book("1003", "Apache Solr Basics", "Rambabu Posa", "21-MAR-2017"));
            bookList.add(new Book("1007", "Spring Data + ElasticSearch", "Rambabu Posa", "01-APR-2017"));
            bookList.add(new Book("1008", "Spring Boot + MongoDB", "Mkyong", "25-FEB-2017"));
    
            for (Book book : bookList) {
                bookService.save(book);
            }
    
            Page<Book> byAuthor = bookService.findByAuthor("Rambabu Posa", new PageRequest(0, 10));
            assertThat(byAuthor.getTotalElements(), is(4L));
    
            Page<Book> byAuthor2 = bookService.findByAuthor("Mkyong", new PageRequest(0, 10));
            assertThat(byAuthor2.getTotalElements(), is(1L));
    
        }
    
        @Test
        public void testDelete() {
    
            Book book = new Book("1001", "Elasticsearch Basics", "Rambabu Posa", "23-FEB-2017");
            bookService.save(book);
            bookService.delete(book);
            Book testBook = bookService.findOne(book.getId());
            assertNull(testBook);
        }
    
    }

    5.运行Spring Boot Application

    5.1要运行这个演示,我们应该按照以下这些步骤:

            1、安装Java并设置JAVA_HOME和PATH变量。

            2、安装Maven。

            3、安装Elasticsearch 2.4.0
    这里做一个假设路径:ELASTICSEARCH_HOME = C:\ elasticsearch-2.4.0

            4、配置ElasticSearch
    打开$ {ELASTICSEARCH_HOME} \ config \ elasticsearch.yml并添加以下配置

    cluster.name: mkyong-cluster

           5、启动Elasticsearch实例


    5.2运行Spring Boot Application,它会将3个book对象插入到Elastic Server中。


    Application.java

    package com.mkyong;
    
    import com.mkyong.book.model.Book;
    import com.mkyong.book.service.BookService;
    import org.elasticsearch.client.Client;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.PageRequest;
    import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
    
    import java.util.Map;
    
    @SpringBootApplication
    public class Application implements CommandLineRunner {
    
        @Autowired
        private ElasticsearchOperations es;
    
        @Autowired
        private BookService bookService;
    
        public static void main(String args[]) {
            SpringApplication.run(Application.class, args);
        }
    
        @Override
        public void run(String... args) throws Exception {
    
            printElasticSearchInfo();
    
            bookService.save(new Book("1001", "Elasticsearch Basics", "Rambabu Posa", "23-FEB-2017"));
            bookService.save(new Book("1002", "Apache Lucene Basics", "Rambabu Posa", "13-MAR-2017"));
            bookService.save(new Book("1003", "Apache Solr Basics", "Rambabu Posa", "21-MAR-2017"));
    
            //fuzzey search
            Page<Book> books = bookService.findByAuthor("Rambabu", new PageRequest(0, 10));
    
            //List<Book> books = bookService.findByTitle("Elasticsearch Basics");
    
            books.forEach(x -> System.out.println(x));
    
    
        }
    
        //useful for debug, print elastic search details
        private void printElasticSearchInfo() {
    
            System.out.println("--ElasticSearch--");
            Client client = es.getClient();
            Map<String, String> asMap = client.settings().getAsMap();
    
            asMap.forEach((k, v) -> {
                System.out.println(k + " = " + v);
            });
            System.out.println("--ElasticSearch--");
        }
    
    }

    输出结果:

    .   ____          _            __ _ _
     /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
     \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v1.5.1.RELEASE)
    
    //...
    --ElasticSearch--
    client.type = transport
    cluster.name = mkyong-cluster
    name = Baal
    network.server = false
    node.client = true
    transport.ping_schedule = 5s
    --ElasticSearch--
    Book{id='1001', title='Elasticsearch Basics', author='Rambabu Posa', releaseDate='23-FEB-2017'}
    Book{id='1002', title='Apache Lucene Basics', author='Rambabu Posa', releaseDate='13-MAR-2017'}
    Book{id='1003', title='Apache Solr Basics', author='Rambabu Posa', releaseDate='21-MAR-2017'}
    //...

    当我们运行应用程序时,我们的数据存储在$ {ELASTICSEARCH_HOME} \ data \ mkyong-cluster中了。


    5.3 Maven打包一下并且运行它。


    运行结果:

    $ mvn package
    $ java -jar target/springboot-springdata-elasticsearch-example-1.0.jar


    5.4用cURL工具测试一下。


    运行结果:

    C:\curl-7.53.1\bin>curl "http://localhost:9200/mkyong/books/_search?pretty=true"
    {
      "took" : 3,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "failed" : 0
      },
      "hits" : {
        "total" : 3,
        "max_score" : 1.0,
        "hits" : [ {
          "_index" : "mkyong",
          "_type" : "books",
          "_id" : "1001",
          "_score" : 1.0,
          "_source" : {
            "id" : "1001",
            "title" : "Elasticsearch Basics",
            "author" : "Rambabu Posa",
            "releaseDate" : "23-FEB-2017"
          }
        }, {
          "_index" : "mkyong",
          "_type" : "books",
          "_id" : "1002",
          "_score" : 1.0,
          "_source" : {
            "id" : "1002",
            "title" : "Apache Lucene Basics",
            "author" : "Rambabu Posa",
            "releaseDate" : "13-MAR-2017"
          }
        }, {
          "_index" : "mkyong",
          "_type" : "books",
          "_id" : "1003",
          "_score" : 1.0,
          "_source" : {
            "id" : "1003",
            "title" : "Apache Solr Basics",
            "author" : "Rambabu Posa",
            "releaseDate" : "21-MAR-2017"
          }
        } ]
      }
    }
    
    


    运行结果:

    C:\curl-7.53.1\bin>curl "http://localhost:9200/mkyong/books/_search?q=_id:1003&pretty=true"
    {
      "took" : 3,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "failed" : 0
      },
      "hits" : {
        "total" : 1,
        "max_score" : 1.0,
        "hits" : [ {
          "_index" : "mkyong",
          "_type" : "books",
          "_id" : "1003",
          "_score" : 1.0,
          "_source" : {
            "id" : "1003",
            "title" : "Apache Solr Basics",
            "author" : "Rambabu Posa",
            "releaseDate" : "21-MAR-2017"
          }
        } ]
      }
    }


    原文链接:

    https://www.mkyong.com/spring-boot/spring-boot-spring-data-elasticsearch-example/


    相关阅读:

    官网:

    https://github.com/spring-projects/spring-data-elasticsearch


    版本约束相关:

    https://github.com/spring-projects/spring-data-elasticsearch/wiki/Spring-Data-Elasticsearch---Spring-Boot---version-matrix


    springboot系列文章:

    https://mp.weixin.qq.com/s/riHsgEXsljMEVfyAluxt2Q

    展开全文
  • 原文地址:spring boot 2.0 整合 elasticsearch NoNodeAvailableException 原文说的有点问题,下面贴出我的配置: 原码云项目地址:https://gitee.com/11230595/springboot-elasticsearch 我的码云项目地址,在...

    原文地址:spring boot 2.0 整合 elasticsearch NoNodeAvailableException

    原文说的有点问题,下面贴出我的配置:

    原码云项目地址:https://gitee.com/11230595/springboot-elasticsearch

    我的码云项目地址,在原来基础上进行了修改和优化

    elasticsearch.yml

    cluster.name: my-application
    network.host: 0.0.0.0

    http.port: 9200
    transport.tcp.port: 9300
    transport.tcp.compress: true

     

    application.properties

    # elasticsearch.yml 文件中的 cluster.name
    spring.data.elasticsearch.cluster-name=my-application
    # elasticsearch 调用地址,多个使用“,”隔开
    spring.data.elasticsearch.repositories.enabled=true
    spring.data.elasticsearch.cluster-nodes=localhost:9300
    spring.data.elasticsearch.properties.path.logs=./elasticsearch/log
    spring.data.elasticsearch.properties.path.data=./elasticsearch/data

     

    展开全文
  • spring boot 版本2.0.0 ...一、pom.xml引入spring-boot-starter-data-elasticsearch,它的version是跟随spring boot的 <dependency> <groupId>org.springframework.boot</groupId> <arti...
  • spring boot 整合 elasticsearch 实现按照属性查询 spring boot 整合 elasticsearch 实现按照ID查询 注意查看README.MD文件
  • Spring Boot+Spring data JPA+Spring Data ElasticSearch的简单使用 ** JPA是什么? JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象...
  • 下载ElasticSearch 下载地址 ... Spring Boot集成ElasticSearch(gradle) // 添加 Spring Data Elasticsearch 的依赖 compile('org.springframework.boot:spring-boot-starter...
  • 文章目录概述官方JAVA API文档... 我们这里没有使用Spring 提供的 spring-boot-starter-data-elasticsearch,使用的是ES原生的API 。 官方JAVA API文档 当前7.0 https://www.elastic.co/guide/en/elasticsearch/...
  • 本次尝试在springboot项目中集成elasticsearch,不过由于spring bootes、rest的版本问题,折腾了好久,写这篇文章的目的一是分享一下,也是为了纪念当时的摸爬滚打。 注:集成过程中有参考部分网友的文章。 版本和...
  • Spring Boot接入Elasticsearch

    千次阅读 2019-01-23 19:00:24
    Spring boot 接入ES 最终效果: 效果丑,也就那么意思一下;因为sping boot已经集成了es,所以接入es只需要很简单的配置一下就好了。当然事先得下载好es,我用的是windows 6.5.3版本,没有遇到什么版本冲突问题,...
  • 测试 spring boot集成 elasticsearch 填写es配置spring: data: elasticsearch: cluster-name: myes # cluster-nodes: 127.0.0.1:9300 cluster-nodes: hadoop137:9300,hadoop138:9300,hadoop139:9300
  • spring boot2.0 + elasticsearch+ redis 遇到elasticsearch执行不成功
  • 使用Spring Data ElasticSearch 3.2.0,需使用ES API为6.8.2 使用Rest High Level Client 6.8.2 使用Jest API 6.3.1 项目暂时未使用脚手架的方式开发Vue前端界面,采用了原始的方式进行开发 技术架构 JDK 1.8 Spring ...
  • Spring boot Elasticsearch RestHighLevelClient

    千次阅读 2019-03-18 17:42:58
    Java 配置 Elasticsearch RestHighLevelClient (带认证) application.yml spring: elasticsearch: rest: uris: - http://localhost:9200 username: elastic password: password pom.xm...
  • 篇幅所限,本文就只用后面两种方式来分别连接并操作Elasticsearch,关于spring boot的更多学习资料可以点击我整理的Spring Boot学习笔记领取,解锁更多spring全家桶的骚操作玩法。???? 1.2.  环境与配
  • Spring Boot 是非常流行的 Java 开发框架,文章主要介绍 Spring Boot 集成 Elasticsearch(简称 ES),实现大数据量的业务数据的查询分析。 本场 Chat 主要介绍内容如下: ES 的安装运行 elasticsearch-head 插件...
  • Jestclient)2、SpringData ElasticSearch [Es版本有可能不合遁1 版本遁配説明: https://github. com/'spring-projects/spring -data -elasticsearch如果版本不造配: 2.4.d 1)、升級SpringBoot版本2)、 安装対麼...
  • 前面学习了 Elasticsearch 的简单基本操作,例如安装,基本的操作命令等,今天就来看看 esSpring boot 的简单整合,实现增删改查的功能。众所周知,Spring boot 支持多种 NoSql 数据库,例如 redis、mongodb,...
  • 唯有“熬过去,才会赢” 前言 ElasticsearchSpring Data是Spring Data项目的一部分,Spring Data Elasticsearch项目提供了对Elasticsearch操作的集成,用于对Elastichsearch文档进行交互并轻松编写访问层代码。...
  • 主要给大家介绍了关于Spring boot中使用ElasticSearch的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
  • jdk1.8、elasticsearch-5.5.2:官网 1.1:pom <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <vers...
  • Elasticsearch 4. Spring Boot整合Elasticsearch

    千次阅读 2021-06-09 15:13:56
    Spring Boot整合elasticsearch 目前常用的Spring Boot整合elasticsearch的java客户端有: spring-boot-starter-data-elasticsearch 官方文档:...
  • Spring boot整合ElasticSearch 环境 Spring boot 2.2.9.RELEASE ElasticSearch 版本6.7 jdk 1.8 pom文件 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>...
  • Spring Data Elasticsearch 1(Elasticsearch1) 序号 Spring Data Elasticsearch版本 Elasticsearch版本 1 1.0.0.RELEASE 1.1.1 2 1.1.0.RELEASE 1.3.2 3 1.2.0.RELEASE 1.4.4 4...
  • Spring Boot系列(十)Spring Boot整合Elasticsearch全文搜索引擎 这一篇介绍Spring Boot整合Elasticsearch全文搜索引擎,需要springboot实战完整视频教程的,点击这里! Elastic Search是一个开源的,分布式...
  • spring-boot-starter-data-elasticsearch (以前做项目的时候,Spring Data ESES服务存在版本匹配关系,但目前在spring boot v2.0.4.RELEASE中使用未发现有版本不兼容情况) spring-boot-starte...
  • 之前介绍过几种操作ES的方法 安装Elasticsearch-Head或者使用Postman,也可以使用官方的Kibana直接请求接口,同时 使用官方推荐的Java High ...Spring Boot 集成ElasticsearchSpring Data Elasticsearch使用起来类

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 26,660
精华内容 10,664
关键字:

bootspring访问es

spring 订阅