SpringBoot使用原生ElasticSearch client报错 elasticsearch6.6

曾经终败给现在 2024-03-24 14:03 173阅读 0赞

SpringBoot集成原生elasticsearch client创建索引时报错

错误信息:

  1. Description:
  2. An attempt was made to call the method org.elasticsearch.search.fetch.subphase.highlight.HighlightPhase.<init>(Ljava/util/Map;)V but it does not exist. Its class, org.elasticsearch.search.fetch.subphase.highlight.HighlightPhase, is available from the following locations:
  3. jar:file:/D:/apache-maven-3.5.4/repository/org/elasticsearch/elasticsearch/6.4.3/elasticsearch-6.4.3.jar!/org/elasticsearch/search/fetch/subphase/highlight/HighlightPhase.class
  4. It was loaded from the following location:
  5. file:/D:/apache-maven-3.5.4/repository/org/elasticsearch/elasticsearch/6.4.3/elasticsearch-6.4.3.jar
  6. Action:
  7. Correct the classpath of your application so that it contains a single, compatible version of org.elasticsearch.search.fetch.subphase.highlight.HighlightPhase

rg.elasticsearch.search.fetch.subphase.highlight.HighlightPhase.(Ljava/util/Map;)V but it does not exist.

大致意思是有一个HighlightPhase方法不存在 方法是从6.4.3中加载,但是本工程中使用的是6.6.0版本的es

最后的解决方案:在pom.xml中额外添加了6.4.3版本的依赖

  1. <dependency>
  2. <groupId>org.elasticsearch.client</groupId>
  3. <artifactId>transport</artifactId>
  4. <version>6.6.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.elasticsearch.client</groupId>
  8. <artifactId>transport</artifactId>
  9. <version>6.4.3</version>
  10. </dependency>

单独使用elasticsearch client时创建索引

未集成springboot时成功创建了索引,并没有报错

代码:

maven配置 参考自es官网

  1. <dependency>
  2. <groupId>org.elasticsearch.client</groupId>
  3. <artifactId>transport</artifactId>
  4. <version>6.6.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.elasticsearch.client</groupId>
  8. <artifactId>transport</artifactId>
  9. <version>6.4.3</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.apache.logging.log4j</groupId>
  13. <artifactId>log4j-core</artifactId>
  14. <version>2.11.1</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.apache.logging.log4j</groupId>
  18. <artifactId>log4j-api</artifactId>
  19. <version>2.11.1</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.apache.logging.log4j</groupId>
  23. <artifactId>log4j-to-slf4j</artifactId>
  24. <version>2.11.1</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.slf4j</groupId>
  28. <artifactId>slf4j-api</artifactId>
  29. <version>1.7.24</version>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.slf4j</groupId>
  33. <artifactId>slf4j-simple</artifactId>
  34. <version>1.7.21</version>
  35. </dependency>

