zoukankan      html  css  js  c++  java
  • neo4j中索引的使用

      neo4j可以对node和relationship中的属性建立索引,索引中的node(relationship)和属性对key-value为多对多的关系。一个node(relationship)可以在某索引中存储多个属性对,一个属性对也可以对应到多个node(relationship)。

    代码:    

    1. Node node1 = graphDb.createNode();  
    2.            node1.setProperty("name","easypoint");  
    3.   
    4.            Node node2 = graphDb.createNode();  
    5.            node2.setProperty("name","csdn");  
    6.   
    7.            nodeIndex.add(node1,"name",node1.getProperty("name"));  
    8.            nodeIndex.add(node1,"name","haha");  
    9.            nodeIndex.add(node2,"name",node2.getProperty("name"));  
    10.            nodeIndex.add(node2,"name","haha");  
    11.   
    12.            for(Node node :nodeIndex.get("name","haha")){  
    13.                 System.out.println(node.getProperty("name"));  
    14.            }  

    结果:

    1. easypoint  
    2. csdn  


        在neo4j中,索引可以分为两类:neo4j本身既是关于relationship的索引实现;基于独立索引引擎,如Apache Lucene的索引机制。通常情况下,我们所说的索引指第二种情况。按照索引的对象可以将索引分为两类:基于node的索引和基于relationship的索引。

        与oracle等关系型数据库不同的是,neo4j中索引的维护由用户自行管理(索引内容的增删改)。索引的维护必须在事务范围内。每个索引具有一个名称,neo4j根据名称来查找或者创建索引。

        维护索引时,有一点需要特别注意:更新索引时,需要手工删除对应的更新项,然后在添加更新后的项; 如下所示:

    1. // create a node with a property  
    2. // so we have something to update later on  
    3. Node fishburn = graphDb.createNode();  
    4. fishburn.setProperty( "name""Fishburn" );  
    5. // index it  
    6. actors.add( fishburn, "name", fishburn.getProperty( "name" ) );  
    7. // update the index entry  
    8. // when the property value changes  
    9. actors.remove( fishburn, "name", fishburn.getProperty( "name" ) );  
    10. fishburn.setProperty( "name""Laurence Fishburn" );  
    11. actors.add( fishburn, "name", fishburn.getProperty( "name" ) );  


    如果,不删除旧项,则会同时存在指向fishburn的两个key-value。

        索引的查询可以使用GET方法,也可以使用QUERY方法,相对与get,query方法功能更强大一些。get方法进行精确的key-value匹配;QUERY

    1. Relationship persephone = roles.get( "name""Persephone" ).getSingle();  
    1. for ( Node movie : movies.query( "title:*Matrix* AND year:1999" ) )  
    2. {  
    3. // This will return "The Matrix" from 1999 only.  
    4. }  


    创建索引时,通过api可以制定索引的配置选项。如下所示,配置索引支持fulltext检索

     
    1. Index<Node> fulltextMovies = index.forNodes( "movies-fulltext",  
    2.                 stringMap(IndexManager.PROVIDER, "lucene""type""fulltext") );  

    1. Node node1 = graphDb.createNode();  
    2.             node1.setProperty("name","easypoint and abb");  
    3.   
    4.             fulltextMovies.add( node1,"name",node1.getProperty("name"));  
    5.   
    6.             for(Node node :fulltextMovies.query( "name""and" )){  
    7.                 System.out.println(node.getProperty("name"));  
    8.             }  


    总结:neo4j提供了索引机制,与关系数据库相比,需要编程人员干预的内容较多,也正是因此,其灵活性是比较强的,但无疑增加了程序人员的工作量。

  • 相关阅读:
    PHP多条件模糊查询
    纯干货!一款APP从设计稿到切图过程全方位揭秘(转)
    0532. K-diff Pairs in an Array (M)
    0933. Number of Recent Calls (E)
    0139. Word Break (M)
    0713. Subarray Product Less Than K (M)
    0399. Evaluate Division (M)
    0495. Teemo Attacking (M)
    0179. Largest Number (M)
    0389. Find the Difference (E)
  • 原文地址:https://www.cnblogs.com/web100/p/neo4j.html
Copyright © 2011-2022 走看看