java - Maven 中的 Elasticsearch "Client only"依赖项

标签 java elasticsearch

我们需要将一个 elasticsearch 客户端包含到一个项目中,该项目本身使用 Lucene 来执行其他索引/存储任务。包含整个库会导致依赖冲突,因为 Lucene 版本不一样(ES 使用 4.7,我们使用 4.0)。 是否有任何“仅限客户端”的 elasticsearch 包或任何人都可以想到任何其他解决方案?

编辑:

排除所有 lucene 包的方法导致以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/lucene/util/Version
    at org.elasticsearch.Version.<clinit>(Version.java:42)
    at org.elasticsearch.client.transport.TransportClient.<init>(TransportClient.java:169)
    at org.elasticsearch.client.transport.TransportClient.<init>(TransportClient.java:125)
    at de.tensquare.wpt.entrysearchindex.ElasticSearchClient.<init>(ElasticSearchClient.java:74)
    at de.tensquare.wpt.entrysearchindex.SearchIndex.<init>(SearchIndex.java:81)
    at de.tensquare.wpt.entrysearchindex.SearchIndex.main(SearchIndex.java:152)
Caused by: java.lang.ClassNotFoundException: org.apache.lucene.util.Version
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

最佳答案

我最近遇到了类似的问题。我们的堆栈中同时包含 solr 和 elasticsearch。使用 elasticserch 传输客户端,我们遇到了 lucence snafus。这是帮助我的解决方法。我创建了一个阴影 jar,它改变了 elasticsearch 引用 lucene 和 carrot 搜索包的方式。

编辑:我在另一个项目中遇到了问题,因此需要进一步调整,更新解决方案

elasticsearch 包装器项目 pom

<?xml version="1.0"?>
<project .>
    <parent>
        <artifactId>someartifactId</artifactId>
        <groupId>some.group.id</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>someartifactId-elasticsearch</artifactId>
    <name>Some Artifact Elasticsearch</name>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>2.1.2</version>
        </dependency>
    </dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/services/org.apache.lucene.codecs.Codec</exclude>
                            <exclude>META-INF/services/org.apache.lucene.codecs.DocValuesFormat</exclude>
                            <exclude>META-INF/services/org.apache.lucene.codecs.PostingsFormat</exclude>
                            <exclude>META-INF/services/org.apache.lucene.analysis.util.CharFilterFactory</exclude>
                            <exclude>META-INF/services/org.apache.lucene.analysis.util.TokenFilterFactory</exclude>
                            <exclude>META-INF/services/org.apache.lucene.analysis.util.TokenizerFactory</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <relocations>
                            <relocation>
                                <pattern>org.</pattern>
                                <shadedPattern>hack.org.</shadedPattern>
                            </relocation>
                            <relocation>
                                <pattern>com.</pattern>
                                <shadedPattern>hack.com.</shadedPattern>
                            </relocation>
                        </relocations>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>

            </configuration>
        </plugin>
    </plugins>
</build>
</project>

在 src/main/resources/META-INF/services 中,创建被打包的着色过滤器和编解码器

hack.org.apache.lucene.analysis.util.CharFilterFactory
hack.org.apache.lucene.analysis.util.TokenFilterFactory
hack.org.apache.lucene.codecs.Codec
hack.org.apache.lucene.codecs.DocValuesFormat
hack.org.apache.lucene.codecs.PostingsFormat
hack.org.apache.lucene.analysis.util.TokenizerFactory

现在要使用 TransportClient(或任何其他 Elasticsearch 类)使用包 hack.org.elasticsearch....

关于java - Maven 中的 Elasticsearch "Client only"依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23627769/

相关文章:

java - “找不到或加载主类”是什么意思?

node.js - Firebase Flashlight(ElasticSearch)过滤,排序,分页

elasticsearch - 如何删除elasticsearch中的文档类型?

elasticsearch - 支持无痛语言的最小版本的kibana是什么

java - 检查 Activity 互联网连接 Android

JAVA : Strurggling with connecting my class methods to my main

java - 如何禁用对话框中的 X?

java - Elasticsearch 查询不返回任何内容

elasticsearch - Elasticsearch 中的 Date_Histogram

java - 如何使用Java下载远程文件