springboot與elasticsearch

1.安裝elasticsearchjava

下載elasticsearchgit

docker pull registry.docker-cn.com/library/elasticsearch

運行elasticsearchgithub

docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -p 9200:9200 -p 9300:9300 --name elasticsearch01 5acf0e8da90b

 看到以下界面,則成功web

 2.數據模擬spring

2.1postman模擬發送數據docker

2.2postman模擬檢索數據(get請求)apache

2.3此外,delete請求是刪除元素,head請求用來檢查是否存在json

3.demo實現app

3.1pom文件elasticsearch

<?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.zy</groupId>
    <artifactId>spring-boot-elasticsearch-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot-elasticsearch-demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.14.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>
        <!--SpringBoot默認使用SpringData ElasticSearch模塊進行操做-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

        <dependency>
            <groupId>io.searchbox</groupId>
            <artifactId>jest</artifactId>
            <version>5.3.3</version>
        </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>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
        </dependency>
        <!--
            解決java.lang.ClassNotFoundException: com.sun.jna.Native問題
        -->
        <dependency>
            <groupId>com.sun.jna</groupId>
            <artifactId>jna</artifactId>
            <version>3.0.9</version>
        </dependency>
    </dependencies>

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


</project>

3.2yml文件

spring:
  elasticsearch:
    jest:
      uris: http://192.168.0.100:9200

3.3實體類

package com.zy.model;

import io.searchbox.annotations.JestId;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Article {
    @JestId
    private Integer id;
    private String author;
    private String title;
    private String content;
}

3.4測試類

package com.zy;

import com.zy.mapper.BookMapper;
import com.zy.model.Article;
import io.searchbox.client.JestClient;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
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.test.context.junit4.SpringRunner;

import java.io.IOException;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootElasticsearchDemoApplicationTests {

    @Autowired
    JestClient jestClient;

    @Autowired
    BookMapper bookMapper;

    @Test
    // 執行成功後的訪問方式   http://192.168.0.100:9200/book/news/1
    public void contextLoads() {
        Article article = Article.builder()
                .id(1)
                .title("good news")
                .author("東野圭吾")
                .content("降價了")
                .build();
        // 構建索引
        Index index = new Index.Builder(article)
                .index("book")
                .type("news")
                .build();
        // 執行索引
        try {
            jestClient.execute(index);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //測試搜索
    @Test
    public void search(){
//查詢表達式
        String json ="{\n" +
                "    \"query\" : {\n" +
                "        \"match\" : {\n" +
                "            \"content\" : \"了\"\n" +
                "        }\n" +
                "    }\n" +
                "}";

        //更多操做:https://github.com/searchbox-io/Jest/tree/master/jest
        //構建搜索功能
        Search search = new Search.Builder(json)
                .addIndex("book")
                .addType("news")
                .build();
        //執行
        try {
            SearchResult searchResult = jestClient.execute(search);
            System.out.println(searchResult.getJsonString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
相關文章
相關標籤/搜索