Neo4j创建自动索引

一、创建Neo4jLegacy indexing

  1.为节点创建索引

  官方API的创建示例为:

Neo4j创建自动索引

将一节点添加至索引:

public static void AddNodeIndex(String nid)
    {
        String txUri=SERVER_ROOT_URI+"index/node/favorites";
        WebResource resource = Client.create().resource(txUri);
        String entity="{\"value\" : \"n204\",\"uri\" : \"http://192.168.209.128:7474/db/data/node/"+nid+"\",\"key\" : \"n201\"}";
        ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
                .type(MediaType.APPLICATION_JSON).entity(entity)
                .post(ClientResponse.class);

System.out.println(response.getStatus());
        System.out.println(response.getEntity(String.class));
        response.close();
    }

ps:nid是要添加索引的节点ID  value是索引值  key是索引名称

2.通过属性查找节点

public static void GetNodeByIndex()
    {
        String txUri=SERVER_ROOT_URI+"index/node/favorites/n201/n201";
        WebResource resource = Client.create().resource(txUri);
       
        ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
                .type(MediaType.APPLICATION_JSON)
                .get(ClientResponse.class);

System.out.println(response.getStatus());
        System.out.println(response.getEntity(String.class));
        response.close();
    }

txUri路径中:favorites为刚创建的索引名称,第一个n201是节点索引key,第二个n201是节点索引值

二、自动创建索引(Legacy Automatic Indexes)

What default configuration means depends on how you have configured your database. If you haven’t

changed any indexing configuration, it means the indexes will be using a Lucene-based backend.

数据库配置之后,就可以自动创建索引

1.配置文件配置

Auto-indexing must be enabled through configuration before we can create or configure them. Firstly

ensure that you’ve added some config like this into your server’s conf/neo4j.properties file:

打开conf/neo4j.properties文件如图

Neo4j创建自动索引

配置下面的节点

# Enable auto-indexing for nodes, default is false.
node_auto_indexing=true

# The node property keys to be auto-indexed, if enabled.
node_keys_indexable=name,ki

# Enable auto-indexing for relationships, default is false.
relationship_auto_indexing=true

# The relationship property keys to be auto-indexed, if enabled.
relationship_keys_indexable=name,ki

Node_keys_indexable、relationship_keys_indexable对应节点、关系的属性

配置完成之后重启服务

Neo4j创建自动索引

重启三个节点的集群

2。测试索引

插入一个节点和关系

// 创建节点
    @Test
    public void test2() {
        URI uri = CreateSimpleGraph.createNode();
        CreateSimpleGraph.addProperty(uri, "name", "张三");
        URI uri1 = CreateSimpleGraph.createNode();
        CreateSimpleGraph.addProperty(uri1, "name", "李四");
    }
// 为节点设置关系
    @Test
    public void test6() {
        for (int i = 1; i < 2; i++) {
            try {
                URI suri = new URI("http://192.168.209.128:7474/db/data/node/171391");
                String uri1="http://192.168.209.128:7474/db/data/node/";
                URI euri = new URI("http://192.168.209.128:7474/db/data/node/171392");
                URI reluri= CreateSimpleGraph.addRelationship(suri, euri, "家人","{\"ki\" : \"1234567890\", \"name\" : \"无\" }");
                System.out.println(reluri);
            } catch (URISyntaxException e) {
                // 异常信息输出该内容
                e.printStackTrace();
            }
        }
    }

3.通过属性查找节点

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/357ac34da985e54fcbd1391828306e36.html