Experimental environment
- ES version: 5.3.0
- Spring BT version: 1.5.9
First of all, of course you need to install the elastic search environment, it is best to install the visual plug-in Elasticsearch-head to enable us to visually view the data.
Of course, this section can refer to my post:
"Centos7 on Elastic search installation Pits Kee"
Https://www.jianshu.com/p/04f4d7b4a1d3
My ES is installed in the Http://113.209.119.170:9200/address (this address needs to be in the Springboot project)
Spring Project Creation
There is no special explanation for this part, but there are a few points to watch out for.
- Note that when you create a new project, check the web and NoSQL elasticsearch dependencies, and take a look at the diagram:
Dependencies that are automatically added in Pom.xml after the project is automatically generated spring-boot-starter-data-elasticsearch
:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
In this project we use the open source RESTful es java client jest
, so we also need to add dependencies in Pom.xml jest
:
<dependency> <groupId>io.searchbox</groupId> <artifactId>jest</artifactId> </dependency>
- In addition to the dependencies you must add
jna
: <dependency> <groupId>net.java.dev.jna</groupId> <artifactId>jna</artifactId> </dependency>
Otherwise the error will be reported when starting the Spring project JNA not found. native methods will be disabled.
:
- You need to configure the ES server address in the configuration file application.yml of the project to
server:port: 6325
Spring
Elasticsearch
Jest
URIs
- http://113.209.119.170:9200 # ES server's address!
read-timeout:5000
Code Organization
My project code is organized as follows:
Each part of the code is described below, comments are:
package 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; }}
package 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<Entity> entityList); List<Entity> searchEntity(String searchContent);}
Package 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_nam E). Type (Entity.type). build (); try {jestclient.execute (index); Logger.info ("ES insert Complete"); } catch (IOException e) {e.printstacktrace (); Logger.error (E.getmessage ()); }}/** * Bulk save content to ES */@Override public void saveentity (list<entity> entitylist) {bulk.b Uilder 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 insert Complete"); } catch (IOException e) {e.printstacktrace (); Logger.error (E.getmessage ()); }}/** * search for content in ES * * @Override public list<entity> searchentity (String searchcontent) {S Earchsourcebuilder 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 (Ent ity. TYPE). build (); try {jestresult result = Jestclient.execute (search); Return Result.getsourceasobjectlist (Entity.class); } catch (IOException e) {logger.error (E.getmessage ()); E.printstacktrace (); } return null; }}
Package 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.P RINTLN ("Save Interface"); if (id>0 && stringutils.isnotempty (name)) {Entity newentity = new Entity (id,name); list<entity> addlist = new arraylist<entity> (); Addlist.add (newentity); Cityesservice.saveentity (addlist); return "OK"; }else {return "bad input value"; }} @RequestMapping (value= "/search", method=requestmethod.get) public list<entity> Save (String name) { List<entity> entitylist = null; if (stringutils.isnotempty (name)) {entitylist = cityesservice.searchentity (name); } return entitylist; }}
Practical experiments
Add a few more data, you can use the Postman tool, or you can enter it directly in the browser, such as adding the following 5 pieces of data:
http://localhost:6325/entityController/save?id=1&name=南京中山陵http://localhost:6325/entityController/save?id=2&name=中国南京师范大学http://localhost:6325/entityController/save?id=3&name=南京夫子庙http://localhost:6325/entityController/save?id=4&name=杭州也非常不错http://localhost:6325/entityController/save?id=5&name=中国南边好像没有叫带京字的城市了
The data insertion effect is as follows (Elasticsearch-head viewing using the visual plugin):
Let's do a search test: for example I want to search the keyword "nanjing"
We enter in the browser:
http://localhost:6325/entityController/search?name=南京
The search results are as follows:
The four records that were inserted in the 5 records that contain the keyword "nanjing" are searched!
Of course, here is the standard word segmentation, will each Chinese as a term, which contains the "South", "Beijing" key word records are searched out, but the score is different, of course, there are some other word segmentation, at this time need other word-breaker support, here is not involved, After the article to do exploration.
The practice of Elastic search engine in Springboot