In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "what is the code for SpringBoot integration ElasticSearch", so the editor summarizes the following contents, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "SpringBoot integration ElasticSearch code is what" article.
1. Elasticseach introduction 1. A brief introduction
Elasticsearch is also a full-text search library based on Lucene, which essentially stores data, and many concepts are similar to MySQL. It is a full-text retrieval technology.
Elasticsearch is a distributed search and analysis engine located at the core of Elastic Stack. Logstash and Beats help collect, aggregate, enrich, and store your data in Elasticsearch. Kibana enables you to explore, visualize, and share insights into data interactively, and to manage and monitor the stack. Elasticsearch is where the magic takes place to index, search, and analyze.
Elasticsearch is a distributed search and analysis engine based on JSON.
Developed in the Apache language and released as open source under the Apache license terms, Java is a popular enterprise search engine. Elasticsearch used in cloud computing, can achieve real-time search, stable, reliable, fast, easy to install and use.
two。 Comparative relationship:
Index (indices)-Databases database
Type (type)-Table data sheet
Document (Document)-Row line
Field (Field)-Columns column
3. Details:
Concept
Description
Index library (indices)
Indices is the plural of index and represents many indexes.
Type (type)
Type is to simulate the concept of table in mysql. There can be different types of indexes under an index library, such as commodity index and order index, with different data formats. However, this will lead to confusion in the index database, so the concept will be removed in future versions
Document (document)
The original data stored in the index library. For example, every piece of commodity information is a document.
Field (field)
Properties in the document
Mapping configuration (mappings)
The data type, attribute, index, storage and other characteristics of the field
4. Find out the interpretation of the data
Took: the query takes time (in milliseconds)
Time_out: whether timeout or not
_ shards: sharding information
Hits: search results overview object
Total: total number of entries searched
Max_score: the highest score for all the documents in the result
Hits: an array of document objects of the search results. Each element is a searched document information.
_ index: index library
_ type: document type
_ id: document id
_ score: document score
_ source: the source data of the document
Second, SpringBoot integrates Elasticseach1. Introduce dependency org.springframework.boot spring-boot-starter-data-elasticsearch org.springframework.boot spring-boot-starter-test test 2. Add the configuration spring: data: elasticsearch: cluster-name: elasticsearch cluster-nodes: 192.168.7.132 elasticsearch 93003. Create a package com.leyou.elasticsearch.pojo; import org.springframework.data.annotation.Id;import org.springframework.data.elasticsearch.annotations.Document;import org.springframework.data.elasticsearch.annotations.Field;import org.springframework.data.elasticsearch.annotations.FieldType corresponding to the pojo class and the index / * * create the pojo class corresponding to the index * * @ author Promsing (Zhang Youbo) * @ version 1.0.0 * @ since 2022-1-26-20:35 * / @ Document (indexName = "item", type = "docs", shards = 1, replicas = 0) public class Item {@ Id private Long id / * title * / @ Field (type = FieldType.Text,analyzer = "ik_max_word") private String title; * Category @ Field (type = FieldType.Keyword) private String category; * Brand private String brand; * Price @ Field (type = FieldType.Double) private Double price; * Image address private String images Public Long getId () {return id;} public void setId (Long id) {this.id = id; public String getTitle () {return title; public void setTitle (String title) {this.title = title; public String getCategory () {return category; public void setCategory (String category) {this.category = category; public String getBrand () {return brand Public void setBrand (String brand) {this.brand = brand; public Double getPrice () {return price; public void setPrice (Double price) {this.price = price; public String getImages () {return images; public void setImages (String images) {this.images = images Public Item () {public Item (Long id, String title, String category, String brand, Double price, String images) {@ Override public String toString () {return "Item {" + "id=" + id + ", title='" + title +'\'+ ", category='" + category +''+ " Brand=' "+ brand +'\'+", price= "+ price +", images=' "+ images +'\'+'}' } 4.SpringData encapsulates basic additions, deletions, modifications and queries, and custom additions, deletions, modifications and queries
You need to inherit the interface-ItemRepository
/ * * Custom API for adding, deleting, modifying and querying * * @ author Promsing (Zhang Youbo) * @ version 1.0.0 * @ since 2022-1-27-15:10 * / public interface ItemRepository extends ElasticsearchRepository {List findByTitle (String title); List findByPriceBetween (Double d1d1d2);} 5. Test method-- add, delete, change and check package com.leyou.elasticsearch; import com.leyou.elasticsearch.dao.ItemRepository;import com.leyou.elasticsearch.pojo.Item;import org.elasticsearch.index.query.MatchQueryBuilder;import org.elasticsearch.index.query.QueryBuilders;import org.elasticsearch.search.sort.SortBuilders;import org.elasticsearch.search.sort.SortOrder;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest Import org.springframework.data.domain.Page;import org.springframework.data.domain.PageRequest;import org.springframework.data.domain.Sort;import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;import org.springframework.test.context.junit4.SpringRunner;import java.util.ArrayList;import java.util.List;import java.util.Optional / * * Test the addition, deletion, modification and query of ES * * @ author Promsing (Zhang Youbo) * @ version 1.0.0 * @ since 2022-1-26-20:57 * / @ SpringBootTest (classes = ElasticsearchApplication.class) @ RunWith (SpringRunner.class) public class ElasticsearchTest {@ Autowired private ElasticsearchTemplate elasticsearchTemplate;//ES template class private ItemRepository itemRepository / / addition, deletion, modification and query of Item / * create index database * / @ Test public void testIndex () {this.elasticsearchTemplate.createIndex (Item.class); this.elasticsearchTemplate.putMapping (Item.class); / / this.elasticsearchTemplate.deleteIndex () } * insert and update public void testCreate () {Item item = new Item (1L, "Xiaomi Mobile 9", "Mobile", "Xiaomi", 3999.00, "https:www.baidu.com"); Object save = this.itemRepository.save (item); List list = new ArrayList () List.add (new Item (2L, "Nut phone R1", "Mobile", "Hammer", 3699.00, "http://image.leyou.com/123.jpg")); list.add (new Item (3L," Huawei META10 "," Mobile "," Huawei ", 4499.00," http://image.leyou.com/3.jpg")); " / / receive a collection of objects and add Iterable items = itemRepository.saveAll (list); System.out.println (items) in batch; * Delete public void testDelete () {Item item=new Item (1L, "Xiaomi Mobile 9", "Mobile", "Xiaomi", 3999.00, "https:www.baidu.com"); this.itemRepository.delete (item) * query public void testFind () {System.out.println ("- primary key query -"); Optional byId = this.itemRepository.findById (1L); System.out.println (byId.get ()); System.out.println ("- query all -"); Iterable all = this.itemRepository.findAll () All.forEach (I-> System.out.println (I)); System.out.println ("- sort query (ascending and descending order) -"); Iterable price = this.itemRepository.findAll (Sort.by ("price"). Descending (); price.forEach (System.out::println) * call custom method public void testFindByU () {List phone = this.itemRepository.findByTitle ("mobile phone"); / / phone.forEach (I-> {/ / System.out.println (I); / /}); List byPriceBetween = this.itemRepository.findByPriceBetween (4000.0, 5000.0); byPriceBetween.forEach (I-> System.out.println (I)) * bulk insert public void indexList () {list.add (new Item (1L, "Xiaomi Mobile 7", "Mobile", "Xiaomi", 3299.00, "http://image.leyou.com/13123.jpg")); list.add (new Item (2L," Nut Mobile R1 "," Mobile "," Hammer ", 3699.00," http://image.leyou.com/13123.jpg")); ") List.add (new Item (3L, "Huawei META10", "Mobile", "Huawei", 4499.00, "http://image.leyou.com/13123.jpg")); list.add (new Item (4L," Xiaomi Mix2S "," Mobile "," Mi ", 4299.00," http://image.leyou.com/13123.jpg")); " List.add (new Item (5L, "Honor V10", "Mobile", "Huawei", 2799.00, "http://image.leyou.com/13123.jpg")); itemRepository.saveAll (list)) * Advanced query public void testSearch () {/ / built through query builder tool-key points: QueryBuilders: entry, ambiguity, range MatchQueryBuilder queryBuilder= QueryBuilders.matchQuery ("title", "mobile"); / / get result set Iterable items = this.itemRepository.search (queryBuilder); items.forEach (System.out::println) * key points-Custom query public void testNative () {/ / build Custom query Builder NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder (); / / add basic query conditions queryBuilder.withQuery (QueryBuilders.matchQuery ("title", "Mobile")); / / query paging result set Page itemPage = this.itemRepository.search (queryBuilder.build ()) System.out.println (itemPage.getTotalPages ()); System.out.println (itemPage.getTotalElements ()); itemPage.forEach (I-> System.out.println (I)); * key points-pagination query public void testPage () {queryBuilder.withQuery (QueryBuilders.matchQuery ("category", "Mobile")); queryBuilder.withPageable (PageRequest.of (1)) * key points-sort public void testSort () {queryBuilder.withSort (SortBuilders.fieldSort ("price") .order (SortOrder.DESC));} the above is about "what is the code of SpringBoot integrating ElasticSearch". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to learn more about related knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.