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

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

错误信息:

Description:

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:

    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

It was loaded from the following location:

    file:/D:/apache-maven-3.5.4/repository/org/elasticsearch/elasticsearch/6.4.3/elasticsearch-6.4.3.jar


Action:

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.<init>(Ljava/util/Map;)V but it does not exist.

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

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

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>6.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>6.4.3</version>
        </dependency>

单独使用elasticsearch client时创建索引

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

代码:

maven配置 参考自es官网

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>6.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>6.4.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.11.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.11.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-to-slf4j</artifactId>
            <version>2.11.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.24</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.21</version>
        </dependency>

java代码

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import java.io.IOException;
import java.net.InetAddress;

/**
 * @program: esclienttes
 * @Date: 2019.4.11 下午 2:09
 * @Author: MicoMecy
 */
public class CreateTest {
    @Test
    public void test() throws IOException {
        Settings settings = Settings.builder()
            .put("cluster.name", "my-elasticsearch").build();
        //Add transport addresses and do something with the client...
        // on startup
        TransportClient client = new PreBuiltTransportClient(settings)
//            .addTransportAddress(new TransportAddress(InetAddress.getByName(""), 9300))
            .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));
        //创建索引
        client.admin().indices().prepareCreate("bolg1").get();


        /*
      {
           创建出的索引结构如下
          "article": {
                  "properties": {
                      "id":{
                       "type":"long",
                       "store":true
                      },
                      "title":{
                       "type":"text",
                       "store":true,
                       "index":true,
                       "analyzer":"ik_smart"
                      },
                      "content":{
                       "type":"text",
                       "store":true,
                       "index":true,
                       "analyzer":"ik_smart"
                      }
                  }
              }
        }
 */
//        拼装JSON
        XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.startObject() //{
            .startObject("article") // "article": {
            .startObject("properties")
            .startObject("id")
            .field("type", "long")
            .field("store", true)
            .endObject()//}
            .startObject("title")
            .field("type", "text")
            .field("store", true)
            .field("index", true)
            .field("analyzer", "ik_smart")
            .endObject()
            .startObject("content")
            .field("type", "text")
            .field("store", true)
            .field("index", true)
            .field("analyzer", "ik_smart")
            .endObject()
            .endObject()
            .endObject()
            .endObject();

        //添加映射
        client.admin().indices()
            .preparePutMapping("bolg1") //准备添加映射=>指定映射所在索引名称
            .setType("article")//指定类型名称
            .setSource(builder) //添加映射内容
            .get();

        //删除索引
//        client.admin().indices().prepareDelete("helll").get();

        // on shutdown
        client.close();
    }
}

Springboot集成elasticsearch client

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

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.transcend</groupId>
    <artifactId>es</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>es</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <!--原生es java api-->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>6.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>6.4.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.11.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.11.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-to-slf4j</artifactId>
            <version>2.11.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.24</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.21</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

将TransportClient交由spring管理

ElasticSearchConfig.java
package com.transcend.config;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @program: es
 * @Date: 2019.4.12 下午 3:24
 * @Author: MicoMecy
 */
@Configuration
public class ElasticSearchConfig {
    @Bean(name = "client")
    public TransportClient getClient() {
        TransportAddress node = null;
        try {
            node = new TransportAddress(InetAddress.getByName("localhost"),9300);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();

        TransportClient client = new PreBuiltTransportClient(settings)
            .addTransportAddress(node);
        return client;
    }
}

SpringBootTest

package com.transcend.createindex;

import com.transcend.db.dao.CstableMapper;
import com.transcend.db.entity.Cstable;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
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.List;

/**
 * @program: es
 * @Date: 2019.4.11 下午 3:47
 * @Author: Haeyden
 * 创建索引分级
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class CreaetIndexGrading {

    @Autowired
    private TransportClient client;
    @Test
    public void createIndexUseClient() throws IOException {
        client.admin().indices().prepareCreate("bolg1").get();

//        拼装JSON
        XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.startObject() //{
            .startObject("article") // "article": {
            .startObject("properties")
            .startObject("id")
            .field("type", "long")
            .field("store", true)
            .endObject()//}
            .startObject("title")
            .field("type", "text")
            .field("store", true)
            .field("index", true)
            .field("analyzer", "ik_smart")
            .endObject()
            .startObject("content")
            .field("type", "text")
            .field("store", true)
            .field("index", true)
            .field("analyzer", "ik_smart")
            .endObject()
            .endObject()
            .endObject()
            .endObject();

        //添加映射
        client.admin().indices()
            .preparePutMapping("bolg1") //准备添加映射=>指定映射所在索引名称
            .setType("article")//指定类型名称
            .setSource(builder) //添加映射内容
            .get();

        //删除索引
//        client.admin().indices().prepareDelete("helll").get();

        // on shutdown
        client.close();
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值