java代码:

  1. import org.elasticsearch.client.transport.TransportClient;
  2. import org.elasticsearch.common.settings.Settings;
  3. import org.elasticsearch.common.transport.TransportAddress;
  4. import org.elasticsearch.common.xcontent.XContentBuilder;
  5. import org.elasticsearch.common.xcontent.XContentFactory;
  6. import org.elasticsearch.transport.client.PreBuiltTransportClient;
  7. import org.junit.Test;
  8. import java.io.IOException;
  9. import java.net.InetAddress;
  10. /**
  11. * @program: esclienttes
  12. * @Date: 2019.4.11 下午 2:09
  13. * @Author: MicoMecy
  14. */
  15. public class CreateTest {
  16. @Test
  17. public void test() throws IOException {
  18. Settings settings = Settings.builder()
  19. .put("cluster.name", "my-elasticsearch").build();
  20. //Add transport addresses and do something with the client...
  21. // on startup
  22. TransportClient client = new PreBuiltTransportClient(settings)
  23. // .addTransportAddress(new TransportAddress(InetAddress.getByName(""), 9300))
  24. .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));
  25. //创建索引
  26. client.admin().indices().prepareCreate("bolg1").get();
  27. /*
  28. {
  29. 创建出的索引结构如下
  30. "article": {
  31. "properties": {
  32. "id":{
  33. "type":"long",
  34. "store":true
  35. },
  36. "title":{
  37. "type":"text",
  38. "store":true,
  39. "index":true,
  40. "analyzer":"ik_smart"
  41. },
  42. "content":{
  43. "type":"text",
  44. "store":true,
  45. "index":true,
  46. "analyzer":"ik_smart"
  47. }
  48. }
  49. }
  50. }
  51. */
  52. // 拼装JSON
  53. XContentBuilder builder = XContentFactory.jsonBuilder();
  54. builder.startObject() //{
  55. .startObject("article") // "article": {
  56. .startObject("properties")
  57. .startObject("id")
  58. .field("type", "long")
  59. .field("store", true)
  60. .endObject()//}
  61. .startObject("title")
  62. .field("type", "text")
  63. .field("store", true)
  64. .field("index", true)
  65. .field("analyzer", "ik_smart")
  66. .endObject()
  67. .startObject("content")
  68. .field("type", "text")
  69. .field("store", true)
  70. .field("index", true)
  71. .field("analyzer", "ik_smart")
  72. .endObject()
  73. .endObject()
  74. .endObject()
  75. .endObject();
  76. //添加映射
  77. client.admin().indices()
  78. .preparePutMapping("bolg1") //准备添加映射=>指定映射所在索引名称
  79. .setType("article")//指定类型名称
  80. .setSource(builder) //添加映射内容
  81. .get();
  82. //删除索引
  83. // client.admin().indices().prepareDelete("helll").get();
  84. // on shutdown
  85. client.close();
  86. }
  87. }

Springboot集成elasticsearch client

pom.xml 含有mybatis相关依赖,可自行删除

  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>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.3.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.transcend</groupId>
  12. <artifactId>es</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>es</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.mybatis.spring.boot</groupId>
  30. <artifactId>mybatis-spring-boot-starter</artifactId>
  31. <version>2.0.0</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-jdbc</artifactId>
  36. </dependency>
  37. <dependency>
  38. <groupId>mysql</groupId>
  39. <artifactId>mysql-connector-java</artifactId>
  40. <scope>runtime</scope>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.springframework.boot</groupId>
  44. <artifactId>spring-boot-starter-test</artifactId>
  45. <scope>test</scope>
  46. </dependency>
  47. <dependency>
  48. <groupId>junit</groupId>
  49. <artifactId>junit</artifactId>
  50. <version>4.12</version>
  51. </dependency>
  52. <!--原生es java api-->
  53. <dependency>
  54. <groupId>org.elasticsearch.client</groupId>
  55. <artifactId>transport</artifactId>
  56. <version>6.6.0</version>
  57. </dependency>
  58. <dependency>
  59. <groupId>org.elasticsearch.client</groupId>
  60. <artifactId>transport</artifactId>
  61. <version>6.4.3</version>
  62. </dependency>
  63. <dependency>
  64. <groupId>org.apache.logging.log4j</groupId>
  65. <artifactId>log4j-core</artifactId>
  66. <version>2.11.1</version>
  67. </dependency>
  68. <dependency>
  69. <groupId>org.apache.logging.log4j</groupId>
  70. <artifactId>log4j-api</artifactId>
  71. <version>2.11.1</version>
  72. </dependency>
  73. <dependency>
  74. <groupId>org.apache.logging.log4j</groupId>
  75. <artifactId>log4j-to-slf4j</artifactId>
  76. <version>2.11.1</version>
  77. </dependency>
  78. <dependency>
  79. <groupId>org.slf4j</groupId>
  80. <artifactId>slf4j-api</artifactId>
  81. <version>1.7.24</version>
  82. </dependency>
  83. <dependency>
  84. <groupId>org.slf4j</groupId>
  85. <artifactId>slf4j-simple</artifactId>
  86. <version>1.7.21</version>
  87. </dependency>
  88. </dependencies>
  89. <build>
  90. <plugins>
  91. <plugin>
  92. <groupId>org.springframework.boot</groupId>
  93. <artifactId>spring-boot-maven-plugin</artifactId>
  94. </plugin>
  95. </plugins>
  96. </build>
  97. </project>

