8

我在我的应用程序中使用 Lucene 4 并且不想更改它。我正在尝试集成将 Lucene 3.5 捆绑为 IndexProvider 实现的 Neo4J,neo4j-lucene-index。

不幸的是,neo4j-lucene-index 不起作用,并且排除了该依赖项,该应用程序在启动时会无限期挂起。我已经尝试过 neo4j-lucene4-index ,但似乎维护得不是很好,需要进行相当大的更新才能与 Neo4J 1.9.1 一起使用。这些变化超出了我对 Neo4J 内部结构的理解。

但是,我可以看到 IndexProviders 是可插拔的,所以我希望有一个现有的 Lucene 替代品——不过我现在找不到它。谁能指出我正确的方向?

Lucene 4 出来这么久了,Neo4J 还不支持它,这似乎很奇怪。我错过了什么吗?

目前,对于我的 Neo4J 配置,我的 POM 看起来像这样:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-neo4j</artifactId>
    <version>2.2.1.RELEASE</version>
    <exclusions>
        <exclusion>
        <artifactId>neo4j</artifactId>
        <groupId>org.neo4j</groupId>
        </exclusion>
        <exclusion>
        <artifactId>neo4j-cypher</artifactId>
        <groupId>org.neo4j</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.neo4j</groupId>
    <artifactId>neo4j-kernel</artifactId>
    <version>1.9.1</version>
    <exclusion>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j-lucene-index</artifactId>
    </exclusion>
</dependency>

<dependency>
    <groupId>org.neo4j.app</groupId>
    <artifactId>neo4j-server</artifactId>
    <version>1.9.1</version>
    <exclusions>
        <exclusion>
        <artifactId>neo4j</artifactId>
        <groupId>org.neo4j</groupId>
        </exclusion>
        <exclusion>
        <artifactId>neo4j-cypher</artifactId>
        <groupId>org.neo4j</groupId>
        </exclusion>
        <exclusion>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j-lucene-index</artifactId>
        </exclusion>
    </exclusions>
</dependency>

    <!-- A temporary dependency until Neo4J builds in support for Lucene 4. 
    Looks like they're planning to incorporate this project anyway This project 
    is available on GitHub, and needs to be built with: mvn license:format mvn 
    install to install into your local repo. 
        <dependency>
            <groupId>com.keatext</groupId>
            <artifactId>neo4j-lucene4-index</artifactId>
            <version>1.9.M01-SNAPSHOT</version>
        </dependency>-->

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.0.1.Final</version>
</dependency>
4

3 回答 3

1

There have been some changes internally from 1.8 -> 1.9. In short, a index provider must register a KernelExtensionFactory via META-INF/services, see https://github.com/neo4j/neo4j/blob/master/community/lucene-index/src/main/resources/META-INF/services/org.neo4j.kernel.extension.KernelExtensionFactory

This KernelExtensionFactory is the entry point, just checkout the Lucene 3 based implementation at https://github.com/neo4j/neo4j/tree/master/community/lucene-index.

于 2013-07-04T14:21:37.097 回答
1

前段时间,我也遇到过这个问题:我在做原型,非常喜欢 Neo4j 的嵌入模式。但是,一旦我决定使用 Lucene 4 - 我就被不兼容的问题绊倒了。

操作系统

正如这里所建议的:如何在我的 java 项目中使用两个版本的 jar - 一种可能的解决方案是使用OSGi,并将 Neo4j 和 Lucene 4 包装到不同的包中。每个包都有单独的类加载器——因此 Neo4j 将在 Lucene 3 的运行时类中使用,但您仍然可以将 Lucene 4 用于您的目的。

但是,就我的原型设计而言——我不想因为两个组件不兼容而花时间为 OSGi 平台调整我的项目。

Maven 阴影插件

所以,我在Maven Shade Plugin的帮助下解决了问题。

Maven Shade Plugin 提供了将所有依赖项合并到单个“胖”JAR(也称为“uber JAR”)的能力。

因此,您可以生成“uber Neo4j 依赖项”,并在您的项目中使用它——而不是“真正的”Neo4j 依赖项。

但是还有一个重要的时刻:Lucene 3 和 Lucene 4 具有相同的包结构,并且许多类仍然具有相同的名称。因此,这可能会导致类加载冲突。

为了解决这个问题,Maven Shade 插件提供了在生成“uber JAR”期间重新定位类的能力:http ://maven.apache.org/plugins/maven-shade-plugin/examples/class-relocation.html

您可以指定包名称,并且在打包过程中 - Shade Plugin 会将类从指定包及其子包移动到其他包,并重写受影响的字节码

因此,在为 Neo4j 编写“uber JAR”期间 - 您可以配置 Shade Plugin 以将 Lucene 3 的类移动到其他包,例如:

org.apache.lucene.* -> shaded_3_6_2.org.apache.lucene.*

(幸运的是,Neo4j 似乎不使用反射,应用于 Lucene 的东西)。

因此,您可以使用以下命令创建空的 Maven 项目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>my.hack</groupId>
    <artifactId>uber-neo4j</artifactId>
    <version>1.9.3</version>
    <packaging>jar</packaging>
    <name>uber-neo4j</name>

    <properties>
        <neo4j-version>1.9.3</neo4j-version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j</artifactId>
            <version>${neo4j-version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <createDependencyReducedPom>false</createDependencyReducedPom>
                            <relocations>
                                <relocation>
                                    <pattern>org.apache.lucene</pattern>
                                    <shadedPattern>shaded_lucene_3_6_2.org.apache.lucene</shadedPattern>
                                </relocation>
                            </relocations>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>neo4j-repo</id>
            <name>Neo4j Repository</name>
            <url>http://m2.neo4j.org/content/repositories/releases</url>
        </repository>
    </repositories>
</project>

描述的配置 - 提供为 Neo4j 生成“uber JAR”的能力,其中包含重命名的 Lucene 3 包(只需执行mvn install)。

最后,您可以将这些东西作为一个模块附加到您的 Maven 项目中。

因此,在此解决方法之后 - 您将能够在您的项目中同时使用:Neo4j 和 Lucene 4。

以防万一,这里是带有 maven 配置的 GitHub 存储库的链接,用于为 Neo4j 生成“uber JAR”:https ://github.com/lagodiuk/neo4j-uber-jar

于 2014-12-13T21:30:56.783 回答
0

如果您不关心 Neo4j 使用什么索引,并且您使用 Maven 来管理依赖项,您可以使用Maven Shade 插件的类重定位功能来重命名 Neo4j 的 Lucene 依赖项,这样它就不会与新版本 Lucene 上的其他依赖项冲突.

在我的情况下,这需要将依赖于 Neo4j 的代码移动到一个单独的 Maven 项目中,因为 Shade 一次作用于整个项目/jar。因此,如果您可以将冲突的 Lucene 依赖项放到不同的项目中,这应该会很好用。

于 2014-12-10T22:52:58.873 回答