I. Introduction
Elasticsearch is a bottom Lucene based distributed search service and provides multi-style API Restful Shard (slice) of data security mode also provides automatic features resharding
Elasticsearch can quickly search and analyze massive amounts of data storage
SpringBoot default support both technologies to interact and elasticsearch: Jest and Spring Data Elasticsearch
SpringBoot default using Spring Data Elasticsearch module to operate
Second, the use
Jest is a very popular Elasticsearch client tools using Http and client interaction
Jest default is not in force to take effect need to import Jest Kit ( io.searchbox.client.JestClient
)
1, operates using Jest
The first is import dependent Jest:
Note: Depending on the version and Elasticsearch corresponding
example, I was 6.x version Elasticsearch then use the version 6.x Jest
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>6.3.1</version>
</dependency>
Elasticsearch version can access ip:9200
to see the data after the return:
Then configuration parameters:
URI 9200 is the underlying default port of the machine
spring.elasticsearch.jest.uris=http://111.111.111.111:9200
Note: Configure the time to bring URI http request method not only ip: port otherwise it will error Illegal character in scheme name
a successful connection
To the primary key attribute of the class with the entity @JestId
Note:
public class Student {
// 用@JestId注解来标明主键 标注之后 存入Elasticsearch的id值就采用该属性的值了
@JestId
private Integer id;
private String name;
private String hobby;
private String introduce;
[getter和setter...]
}
Then there is the call:
Injection JestClient to class:
@Autowired
JestClient jestClient;
Elasticsearch to index a document:
Student student=new Student();
student.setId(1);
student.setName("陈涛");
student.setHobby("swimming");
student.setIntroduce("Hello World");
// 构建一个索引
// index(索引名).type(类型名)
// 还可通过id(id值)来设置id 因为id已经有了 就不设置了
Index index = new Index.Builder(student).index("zjitc").type("student").build();
try {
// 执行该索引
jestClient.execute(index);
} catch (IOException e) {
e.printStackTrace();
}
Success Index
research all:
Only need to pass JSON format can search expression
// 定义搜索表达式
String json="{\n" +
" \"query\" : {\n" +
" \"match\" : {\n" +
" \"introduce\" : \"Hello\"\n" +
" }\n" +
" }\n" +
"}";
// 构建搜索
Search search = new Search.Builder(json).addIndex("zjitc").addType("student").build();
try {
// 执行该索引
SearchResult result = jestClient.execute(search);
// 输出JSON格式的结果
System.out.println(result.getJsonString());
} catch (IOException e) {
e.printStackTrace();
}
About search expressions introduction of another blog I see: under Elasticsearch search middleware Docker environment installation and use basic operations (CRUD, conditional search, full-text search, phrase search, highlight the search)
official documents: Elasticsearch : The Definitive Guide
2, operates using Spring Data Elasticsearch
Spring Boot provides a very convenient search function supported by the integration of Spring Data Elasticsearch
First is the import-dependent:
If you create a project with SpringBoot Initializer, then select Spring Data Elasticsearch module:
if the project is not created with the wizard you will need to manually import dependence SpringDataElasticsearch:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
Then the configuration parameters:
Spring Data Elasticsearch default has been automatically configured TransportClient clients
need to configure a cluster node clusterNodes information: clusterName
node name can also access the ip:9200
data returned to see:
Configuration:
# 节点名称
spring.data.elasticsearch.cluster-name=docker-cluster
# 使用的是9300端口 并不是使用Http的9200端口通信的
spring.data.elasticsearch.cluster-nodes=111.111.111.111:9300
connection succeeded
Note : If the startup may be unsuccessful version SpringDataElasticsearch and Elasticsearch not fit
Solution:
1, upgrade SpringBoot version
2, install the corresponding version of Elasticsearch
To the entity classes with the @Document
annotation to specify the index name and type name:
// 用@Document注解来指定索引名和类型名
@Document(indexName = "ciczjitc",type = "book")
public class Book {
private Integer id;
private String name;
private String author;
[getter和setter...]
}
Spring Data Elasticsearch also enabled ElasticsearchRepository
Interface This interface defines a number of operations Elasticsearch method
as long as the interface can inherit
Write a ElasticsearchRepository sub-interfaces to operate:
//ElasticsearchRepository泛型:<要存储的类型,主键的类型>
public interface BookRepository extends ElasticsearchRepository<Book,Integer> {
}
Finally, use:
A class injection ElasticsearchRepository:
@Autowired
StudentRepository studentRepository;
Elasticsearch to index a document:
Book book=new Book();
book.setId(1);
book.setName("My life");
book.setAuthor("张涛");
bookRepository.index(book);
Success Index
Also support fuzzy queries:
Interface to add a method inherited ElasticsearchRepository interface:
public interface BookRepository extends ElasticsearchRepository<Book,Integer> {
public List<Book> findByNameLike(String name);
}
test:
List<Book> books = bookRepository.findByNameLike("life");
for (Book b:books)
{
System.out.println(b);
}
search successful
More ways to refer to the official documentation: official document