全文检索工具elasticsearch:第三章: Java程序中的应用

搭建模块

 







创建二个项目

 










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:

   

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>com.javawxid.gmall</groupId>
  7. <artifactId>gmall-parent</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <groupId>com.javawxid.gmall</groupId>
  11. <artifactId>gmall-list-service</artifactId>
  12. <version>0.0.1-SNAPSHOT</version>
  13. <name>gmall-list-service</name>
  14. <description>Demo project for Spring Boot</description>
  15. <properties>
  16. <java.version>1.8</java.version>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>com.javawxid.gmall</groupId>
  21. <artifactId>gmall-service-util</artifactId>
  22. <version>1.0-SNAPSHOT</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>com.javawxid.gmall</groupId>
  26. <artifactId>gmall-api</artifactId>
  27. <version>1.0-SNAPSHOT</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  32. </dependency>
  33. <!-- 导入Jest依赖 -->
  34. <!-- https://mvnrepository.com/artifact/io.searchbox/jest -->
  35. <dependency>
  36. <groupId>io.searchbox</groupId>
  37. <artifactId>jest</artifactId>
  38. <version>5.3.3</version>
  39. </dependency>
  40. <!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna -->
  41. <dependency>
  42. <groupId>net.java.dev.jna</groupId>
  43. <artifactId>jna</artifactId>
  44. <version>4.5.1</version>
  45. </dependency>
  46. </dependencies>
  47. <build>
  48. <plugins>
  49. <plugin>
  50. <groupId>org.springframework.boot</groupId>
  51. <artifactId>spring-boot-maven-plugin</artifactId>
  52. </plugin>
  53. </plugins>
  54. </build>
  55. </project>

其中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:

  

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>com.javawxid.gmall</groupId>
  7. <artifactId>gmall-parent</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <groupId>com.javawxid.gmall</groupId>
  11. <artifactId>gmall-list-web</artifactId>
  12. <version>0.0.1-SNAPSHOT</version>
  13. <name>gmall-list-web</name>
  14. <description>Demo project for Spring Boot</description>
  15. <properties>
  16. <java.version>1.8</java.version>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>com.javawxid.gmall</groupId>
  21. <artifactId>gmall-api</artifactId>
  22. <version>1.0-SNAPSHOT</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>com.javawxid.gmall</groupId>
  26. <artifactId>gmall-web-util</artifactId>
  27. <version>1.0-SNAPSHOT</version>
  28. </dependency>
  29. </dependencies>
  30. <build>
  31. <plugins>
  32. <plugin>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-maven-plugin</artifactId>
  35. </plugin>
  36. </plugins>
  37. </build>
  38. </project>

 
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<SearchResult.Hit<HashMap, Void>> hits = result.getHits(HashMap.class);
     
            for (SearchResult.Hit<HashMap, Void> 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}

以上技术方面的准备就做好了。