How to use elastic search in combination with java? I don't need to talk much about it. Load it!!!
Integrating Spring Data ElasticSearch
1, How to view official documents
Official documents: https://www.elastic.co/cn/elastic-stack (follow the steps below to choose another version)
Skip 6.3.0 document directly: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.3/java-rest-high.html
Here's how to get documents (you don't need to look at them):
-
Step 1: query "document"
-
Step 2: view the Client document
-
Step 3: view the REST based api
-
Step 4: determine the version used
-
Step 5: select the basic API to use
-
- The Low Level Rest Client is a low-level encapsulation that provides some basic functions, but is more flexible
- The High Level Rest Client is a high-level encapsulation based on the Low Level Rest Client, with richer and more complete functions
Two. Introduction:
Spring Data Elasticsearch is a sub module of Spring Data project.
Simplifies the opening of native elastic search.
Check the official website of Spring Data: http://projects.spring.io/spring-data/
The mission of Spring Data is to provide a unified programming interface for all kinds of data access, whether it is a relational database (such as MySQL), a non relational database (such as Redis), or an index database like elastic search. In order to simplify the code of developers and improve the development efficiency.
Modules with many different data operations:
-
Spring Data ElasticSearch simplifies native ElasticSearch
-
Characteristic:
-
1) Based on the @ Configuration configuration, as long as it is configured in the yml file, it can be used in the project.
-
2) Tool class ElasticsearchTemplate ES template, similar to general mapper, operates ES through object
-
3) Provides a Repository for the persistence layer interface, which is equivalent to using mapper
-
3, Setting up the environment
First of all, this is based on the spring cloud environment
Step 1: modify pom.xml file and add corresponding coordinates
<!--es-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
Step 2: modify the yml file and add the relevant configuration of elastic search
spring:
redis:
Database: 1 ා confirm to use the database
host: 127.0.0.1, redis service address
port: 6379? redis port number
data:
elasticsearch:
cluster-name: elasticsearch
cluster-nodes: 127.0.0.1:9300
Step 3: write the test (empty method)
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class)
public class TestES {@Test
public void demo01(){
System.out.println("Huang sir666");
}
}
Note: after writing and running here, errors may be reported. If running normally, continue. If there is any problem, please go to my last blog for a detailed explanation
Come, I'm afraid you don't want to find it. Go to the address directly: (it's so intimate, hehe)
4, Index operation
If there are friends who study and understand my previous blog, it can be said that with the understanding of the previous blog, the next operation will be more easy to understand!
Of course, if you haven't read the previous blog, go to the address directly, you can go to understand ha!
Review the api operations:
1. Mapping class
Mapping class: used to represent the data correspondence between java and elastic search. Using annotations in spring data elastic search.
Annotated name | describe |
---|---|
@Document | Used to configure Java class and index / type correspondence -indexName: corresponding index library name -Type: the type corresponding to the index library -shards: number of slices, default 5 -Replicas: number of replicas, default 1 |
@Id | Unique identification |
@Field | Used to configure the field correspondence between Java properties and es – type: field type, enumeration: FieldType – analyzer: word breaker name – index: index or not, boolean type, default is true – store: store or not, boolean type, default is false |
Implementation: create a class
@Document(indexName = "czxy1", type = "book", shards = 4, replicas = 2) @Data @NoArgsConstructor @AllArgsConstructor @ToString public class ESBook { @Id private Long id; @Field(type = FieldType.Text, analyzer = "ik_max_word") private String titles; @Field(type = FieldType.Keyword, index = true) private String images; @Field(type = FieldType.Float) private Float price; }
3. Create index, add mapping and delete index
The ElasticsearchTemplate tool class provides corresponding methods to complete the following functions:
-
Create index: createindex (mapping class. Class)
-
Add mapping: putmapping (mapping class. Class)
-
Delete index: deleteindex (mapping class. Class)
-
Step 1: directly inject ElasticsearchTemplate into the test class
-
Step 2: call the corresponding API for operation
@RunWith(SpringRunner.class) @SpringBootTest(classes = TestApplication.class) public class TestES { @Resource private ElasticsearchTemplate elasticsearchTemplate; @Test public void run1() { //Create index elasticsearchTemplate.createIndex(ESBook.class); } @Test public void run2() { //add mapping elasticsearchTemplate.putMapping(ESBook.class); } @Test public void run3() { //delete mapping elasticsearchTemplate.deleteIndex(ESBook.class); } }
5, Document operation (addition, deletion and modification)
1. Top level interface: Repository
-
Spring data elastic search provides a top-level interface Repository for adding, deleting, and querying configuration classes. The top-level class does not complete specific functions. The specific functions are completed by subclasses
-
Pagingandsorting Repository: (second generation) complete paging and sorting functions
-
ElasticsearchCrudRepository: add, delete, modify and search
-
Elastic search Repository: (fourth generation) complete all functions
-
-
Write: just write sub interface, inherit ElasticsearchRepository, and spring data will load the class automatically.
-
Note: when using the interface, you need to determine two generic information
-
First generic: mapping class, ESBook
-
The second generic type: the type uniquely identified by the mapping class, the type Long of the ID
-
-
public interface ESBookRespository extends ElasticsearchRepository<ESBook, Long> { }
-
Use: in other locations, directly inject the repository sub interface
2. Add data
Create a test class
@RunWith(SpringRunner.class) @SpringBootTest(classes = TestApplication.class) public class TestESData { @Resource private ESBookRespository esBookRespository; @Test public void run1(){ //Add a data //Prepare data ESBook esBook = new ESBook(55L, "sir","10.jpg",222f); / / add esBookRespository.save(esBook); } @Test public void run2(){ //Add a set of data //Prepare data ArrayList<ESBook> list = new ArrayList<>(); list.add(new ESBook(59L, "Huang sir programming ideas", "6.jpg",12f)); list.add(new ESBook(99L, "goddess of Huang sir", "7.jpg",56f)); list.add(new ESBook(70L,"java programming ideas", "8.jpg",89f)); / / add esBookRespository.saveAll(list); } }
3. Modify data
-
Modify and add using the same method
-
Differentiation criteria:
-
If the value of id has corresponding data, update is performed.
-
If the value of id does not have corresponding data, add.
-
@Test
public void demo03(){
//Update data. There must be data with id=1 in es
// Preparation data
ESBook esBook = new ESBook(1L,"In those years, the pit we threw together II","1111.jpg",1998f);// Add to
esBookRepository.save(esBook);
}
4. Delete data
@Test
public void demo04(){
//Delete dataESBook esBook = new ESBook();
esBook.setId(1L);esBookRepository.delete(esBook);
}
Six. Query
1. Basic query
Create a test class:
@RunWith(SpringRunner.class) @SpringBootTest(classes = TestApplication.class) public class TestESFind { @Resource private ESBookRespository esBookRespository; @Test public void run1() { //Query all Iterable<ESBook> all = esBookRespository.findAll(); Iterator<ESBook> iterator = all.iterator(); while (iterator.hasNext()) { ESBook esbook = iterator.next(); System.out.println(esbook); } } @Test public void run2() { //Query details by id Optional<ESBook> op = esBookRespository.findById(2L); ESBook esBook = op.get(); System.out.println(esBook); } }
Extension: what is Optional
2. User defined method query
-
Custom method query, spring data automatically queries according to the agreed method name.
-
Contract method name requirements: findBy field name | keyword, etc
-
For example: findbytitle (value) queries by title
-
-
Example 1: query by title
-
1) Modify response custom interface and add method declaration
-
public interface ESBookRespository extends ElasticsearchRepository<ESBook, Long> { //Example 1. Query according to the title public List<ESBook> findByTitles(String title); }
2) To test classes, call custom methods, and complete functions
@Test public void run3() { List < esbook > List = esbookrepository.findbytitles ("sir"); System.out.println(list); }
Example 2: interval query, price 1-200
1) statement
//Example 2: interval query, price 1-200 public List<ESBook> findByPriceBetween(Float start, Float end);
2) call
@Test public void run4() { List<ESBook> b = esBookRespository.findByPriceBetween(1f, 200f); System.out.println(b); }
Example 3: query price > = 123
1) statement
//Example 3. Query price is greater than > = 123 public List<ESBook> findByPriceGreaterThanEqual(Float price);
2) call
@Test public void run5() { List<ESBook> byPriceGreaterThanEqual = esBookRespository.findByPriceGreaterThanEqual(123f); System.out.println(byPriceGreaterThanEqual); }
Example 4: interval query, price 50-300, and sorting by images in descending order
1) statement
//Example 4: range query price is 400-600, and it is sorted in descending order of images public List<ESBook> findByPriceBetweenOrderByImagesDesc(Float a, Float b);
2) call
@Test public void run6() { List<ESBook> list = esBookRespository.findByPriceBetweenOrderByImagesDesc(400f, 600f); System.out.println(list); }
3. User defined query
(1) Keyword query: match
-
Query criteria Builder: native search query builder
-
match condition: querybuilders.matchquery (field, value);
-
Query criteria: queryBuilder.withQuery(...)
-
Query: repository.search (condition)
-
Page return type, which is itself a container, can directly traverse to get data
-
Total number of entries: page.getTotalElements()
-
Total pages: page.getTotalPages()
-
Review api operations:
Realization:
@RunWith(SpringRunner.class) @SpringBootTest(classes = TestApplication.class) public class TestQurey { @Resource private ESBookRespository esBookRespository; //Demand; query title as "next year @Test public void run1() { //Query condition builder (store all query conditions) NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder(); //Keyword query match queryBuilder.withQuery(QueryBuilders.matchQuery("titles", "Huang sir")); //query Page<ESBook> page = esBookRespository.search(queryBuilder.build()); //Processing result System.out.println("Total number:" + page.getTotalElements()); System.out.println("PageCount:" + page.getTotalPages()); for (ESBook esBook : page) { System.out.println(esBook); } } }
(2) Multi condition query: bool -- must/mustNot (intersection)
-
Multi conditional patching, using BoolQueryBuilder object
-
QueryBuilders.boolQuery()
-
-
Union operation: must() / mustNot()
Review:
Realization:
//Multiple conditions are used for query. The query title is "Huang sir" and "2" is not included
@Test
public void run2() {
//Query criteria builder
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
//Query criteria - multiple criteria
BoolQueryBuilder bool = QueryBuilders.boolQuery();
//Condition 1; must
bool.must(QueryBuilders.matchQuery("titles", "Huang sir"));
//Condition 2; not required
bool.mustNot(QueryBuilders.matchQuery("titles", "2"));
queryBuilder.withQuery(bool);
//query
Page<ESBook> page = esBookRespository.search(queryBuilder.build());
//Processing result
System.out.println("Total number:" + page.getTotalElements());
System.out.println("PageCount:" + page.getTotalPages());
//It can be traversed in this way, or it can be used as a for loop
page.forEach(esBook -> {
System.out.println(esBook);
});
}
(3) Multi condition query: bool--should (Union)
BoolQueryBuilder provides should for writing union conditions
Review:
Realization:
//Requirement: query“Huang sir Programming idea"as well as"Huang sir Goddess"related data
@Test
public void run3() {
//Query criteria builder
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
//query criteria
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.should(QueryBuilders.matchQuery("titles", "Huang sir Programming idea"));
boolQueryBuilder.should(QueryBuilders.matchQuery("titles", "Huang sir Goddess"));
queryBuilder.withQuery(boolQueryBuilder);
//query
Page<ESBook> page = esBookRespository.search(queryBuilder.build());
//Processing result
System.out.println("Total number:" + page.getTotalElements());
System.out.println("PageCount:" + page.getTotalPages());
for (ESBook esBook : page) {
System.out.println(esBook);
}
}
(4) Precise query: term
Review API operations:
Realization:
//Precise query term //#Demand: query price as 12 information @Test public void run4() { //Query criteria builder NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder(); //query criteria queryBuilder.withQuery(QueryBuilders.termQuery("price", 12)); //query Page<ESBook> page = esBookRespository.search(queryBuilder.build()); //Processing result System.out.println("Total number:" + page.getTotalElements()); System.out.println("PageCount:" + page.getTotalPages()); for (ESBook esBook : page) { System.out.println(esBook); } }
(5) Interval query
Review:
Realization:
//Requirement: query result is between 10-100 @Test public void run5() { //Query criteria builder NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder(); //Query criteria queryBuilder.withQuery(QueryBuilders.rangeQuery("price").gte(10).lte(100)); / / query Page<ESBook> page = esBookRespository.search(queryBuilder.build()); //Processing results System.out.println("total number:" + page.getTotalElements()); System.out.println("total pages:" + page.getTotalPages()); for (ESBook esBook : page) { System.out.println(esBook); }
4. Paging query
-
The builder provides withPageable() to set paging data
-
Set specific paging parameters through tool class: pagerequest.of (pagenum, PageSize)
Review:
Realization:
//paging @Test public void run6() { //Query criteria builder NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder(); //query criteria int pageNum = 1; //How many pages int pageSize = 2; //Number of items per page queryBuilder.withPageable(PageRequest.of(pageNum, pageSize)); //query Page<ESBook> page = esBookRespository.search(queryBuilder.build()); //Processing result System.out.println("Total number:" + page.getTotalElements()); System.out.println("PageCount:" + page.getTotalPages()); System.out.println("How many pages:" + page.getNumber()); System.out.println("Number of pages per page:" + page.getSize()); for (ESBook esBook : page) { System.out.println(esBook); } }
5. Sorting Query
-
Set sorting conditions through queryBuilder.withSort
-
Sorting fields are determined by SortBuilders.fieldSort("price")
-
Sort by. Order (sortorder. DESC)
Review:
Realization:
//Demand; in descending order of price
@Test
public void run7() {
//Query add builder
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
//query criteria
queryBuilder.withSort(SortBuilders.fieldSort("price").order(SortOrder.DESC));
//query
Page<ESBook> page = esBookRespository.search(queryBuilder.build());
//Processing result
System.out.println("Total number:" + page.getTotalElements());
System.out.println("PageCount:" + page.getTotalPages());
for (ESBook esBook : page) {
System.out.println(esBook);
}
}
6. summary
-
ES template: ElasticsearchTemplate
-
Build index: createindex (mapping class. Class)
-
Add mapping: putmapping (mapping class. Class)
-
Delete index: deleteindex (mapping class. Class)
-
-
Mapping class: Java objects correspond to ES data
-
@Document, used to configure index and type name
-
indexName: index name
-
Type: type name
-
-
@Id, unique Id
-
@Field, field in map
-
Type: field type
-
analyzer: participle
-
Index: index
-
store: Backup
-
-
-
Data operation top-level interface Repository, a more comprehensive sub interface elastic search Repository
-
Add data: save (object), saveall (Collection)
-
Delete data: Delete (object), deletebyid (T), deleteAll()
-
Basic query: findAll(), findById()
-
Custom method query: findBy field name, keyword... (And, Or, Between, OrderBy, etc.)
-
-
Query criteria Builder: native search query builder
-
Query condition: withQuery(....)
-
Paging condition: withPageable(...)
-
Sort by: withSort(....)
-
Build condition: build()
-
-
Query criteria, obtained from the tool class QueryBuilders
-
Keyword query: matchquery (field, value)
-
Multi criteria query: boolQuery()
-
Must have: must()
-
Must not have: mustNot()
-
Union: should()
-
-
Precise query: termquery (field, value)
-
Interval query: rangequery (field)
-
Greater than: gt()
-
Greater than or equal to: gte()
-
Less than: lt()
-
Less than or equal to: lte()
-
-
-
Paging condition
-
By setting the paging parameter pagerequest.of (pagenum, PageSize)
-
-
Sort condition, obtained through the tool class SortBuilders
-
Sort field: fieldsort
-
Sort by: order (sortorder. Desc | sortorder. ASC)
-
-
Query result: Page object
-
Total number of entries: page.getTotalElements()
-
Total pages: page.getTotalPages()
-
Page: page.getNumber()
-
Number of pages: page.getSize()
-
Get content: page.getContent(), Page object can be traversed directly
-
Please give yourself in the effort to point like Oh!
Make a little progress every day`~~~~~