Neo4j SDN 4 node_auto_index

时间:2016-05-30 19:13:23

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

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

我有以下搜索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)

1 个答案:

答案 0 :(得分:3)

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

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

这应该按照您的预期连接组件。