【问题标题】:Alternative IndexProvider for Neo4J 1.9.1Neo4J 1.9.1 的替代 IndexProvider
【发布时间】:2013-07-02 10:32:12
【问题描述】:

我在我的应用程序中使用 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>

【问题讨论】:

  • 我们做了一些测试,Lucene4 在我们的用例中比 3.6 慢得多,这就是为什么它没有被优先考虑。
  • 谢谢迈克尔,这很有趣。对于我的用例,我想将它与我现有的 MongoDB 数据配对,以遍历相关数据。我们目前正在使用 Lucene 4,但我想我应该看看降级到 3.6 对我的内部搜索要求的影响。我有一些性能测试脚本可以在前后应用来比较

标签: lucene neo4j


【解决方案1】:

从 1.8 -> 1.9 内部发生了一些变化。简而言之,索引提供者必须通过 META-INF/services 注册一个 KernelExtensionFactory,参见https://github.com/neo4j/neo4j/blob/master/community/lucene-index/src/main/resources/META-INF/services/org.neo4j.kernel.extension.KernelExtensionFactory

这个 KernelExtensionFactory 是入口点,只需在https://github.com/neo4j/neo4j/tree/master/community/lucene-index 处查看基于 Lucene 3 的实现即可。

【讨论】:

  • 我很惊讶没有内置的默认实现。感谢您的链接,我会检查出来。我现在只想要一些非常简单的东西来证明我们的应用程序的概念。
  • Neo4j 将索引视为实现细节,不依赖于任何仅 Lucene4 的特性。
【解决方案2】:

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

OSGi

正如这里所建议的:How to use two versions of jar in my java project - 一种可能的解决方案是使用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 Plugin 提供了在生成“uber JAR”期间重新定位类的能力:http://maven.apache.org/plugins/maven-shade-plugin/examples/class-relocation.html

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

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

org.apache.lucene.* -&gt; shaded_3_6_2.org.apache.lucene.*

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

因此,您可以使用以下 pom.xml 创建空的 maven 项目:

    <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>

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

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

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

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

【讨论】:

    【解决方案3】:

    如果您不关心 Neo4j 使用什么索引,并且使用 Maven 来管理依赖项,则可以使用 the Maven Shade plugin's class relocation feature 重命名 Neo4j 的 Lucene 依赖项,这样它就不会与新版本 Lucene 上的其他依赖项冲突。

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

    【讨论】:

      猜你喜欢
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-06
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      相关资源
      最近更新 更多