【问题标题】:Neo4j SDN 4 node_auto_indexNeo4j SDN 4 node_auto_index
【发布时间】:2016-05-30 19:13:22
【问题描述】:

我正在从 SDN 3 迁移到 SDN 4,从 Neo4j 2.3 迁移到 3.0.1

我有以下 Search Cypher 查询:

@Query(value = "START d=node:node_auto_index({autoIndexQuery}) MATCH (d:Decision) RETURN d")
Page<Decision> searchDecisions(@Param("autoIndexQuery") String autoIndexQuery, Pageable page);

我的测试中的参数 autoIndexQuery 等于以下 Lucene 查询:

(name:rdbma~0.33)^3 OR description:rdbma OR (name:mosyl~0.33)^3 OR description:mosyl

现在

decisionRepository.searchDecisions(autoIndexQuery, new PageRequest(page, size));

返回null

但在 SDN 3 和 Neo4j 2.3 上运行良好并返回 Decision 节点。

这是我的 Neo4jTestConfig:

@Configuration
@EnableNeo4jRepositories(basePackages = "com.example")
@EnableTransactionManagement
public class Neo4jTestConfig extends Neo4jConfiguration implements BeanFactoryAware {

    private static final String NEO4J_EMBEDDED_DATABASE_PATH_PROPERTY = "neo4j.embedded.database.path";

    @Resource
    private Environment environment;

    private BeanFactory beanFactory;

    @SuppressWarnings("deprecation")
    @Bean(destroyMethod = "shutdown")
    public GraphDatabaseService graphDatabaseService() {

        // @formatter:off
        GraphDatabaseService graphDb = new GraphDatabaseFactory()
                .newEmbeddedDatabaseBuilder(new File(environment.getProperty(NEO4J_EMBEDDED_DATABASE_PATH_PROPERTY)))       
                .setConfig(GraphDatabaseSettings.node_keys_indexable, "name,description")
                .setConfig(GraphDatabaseSettings.node_auto_indexing, "true").
                newGraphDatabase();         
        // @formatter:on

        return graphDb;
    }

    /**
     * Hook into the application lifecycle and register listeners that perform
     * behaviour across types of entities during this life cycle
     * 
     */
    @EventListener
    public void handleBeforeSaveEvent(BeforeSaveEvent event) {
        Object entity = event.getEntity();
        if (entity instanceof BaseEntity) {
            BaseEntity baseEntity = (BaseEntity) entity;
            if (baseEntity.getCreateDate() == null) {
                baseEntity.setCreateDate(new Date());
            } else {
                baseEntity.setUpdateDate(new Date());
            }
        }
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }

    public BeanFactory getBeanFactory() {
        return beanFactory;
    }

    @Bean
    public org.neo4j.ogm.config.Configuration getConfiguration() {
        // @formatter:off
        org.neo4j.ogm.config.Configuration config = new org.neo4j.ogm.config.Configuration();
        config.driverConfiguration()
            .setDriverClassName("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver");
        // @formatter:on
        return config;
    }

    @Override
    public SessionFactory getSessionFactory() {
        return new SessionFactory(getConfiguration(), "com.example");
    }

}

我的配置有什么问题以及如何让它在 SDN 4 上运行?

更新

另外,我找到了以下答案Can't configure node_auto_index with InProcessServer() SDN 4,但在我的类路径中找不到 ServerControls 和 TestServerBuilders。

这是我的数据库索引配置:

@Transactional
public class SearchDaoImpl implements SearchDao {

    @Autowired
    private DecisionRepository decisionRepository;

    @PostConstruct
    public void init() {
        EmbeddedDriver embeddedDriver = (EmbeddedDriver) Components.driver();
        GraphDatabaseService databaseService = embeddedDriver.getGraphDatabaseService();
        try (Transaction t = databaseService.beginTx()) {
            Index<Node> autoIndex = databaseService.index().forNodes("node_auto_index");
            databaseService.index().setConfiguration(autoIndex, "type", "fulltext");
            databaseService.index().setConfiguration(autoIndex, "to_lower_case", "true");
            databaseService.index().setConfiguration(autoIndex, "analyzer", StandardAnalyzerV36.class.getName());
            t.success();
        }
    }
...
}

现在我不知道我的代码中的哪个位置是添加的正确位置

Components.setDriver(new EmbeddedDriver(graphDatabaseService()));

按照以下答案的建议

更新 2

@SuppressWarnings("deprecation")
@Bean(destroyMethod = "shutdown")
public GraphDatabaseService graphDatabaseService() {

    // @formatter:off
    GraphDatabaseService graphDatabaseService = new GraphDatabaseFactory()
            .newEmbeddedDatabaseBuilder(new File(environment.getProperty(NEO4J_EMBEDDED_DATABASE_PATH_PROPERTY)))       
            .setConfig(GraphDatabaseSettings.node_keys_indexable, "name,description")
            .setConfig(GraphDatabaseSettings.node_auto_indexing, "true").
            newGraphDatabase();         
    // @formatter:on        

    Components.setDriver(new EmbeddedDriver(graphDatabaseService));

    return graphDatabaseService;
}

@Override
public SessionFactory getSessionFactory() {
    return new SessionFactory("com.example");
}

以下配置失败:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getSession' defined in com.techbook.domain.configuration.Neo4jTestConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.neo4j.ogm.session.Session]: Factory method 'getSession' threw exception; nested exception is org.neo4j.ogm.exception.ServiceNotFoundException: Driver: null
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)

【问题讨论】:

    标签: neo4j lucene cypher spring-data-neo4j spring-data-neo4j-4


    【解决方案1】:

    通过设置驱动程序类名称属性,EmbeddedDriver 将创建GraphDatabaseService 的私有实例,这不是您想要的,因为您正在自己初始化它。相反,您需要告诉 Components 类明确使用您提供的驱动程序:

    Components.setDriver(new EmbeddedDriver(graphDatabaseService()));

    这应该像您期望的那样连接组件。

    【讨论】:

    • 谢谢!这行代码应该在我的应用程序中添加到哪里?
    • 最简单的事情可能是在 GraphDatabaseService bean 声明的末尾,就在你返回 graphDb 实例之前。
    • 现在它因Driver: null 异常而失败。我已经更新了我的问题。
    • 我已将其添加到@PostConstruct public void init() { Components.setDriver(new EmbeddedDriver(graphDatabaseService())); },现在可以正常使用
    猜你喜欢
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    • 2017-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多