springboot 与检索
1、检索
2、检索-Elasticsearch快速入门
https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html
3、springboot整合elasticSearch测试
<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>
@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