In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use springData to operate es". In daily operation, I believe many people have doubts about how to use springData to operate es. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to use springData to operate es". Next, please follow the editor to study!
Configuration file elasticsearch.yml for 1 Es
Http.cors.enabled: truehttp.cors.allow-origin: "*" network.host: 127.0.0.1
2 configure Chinese word splitter (see previous blog)
3 Import jar
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.7.RELEASE com.example demo 0.0.1-SNAPSHOT demo Demo project for Spring Boot 1.8 Org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test Org.springframework.boot spring-boot-starter-data-elasticsearch org.springframework.boot spring-boot-test Org.springframework.boot spring-boot-maven-plugin
4 write configuration file (7.3temporarily not supported by springdata)
# elasticsearch-6.5.4 needs to re-configure the ip address spring.data.elasticsearch.cluster-name=elasticsearchspring.data.elasticsearch.cluster-nodes=127.0.0.1:9300# on the configuration file to set the connection timeout spring.data.elasticsearch.properties.transport.tcp.connect_timeout=120s
5 add comments to the entity class
/ * @ author shihaifeng * @ date 2019-08-29 14:06 * @ desc (description) * * / @ Document (indexName = "book", type = "user") public class Book implements Serializable {@ Id / / primary key private Integer id; @ Field (type = FieldType.Text, analyzer = "ik_max_word") private String name; @ Field (type = FieldType.Text, analyzer = "ik_max_word") private String context @ Field (type = FieldType.Text, analyzer = "ik_smart") private String auto;}
6. Write the interface
Package com.example.demo.inter;import com.example.demo.entity.Book;import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;/** * @ author shihaifeng * @ date 2019-08-29 14:47 * @ desc (book interface-used for direct manipulation) * * Book entity object * Integer is the primary key * / public interface IBookServer extends ElasticsearchRepository {}
7 Test
Package com.example.demo;import com.example.demo.entity.Book;import com.example.demo.inter.IBookServer;import org.elasticsearch.action.search.SearchResponse;import org.elasticsearch.index.query.QueryBuilders;import org.elasticsearch.index.query.TermQueryBuilder;import org.elasticsearch.search.SearchHit;import org.elasticsearch.search.SearchHits;import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;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.Pageable;import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;import org.springframework.data.elasticsearch.core.SearchResultMapper;import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;import org.springframework.data.elasticsearch.core.query.NativeSearchQuery Import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;import org.springframework.test.context.junit4.SpringRunner;import java.util.*;;/** * @ author shihaifeng * @ date 2019-08-29 14:52 * @ desc (description) * * / @ RunWith (SpringRunner.class) @ SpringBootTestpublic class BookTest {@ Autowired private IBookServer bookServer; @ Autowired private ElasticsearchTemplate elasticsearchTemplate / * add data * / @ Test public void addBook () {/ / add a data bookServer.save (new Book (1, "title", "content", "author"); List list = new ArrayList () List.add (new Book (2), "Amitabha Buddha", "Da Bing, whose real name is Yan Bing, was born in Laiyang City, Yantai City, Shandong Province on October 23, 1980. he is a host, folk singer, writer and oil painter in mainland China. He graduated from Shandong Academy of Arts." List.add ("Big Ice") (new Book (3) "I don't", "goodwill can dissipate violence, goodwill can win, goodwill can bring karma to death, goodwill can turn back. Kindness can help people capture and build a unique sense of happiness. "I don't" is not only a kind of well-intentioned calm, but also a kind of well-intentioned introspection, "Big Bing"); list.add (4, "good, touch the head", "is written by the host Da Bing, a book by Hunan Literature and Art Publishing House that records the legendary stories of the love and warmth of Da Bing and his friends for more than a decade." , "Big Ice"); list.add (new Book (5, "they are the happiest", "the story in the book is about the wonderful growth of the protagonist Dabing for a decade, as well as ten stories about their happiness along the way, but also a tangible and silent protest against the current values. Dabing, who has a multi-habitat identity, from the host to the folk singer, from the jianghu Ranger to the senior Wen Qing, he holds a hand drumming and sings in the world, but he never writes the story on paper. " , "Big Ice") List.add (new Book (6, "Border Town", "Shen Congwen's writing style tends to romanticism, he requires the poetic effect of the novel, the integration of realism, dream recording and symbolism, and the simple language style.\ n" + "+" Shen Congwen and his works Shen Congwen and his works (5) are simple, prominent, simple and thick. Simple and vivid "," Shen Congwen ") List.add (new Book (7, A Journey to Hunan), Shen Congwen is the main performer and reflector of the rural world with special significance. He believes that beauty lies in life. Although he is in a city of hypocrisy, selfishness and indifference, he is infatuated with the beauty of human nature. List.add (8, "long River", "I don't know why I suddenly fell in love with you." Shen Congwen, a teacher, quietly wrote to his student Zhang Zhaohe, he said in his letter. " , "Shen Congwen"); bookServer.saveAll (list); / / add data in bulk} / * * query information * / @ Test public void query () {Optional book = bookServer.findById (3); System.out.println ("- findById-") System.out.println (book.get (). ToString ()); Iterable books = bookServer.findAll (); System.out.println ("- findAll-"); for (Book book1: books) {System.out.println (book1) } System.out.println ("- search QueryBuilder-"); Page search = bookServer.search (new TermQueryBuilder ("context", "Shen Congwen"), PageRequest.of (0,10); for (Book book1: search.getContent ()) {System.out.println (book1) } System.out.println ("- search SearchQuery (highlighted) -") NativeSearchQuery searchQuery = new NativeSearchQueryBuilder () .withQuery (QueryBuilders.termQuery ("context", "Shen Congwen") / / set the query content. WithHighlightFields (new HighlightBuilder.Field ("context"). PreTags ("). PostTags ("). Build () / sets the highlight .setPageable (PageRequest.of (0,10)) / / set paging information AggregatedPage page = elasticsearchTemplate.queryForPage (searchQuery, Book.class, new SearchResultMapper () {@ Override public AggregatedPage mapResults (SearchResponse searchResponse, Class aClass, Pageable pageable) {/ / return result set remapping List list = new ArrayList (); SearchHits hits = searchResponse.getHits (); Iterator iterator = hits.iterator () While (iterator.hasNext ()) {Book book = new Book (); SearchHit searchHit = iterator.next (); Map source = searchHit.getSourceAsMap (); book.setId (source.get ("id"). ToString ()) / / set id book.setName (source.get ("name") .toString ()); / / set the book title book.setAuto (source.get ("auto") .toString ()); / / set the author Map highlightFields = searchHit.getHighlightFields (); / / get the highlighted column book.setContext (highlightFields.get ("context") .toString ()) / / sets the highlighted column System.out.println ("highlighted value is =" + highlightFields.get ("context")); list.add (book);} / / returns the result of the query return new AggregatedPageImpl (list);}}); List content = page.getContent () System.out.println ("Total number found is =" + content.size ();} / * * with new data * / @ Test public void update () {bookServer.save (new Book (1, "title 3", "Book content 3", "author 3")) } / * Delete data * / @ Test public void delete () {bookServer.delete (new Book (1, "title 3", "content 3", "author 3"));}} at this point, the study on "how to use springData to operate es" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.