Skip to content

Commit

Permalink
fix code style , add test example
Browse files Browse the repository at this point in the history
  • Loading branch information
jadepeng committed May 28, 2021
1 parent 931c641 commit b8f8b6a
Showing 1 changed file with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.baidu.hugegraph.example;

import java.util.Arrays;
import java.util.List;

import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.slf4j.Logger;

import com.baidu.hugegraph.HugeFactory;
import com.baidu.hugegraph.HugeGraph;
import com.baidu.hugegraph.schema.SchemaManager;
import com.baidu.hugegraph.testutil.Assert;
import com.baidu.hugegraph.traversal.optimize.ConditionP;
import com.baidu.hugegraph.util.Log;
import com.google.common.collect.Sets;

public class TestCollectionIndexExample {

private static final Logger LOG = Log.logger(Example3.class);

public static void main(String[] args) throws Exception {
LOG.info("TestIndexExample start!");

HugeGraph graph = ExampleUtil.loadGraph();

initData(graph);
queryTest(graph);

graph.close();

HugeFactory.shutdown(30L);
}

public static void initData(final HugeGraph graph) {
SchemaManager schema = graph.schema();

schema.propertyKey("id").asInt().create();
schema.propertyKey("name").asText().create();
schema.propertyKey("weight").asDouble().create();
schema.propertyKey("tags").asText().valueSet().create();
schema.propertyKey("category").asText().valueSet().create();
schema.propertyKey("country").asText().create();

schema.vertexLabel("soft").properties("name", "tags", "country", "category")
.primaryKeys("name").create();

graph.addVertex(T.label, "soft", "name", "hugegraph",
"country", "china",
"category", Arrays.asList("graphdb"),
"tags", Arrays.asList("graphdb", "gremlin"));

graph.addVertex(T.label, "soft", "name", "neo4j",
"country", "usa",
"category", Arrays.asList("graphdb"),
"tags", Arrays.asList("graphdb", "cypher"));
graph.tx().commit();


schema.indexLabel("softByTag").onV("soft").secondary()
.by("tags").create();

schema.indexLabel("softByCategory").onV("soft").search()
.by("category").create();

}

public static void queryTest(final HugeGraph graph) {
List<Vertex> vertices;
vertices = graph.traversal().V().has("soft", "category",
ConditionP.textContains("graphdb")).toList();
Assert.assertEquals(2, vertices.size());

// by single item
vertices = graph.traversal().V().has("soft", "tags", "gremlin").toList();
Assert.assertEquals(1, vertices.size());

// by contains
vertices = graph.traversal().V().has("soft", "tags", ConditionP.contains("gremlin")).toList();
Assert.assertEquals(1, vertices.size());

// collection search
vertices = graph.traversal().V().has("soft", "tags", Sets.newHashSet(
"cypher",
"graphdb")).toList();
Assert.assertEquals(1, vertices.size());
}


}

0 comments on commit b8f8b6a

Please sign in to comment.