In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Experimental environment ES version: 5.3.0spring bt version: 1.5.9
First of all, of course, we need to install the elasticsearch environment, and it is best to install the visualization plug-in elasticsearch-head to make it easier for us to view the data visually.
Of course, you can refer to my post in this part:
"filling holes in elastic search installation on centos7"
Https://www.jianshu.com/p/04f4d7b4a1d3
My ES is installed at the http://113.209.119.170:9200/ address (this address needs to be assigned to the springboot project)
Spring project creation
There is nothing special to explain in this part, but there are a few points to be careful.
Note that when creating a new project, remember to check the Elasticsearch dependency in web and NoSQL, and take a diagram to illustrate it:
Spring-boot-starter-data-elasticsearch dependencies are automatically added to pom.xml after the project is automatically generated:
Org.springframework.boot spring-boot-starter-data-elasticsearch
In this project, we use the open source restful-based es java client jest, so we also need to add jest dependencies to pom.xml:
In addition, io.searchbox jest must add a dependency on jna: net.java.dev.jna jna
Otherwise, JNA not found will be reported when you start the spring project. Native methods will be disabled. Error of:
The configuration file application.yml of the project needs to configure the es server address to server:port: 6325
Spring:
Elasticsearch:
Jest:
Uris:
Address of the http://113.209.119.170:9200 # ES server!
Read-timeout: 5000 Code Organization
My project code is organized as follows:
Each part of the code is explained in detail as follows, with comments as follows:
Entity.javapackage com.hansonwang99.springboot_es_demo.entity;import java.io.Serializable;import org.springframework.data.elasticsearch.annotations.Document;public class Entity implements Serializable {private static final long serialVersionUID =-763638353551774166L; public static final String INDEX_NAME = "index_entity"; public static final String TYPE = "tstype"; private Long id; private String name; public Entity () {super () } public Entity (Long id, String name) {this.id = id; this.name = name;} public Long getId () {return id;} public void setId (Long id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name }} TestService.javapackage com.hansonwang99.springboot_es_demo.service;import com.hansonwang99.springboot_es_demo.entity.Entity;import java.util.List;public interface TestService {void saveEntity (Entity entity); void saveEntity (List entityList); List searchEntity (String searchContent);} TestServiceImpl.javapackage com.hansonwang99.springboot_es_demo.service.impl;import java.io.IOException;import java.util.List;import com.hansonwang99.springboot_es_demo.entity.Entity Import com.hansonwang99.springboot_es_demo.service.TestService;import org.elasticsearch.index.query.QueryBuilders;import org.elasticsearch.search.builder.SearchSourceBuilder;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import io.searchbox.client.JestClient;import io.searchbox.client.JestResult;import io.searchbox.core.Bulk;import io.searchbox.core.Index;import io.searchbox.core.Search @ Servicepublic class TestServiceImpl implements TestService {private static final Logger LOGGER = LoggerFactory.getLogger (TestServiceImpl.class); @ Autowired private JestClient jestClient; @ Override public void saveEntity (Entity entity) {Index index = new Index.Builder (entity) .index (Entity.INDEX_NAME) .type (Entity.TYPE) .build (); try {jestClient.execute (index); LOGGER.info ("ES insertion complete") } catch (IOException e) {e.printStackTrace (); LOGGER.error (e.getMessage ());}} / * batch save content to ES * / @ Override public void saveEntity (List entityList) {Bulk.Builder bulk = new Bulk.Builder () For (Entity entity: entityList) {Index index = new Index.Builder (entity) .index (Entity.INDEX_NAME) .type (Entity.TYPE) .build (); bulk.addAction (index);} try {jestClient.execute (bulk.build ()); LOGGER.info ("ES insertion complete") } catch (IOException e) {e.printStackTrace (); LOGGER.error (e.getMessage ());}} / * search for content in ES * / @ Override public List searchEntity (String searchContent) {SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder (); / / searchSourceBuilder.query (QueryBuilders.queryStringQuery (searchContent)); / / searchSourceBuilder.field ("name") SearchSourceBuilder.query (QueryBuilders.matchQuery ("name", searchContent)); Search search = new Search.Builder (searchSourceBuilder.toString ()) .addIndex (Entity.INDEX_NAME) .addType (Entity.TYPE). Build (); try {JestResult result = jestClient.execute (search); return result.getSourceAsObjectList (Entity.class) } catch (IOException e) {LOGGER.error (e.getMessage ()); e.printStackTrace ();} return null;}} EntityController.javapackage com.hansonwang99.springboot_es_demo.controller;import java.util.ArrayList;import java.util.List;import com.hansonwang99.springboot_es_demo.entity.Entity;import com.hansonwang99.springboot_es_demo.service.TestService;import org.apache.commons.lang.StringUtils Import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping ("/ entityController") public class EntityController {@ Autowired TestService cityESService; @ RequestMapping (value= "/ save", method=RequestMethod.GET) public String save (long id, String name) {System.out.println ("save Interface") If (id > 0 & & StringUtils.isNotEmpty (name)) {Entity newEntity = newEntity (id,name); List addList = new ArrayList (); addList.add (newEntity); cityESService.saveEntity (addList); return "OK";} else {return "Bad input value" } @ RequestMapping (value= "/ search", method=RequestMethod.GET) public List save (String name) {List entityList = null; if (StringUtils.isNotEmpty (name)) {entityList = cityESService.searchEntity (name);} return entityList;}} actual experiment
To add a few pieces of data, you can use the postman tool or enter them directly in the browser, such as the following 5 pieces of data:
Http://localhost:6325/entityController/save?id=1&name= Nanjing Sun Yat-sen Mausoleum http://localhost:6325/entityController/save?id=2&name= China Nanjing normal University http://localhost:6325/entityController/save?id=3&name= Nanjing Confucius Temple http://localhost:6325/entityController/save?id=4&name= Hangzhou is also very good http://localhost:6325/entityController/save?id=5&name= there seems to be no city with the Beijing character in the south of China.
The effect of data insertion is as follows (watch by using the visualization plug-in elasticsearch-head):
Let's do a search test: for example, I want to search for the keyword "Nanjing".
We type in the browser:
Http://localhost:6325/entityController/search?name= Nanjing
The search results are as follows:
The four records containing the keyword "Nanjing" in the 5 records just inserted have been searched out!
Of course, here is the standard word segmentation, each Chinese as a term, all the records containing the "south", "Beijing" keywords have been searched out, but the score is different, of course, there are some other word segmentation ways, at this time need the support of other word segmentation plug-ins, not covered here, later to explore.
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.