将TransportClient交由spring管理

  1. ElasticSearchConfig.java
  2. package com.transcend.config;
  3. import org.elasticsearch.client.transport.TransportClient;
  4. import org.elasticsearch.common.settings.Settings;
  5. import org.elasticsearch.common.transport.TransportAddress;
  6. import org.elasticsearch.transport.client.PreBuiltTransportClient;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import java.net.InetAddress;
  10. import java.net.UnknownHostException;
  11. /**
  12. * @program: es
  13. * @Date: 2019.4.12 下午 3:24
  14. * @Author: MicoMecy
  15. */
  16. @Configuration
  17. public class ElasticSearchConfig {
  18. @Bean(name = "client")
  19. public TransportClient getClient() {
  20. TransportAddress node = null;
  21. try {
  22. node = new TransportAddress(InetAddress.getByName("localhost"),9300);
  23. } catch (UnknownHostException e) {
  24. e.printStackTrace();
  25. }
  26. Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();
  27. TransportClient client = new PreBuiltTransportClient(settings)
  28. .addTransportAddress(node);
  29. return client;
  30. }
  31. }

SpringBootTest

  1. package com.transcend.createindex;
  2. import com.transcend.db.dao.CstableMapper;
  3. import com.transcend.db.entity.Cstable;
  4. import org.elasticsearch.client.transport.TransportClient;
  5. import org.elasticsearch.common.xcontent.XContentBuilder;
  6. import org.elasticsearch.common.xcontent.XContentFactory;
  7. import org.junit.Test;
  8. import org.junit.runner.RunWith;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.boot.test.context.SpringBootTest;
  11. import org.springframework.test.context.junit4.SpringRunner;
  12. import java.io.IOException;
  13. import java.util.List;
  14. /**
  15. * @program: es
  16. * @Date: 2019.4.11 下午 3:47
  17. * @Author: Haeyden
  18. * 创建索引分级
  19. */
  20. @RunWith(SpringRunner.class)
  21. @SpringBootTest
  22. public class CreaetIndexGrading {
  23. @Autowired
  24. private TransportClient client;
  25. @Test
  26. public void createIndexUseClient() throws IOException {
  27. client.admin().indices().prepareCreate("bolg1").get();
  28. // 拼装JSON
  29. XContentBuilder builder = XContentFactory.jsonBuilder();
  30. builder.startObject() //{
  31. .startObject("article") // "article": {
  32. .startObject("properties")
  33. .startObject("id")
  34. .field("type", "long")
  35. .field("store", true)
  36. .endObject()//}
  37. .startObject("title")
  38. .field("type", "text")
  39. .field("store", true)
  40. .field("index", true)
  41. .field("analyzer", "ik_smart")
  42. .endObject()
  43. .startObject("content")
  44. .field("type", "text")
  45. .field("store", true)
  46. .field("index", true)
  47. .field("analyzer", "ik_smart")
  48. .endObject()
  49. .endObject()
  50. .endObject()
  51. .endObject();
  52. //添加映射
  53. client.admin().indices()
  54. .preparePutMapping("bolg1") //准备添加映射=>指定映射所在索引名称
  55. .setType("article")//指定类型名称
  56. .setSource(builder) //添加映射内容
  57. .get();
  58. //删除索引
  59. // client.admin().indices().prepareDelete("helll").get();
  60. // on shutdown
  61. client.close();
  62. }
  63. }

发表评论

表情:
评论列表 (有 0 条评论,173人围观)

还没有评论,来说两句吧...

相关阅读