springboot 与检索

2024-06-24 08:40

springboot 与检索

1、检索

image.png

2、检索-Elasticsearch快速入门

https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html

image.png

3、springboot整合elasticSearch测试

image.png

image.png

<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-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        
          <!-- https://mvnrepository.com/artifact/io.searchbox/jest -->
        <dependency>
            <groupId>io.searchbox</groupId>
            <artifactId>jest</artifactId>
            <version>5.3.2</version>
        </dependency>
    </dependencies>

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

image.png

image.png

image.png

image.png

@SpringBootApplication
public class SpringElasticsearchApplication {

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

}
spring:
  elasticsearch:
    jest:
      uris: http://182.61.149.246:9200
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

@RunWith(SpringRunner.class)
@SpringBootTest(classes=SpringElasticsearchApplication.class)
@AutoConfigureMockMvc
class SpringElasticsearchApplicationTests {
    @Autowired
    private MockMvc mvc;

    @Test
    void contextLoads() {
    }
}


import io.searchbox.client.JestClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class TestSpringElasticSearch extends SpringElasticsearchApplicationTests {
    @Autowired
    JestClient jestClient;


   @Test
    public void testCrtIdx()  {
        Article article=new Article();
        article.setId(1);
        article.setTitle("西游记");
        article.setAuthor("吴承恩");
        article.setContent("孙悟空三打白骨精");
        Index index = new Index.Builder(article).index("atguigu").type("news").build();
        try {
            JestResult jestResult= jestClient.execute(index);
            System.out.println(jestResult.getJsonObject().toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testgetByIdx()  {
        String json ="{\"query\":{\"match\":{\"content\":\"孙悟空\"}}}";

        Search index = new Search.Builder(json).addIndex("atguigu").addType("news").build();
        try {
            JestResult jestResult= jestClient.execute(index);
            System.out.println(jestResult.getJsonObject().toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
spring:
  elasticsearch:
    jest:
      uris: http://182.61.149.246:9200
  data:
    elasticsearch:
      cluster-name: elasticsearch
      cluster-nodes: 182.61.149.246:9300
相关新闻
热点
视频
投票
查看结果
Tags

站点地图 在线访客: 今日访问量: 昨日访问量: 总访问量:

© 2025 个人网站 版权所有

备案号:苏ICP备2024108837号

苏公网安备32011302322151号