I. Introduction
ElasticSearch can quickly store, search and analyze massive amounts of data. Elasticsearch is a distributed search service providing Restful API, based on the underlying Lucene, multi Shard (slice) means to ensure data security, and provide automatic resharding features, and other large GitHub site is employed as ElasticSearch search service.
Staff documents stored in the form of an example: a document on behalf of employee data. Stored data to conduct ElasticSearch called index, but before indexing a document, you need to determine where the document is stored in.
ElasticSearch a cluster index may comprise a plurality, corresponding to each index may comprise a plurality of types. These different types of stores a plurality of documents, each have multiple attributes.
Similar to the relationship:
Index - Database
Type - table
documents - records in the table
Properties - Column
Second, integration elasticsearch
1, dependent on the introduction of
<!--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>
2, application.properties Configuration
spring.elasticsearch.jest.uris=http://192.168.106.130:9200
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=192.168.106.130:9300
3, of the main program Springboot11ElasticApplication
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/***
* SpringBoot默认支持两种技术来和ES交互
* 1、Jest(默认不生效)
* 需要导入jest的工具包(io.searchbox.client.JestClient)
* 2、SpringData ElasticSearch【ES版本有可能不合适】
* 版本适配说明:https://github.com/spring-projects/spring-data-elasticsearch
* 如果版本不适配:2.4.6
* 1)、升级SpringBoot版本
* 2)、安装对应版本的ES
* 1)、Client 节点信息clusterNodes;clusterName
* 2)、ElasticsearchTemplate
* 3)、编写一个ElasticsearchRepository的子接口来操作ES;
* 两种用法:https://github.com/spring-projects/spring-data-elasticsearch
* 1)、编写一个ElasticsearchRepository
*
**/
@SpringBootApplication
public class Springboot11ElasticApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot11ElasticApplication.class, args);
}
}
4, the entity class Article
import io.searchbox.annotations.JestId;
public class Article {
@JestId
private Integer id;
private String author;
private String title;
private String content;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
5, the entity class Book
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "atguigu", type = "book")
public class Book {
private Integer id;
private String bookName;
private String author;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", bookName='" + bookName + '\'' +
", author='" + author + '\'' +
'}';
}
}
6、BookRepository
import com.atguigu.elastic.bean.Book;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface BookRepository extends ElasticsearchRepository<Book, Integer> {
public List<Book> findByBookNameLike(String bookName);
}
7, using JestClient
@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot11ElasticApplicationTests {
@Autowired
JestClient jestClient;
@Autowired
BookRepository bookRepository;
@Test
public void test02() {
/* Book book = new Book();
book.setId(1);
book.setBookName("西游记");
book.setAuthor("吴承恩");
bookRepository.index(book);*/
for (Book book : bookRepository.findByBookNameLike("游")) {
System.out.println(book);
}
}
@Test
public void contextLoads() {
//1、给Es中索引(保存)一个文档;
Article article = new Article();
article.setId(1);
article.setTitle("好消息");
article.setAuthor("zhangsan");
article.setContent("Hello World");
//构建一个索引功能
Index index = new Index.Builder(article).index("atguigu").type("news").build();
try {
//执行
jestClient.execute(index);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void search() {
String json = "{\n" +
" \"query\": {\n" +
" \"match\": {\n" +
" \"content\": \"hello\"\n" +
" }\n" +
" }\n" +
"}";
//构建搜索功能
Search search = new Search.Builder(json).addIndex("atguigu").addType("news").build();
try {
//执行
SearchResult result = jestClient.execute(search);
System.out.println(result.getJsonString());
} catch (IOException e) {
e.printStackTrace();
}
}
}