搭建模块
创建二个项目
gmall-list-service的appliction.properties:
server.port=8073
spring.datasource.url=jdbc:mysql://localhost:3306/gmall?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
spring.dubbo.registry.protocol=zookeeper
spring.dubbo.registry.address=192.168.0.100:2181
spring.dubbo.application.name=gmall-list-service
spring.dubbo.protocol.name=dubbo
spring.dubbo.base-package=com.javawxid
spring.elasticsearch.jest.uris=http://192.168.0.100:9200
logging.level.root=info
gmall-list-service的pom.xml:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.javawxid.gmall
gmall-parent
1.0-SNAPSHOT
com.javawxid.gmall
gmall-list-service
0.0.1-SNAPSHOT
gmall-list-service
Demo project for Spring Boot
1.8
com.javawxid.gmall
gmall-service-util
1.0-SNAPSHOT
com.javawxid.gmall
gmall-api
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-data-elasticsearch
io.searchbox
jest
5.3.3
net.java.dev.jna
jna
4.5.1
org.springframework.boot
spring-boot-maven-plugin
其中jest和jna请将版本号,部分纳入gmall-parent中管理。spring-boot-starter-data-elasticsearch不用管理版本号,其版本跟随springboot的1.5.10大版本号。
gmall-list-web的appliction.properties:
server.port=8083
spring.dubbo.registry.address=192.168.0.100:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.application.name=gmall-list-web
spring.dubbo.base-package=com.javawxid
spring.dubbo.consumer.timeout=600000
spring.dubbo.consumer.check=false
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.cache=false
logging.level.root=info
gmall-list-web的pom.xml:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.javawxid.gmall
gmall-parent
1.0-SNAPSHOT
com.javawxid.gmall
gmall-list-web
0.0.1-SNAPSHOT
gmall-list-web
Demo project for Spring Boot
1.8
com.javawxid.gmall
gmall-api
1.0-SNAPSHOT
com.javawxid.gmall
gmall-web-util
1.0-SNAPSHOT
org.springframework.boot
spring-boot-maven-plugin
2、 关于es的java 客户端的选择
目前市面上有两类客户端
一类是TransportClient 为代表的ES原生客户端,不能执行原生dsl语句必须使用它的Java api方法。
另外一种是以Rest Api为主的missing client,最典型的就是jest。 这种客户端可以直接使用dsl语句拼成的字符串,直接传给服务端,然后返回json字符串再解析。
两种方式各有优劣,但是最近elasticsearch官网,宣布计划在7.0以后的版本中废除TransportClient。以RestClient为主。
所以在官方的RestClient 基础上,进行了简单包装的Jest客户端,就成了首选,而且该客户端也与springboot完美集成。
3、插入数据
PUT /movie_chn/movie/1
{ "id":1,
"name":"红海行动",
"doubanScore":8.5,
"actorList":[
{"id":1,"name":"张译"},
{"id":2,"name":"海清"},
{"id":3,"name":"张涵予"}
]
}
PUT /movie_chn/movie/2
{
"id":2,
"name":"湄公河行动",
"doubanScore":8.0,
"actorList":[
{"id":3,"name":"张涵予"}
]
}
PUT /movie_chn/movie/3
{
"id":3,
"name":"红海事件",
"doubanScore":5.0,
"actorList":[
{"id":4,"name":"张晨"}
]
}
4、在测试类中测试ES
package com.javawxid;
import io.searchbox.client.JestClient;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
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.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class GmallListServiceApplicationTests {
@Autowired
JestClient jestClient;
@Test
public void testEs() throws IOException {
String query="{\n" +
" \"query\": {\n" +
" \"match\": {\n" +
" \"actorList.name\": \"张译\"\n" +
" }\n" +
" }\n" +
"}";
Search search = new Search.Builder(query).addIndex("movie_chn").addType("movie").build();
SearchResult result = jestClient.execute(search);
List> hits = result.getHits(HashMap.class);
for (SearchResult.Hit hit : hits) {
HashMap source = hit.source;
System.err.println("source = " + source);
}
}
}
打印结果:
source = {doubanScore=8.5, es_metadata_id=1, name=红海行动, actorList=[{id=1.0, name=张译}, {id=2.0, name=海清}, {id=3.0, name=张涵予}], id=1.0}
source = {doubanScore=5.0, es_metadata_id=3, name=红海事件, actorList=[{id=4.0, name=张晨}], id=3.0}
source = {doubanScore=8.0, es_metadata_id=2, name=湄公河行动, actorList=[{id=3.0, name=张涵予}], id=2.0}
以上技术方面的准备就做好了。