我正在尝试使用Java连接到Elasticsearch。这是我的代码的一部分:

private static TransportClient client = null;
    public BooleanQuery() {
        try {
            client = new PreBuiltTransportClient(Settings.EMPTY);

            client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

运行该程序时,出现以下异常:
Exception in thread "main" java.lang.NoSuchMethodError: com.carrotsearch.hppc.ObjectHashSet.equals(Ljava/lang/Object;Ljava/lang/Object;)Z
    at com.carrotsearch.hppc.ObjectHashSet.add(ObjectHashSet.java:159)
    at com.carrotsearch.hppc.ObjectHashSet.addAll(ObjectHashSet.java:189)
    at com.carrotsearch.hppc.ObjectHashSet.from(ObjectHashSet.java:544)
    at org.elasticsearch.index.mapper.MapperService.<clinit>(MapperService.java:101)
    at org.elasticsearch.common.settings.IndexScopedSettings.<clinit>(IndexScopedSettings.java:60)
    at org.elasticsearch.common.settings.SettingsModule.<init>(SettingsModule.java:69)
    at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:138)
    at org.elasticsearch.client.transport.TransportClient.<init>(TransportClient.java:265)
    at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:130)
    at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:116)
    at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:106)
    at com.accenture.poc.elasticsearch.BooleanQuery.<init>(BooleanQuery.java:29)
    at com.accenture.poc.DataProcessor.main(DataProcessor.java:134)

这是我的pom.xml:
<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>

    <groupId>com.accenture</groupId>
    <artifactId>poc</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>poc</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>19.0</version>
        </dependency>
        <dependency>
            <groupId>info.debatty</groupId>
            <artifactId>java-string-similarity</artifactId>
            <version>RELEASE</version>
        </dependency>

        <!-- Dependencies of Wiki -->
                <!-- https://mvnrepository.com/artifact/org.fusesource.jdbm/jdbm -->
        <dependency>
            <groupId>org.fusesource.jdbm</groupId>
            <artifactId>jdbm</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.bluestemsoftware.open.maven.tparty</groupId>
            <artifactId>xerces-impl</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant-compress</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.7</version>
        </dependency>

        <dependency>
            <groupId>org.apache.marmotta</groupId>
            <artifactId>marmotta-loader-hbase</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>
    <!-- Elasticsearch dependency -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.6.2</version>
            </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.9.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
        <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.9.1</version>
        </dependency>
    </dependencies>

</project>

错误指向“客户端=新的PreBuiltTransportClient(Settings.EMPTY);”行。我不确定这是什么意思?我没有在网上找到与此相关的任何信息。任何帮助表示赞赏。

最佳答案

运行mvn dependency:tree -Dverbose会发现com.carrotsearch:hppc:jar有2个版本,分别为(0.4.2,0.7.1)。后者附带了elasticsearch依赖项。第一个来自org.apache.marmotta:marmotta-loader-hbase:jar

因此,我怀疑您的构建中同时存在两个版本的hppc.jar,因为到添加 flex 搜索依赖项时,您还没有执行干净操作(mvn clean)。

因此,请尝试重新构建项目,然后重试:
mvn clean package

10-07 12:21