org.neo4j.graphdb.index.Index Java Examples

The following examples show how to use org.neo4j.graphdb.index.Index. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FulltextIndex.java    From ongdb-lab-apoc with Apache License 2.0 5 votes vote down vote up
private <T extends PropertyContainer> void indexEntityWithMap(T pc, Map<String, Object> document, Index<T> index) {
    index.remove(pc);
    document.forEach((key, value) -> {
        index.remove(pc, key);
        index.add(pc, key, value);
        System.out.println("Node:" + pc + "," + key + ":" + value);
    });
}
 
Example #2
Source File: FulltextIndex.java    From ongdb-lab-apoc with Apache License 2.0 5 votes vote down vote up
/**
 * @param
 * @return
 * @Description: TODO(构建索引并返回MESSAGE - 支持自动更新)
 */
private String chineseFulltextAutoIndex(String indexName, String labelName, List<String> propKeys, Map<String, String> options) {

    Label label = Label.label(labelName);

    // 按照标签找到该标签下的所有节点
    ResourceIterator<Node> nodes = db.findNodes(label);

    // 合并用户配置
    FULL_INDEX_CONFIG.putAll(options);

    int nodesSize = 0;
    int propertiesSize = 0;
    while (nodes.hasNext()) {
        nodesSize++;
        Node node = nodes.next();
        System.out.println("current nodes:" + node.toString());

        // 每个节点上需要添加索引的属性
        Set<Map.Entry<String, Object>> properties = node.getProperties(propKeys.toArray(new String[0])).entrySet();
        System.out.println("current node properties" + properties);

        // 查询该节点是否已有索引,有的话删除
        if (db.index().existsForNodes(indexName)) {
            Index<Node> oldIndex = db.index().forNodes(indexName);
            System.out.println("current node index" + oldIndex);
            oldIndex.remove(node);
        }

        // 为该节点的每个需要添加索引的属性添加全文索引
        Index<Node> nodeIndex = db.index().forNodes(indexName, FULL_INDEX_CONFIG);
        for (Map.Entry<String, Object> property : properties) {
            propertiesSize++;
            nodeIndex.add(node, property.getKey(), property.getValue());
        }
    }

    String message = "IndexName:" + indexName + ",LabelName:" + labelName + ",NodesSize:" + nodesSize + ",PropertiesSize:" + propertiesSize;
    return message;
}
 
Example #3
Source File: Neo4jIndexHandler.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
private GraphTraversal<Vertex, Vertex> traversalFromIndex(Collection collection, QuickSearch quickSearch) {
  Index<Node> index = getQuickSearchIndex(collection);

  try {
    IndexHits<Node> hits = index.query(QUICK_SEARCH_PROP_NAME, createQuery(quickSearch));
    List<Long> ids = StreamIterator.stream(hits.iterator()).map(h -> h.getId()).collect(toList());
    return ids.isEmpty() ? EmptyGraphTraversal.instance() : traversal().V(ids);
  } catch (Exception e) {
    LOG.error("Unexpected exception during search", e);
    return EmptyGraphTraversal.instance();
  }

}
 
Example #4
Source File: Neo4jIndexHandler.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void removeFromQuickSearchIndex(Collection collection, Vertex vertex) {
  Index<Node> index = getQuickSearchIndex(collection);
  // make sure only this field is removed from the index.
  final Optional<Node> node = vertexToNode(vertex);
  if (node.isPresent()) {
    index.remove(node.get(), QUICK_SEARCH_PROP_NAME);
  }
}
 
Example #5
Source File: Neo4jIndexHandler.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void upsertIntoQuickSearchIndex(Collection collection, String quickSearchValue, Vertex vertex,
                                       Vertex oldVertex) {
  if (oldVertex != null) {
    this.removeFromQuickSearchIndex(collection, oldVertex);
  }

  Index<Node> index = getQuickSearchIndex(collection);

  vertexToNode(vertex).ifPresent(node -> index.add(node, QUICK_SEARCH_PROP_NAME, quickSearchValue));
}
 
Example #6
Source File: Neo4jIndexHandler.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
@Override
@Deprecated
public void insertIntoIdIndex(UUID timId, Vertex vertex) {
  Index<Node> index = getIdIndex();
  final Optional<Node> node = vertexToNode(vertex);
  if (node.isPresent()) {
    index.add(node.get(), TIM_ID, timId.toString());
  }
}
 
Example #7
Source File: RelationTypeRdfUriMigration.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void execute(TinkerPopGraphManager graphWrapper) throws IOException {
  final Graph graph = graphWrapper.getGraph();
  final Transaction transaction = graph.tx();

  if (!transaction.isOpen()) {
    transaction.open();
  }

  final GraphDatabaseService graphDatabase = graphWrapper.getGraphDatabase();
  final Index<Node> rdfIndex = graphDatabase.index().forNodes(RDFINDEX_NAME);

  final String regularNameProp = "relationtype_regularName";
  final String inverseNameProp = "relationtype_inverseName";

  graph.traversal().V().has(regularNameProp).forEachRemaining(vertex -> {
    final String regularName = vertex.property(regularNameProp).isPresent() ?
      vertex.<String>property(regularNameProp).value() : "";

    final String inverseName = vertex.property(inverseNameProp).isPresent() ?
      vertex.<String>property(inverseNameProp).value() : "";

    final String rdfUri = TIMBUCTOO_NAMESPACE + regularName;
    final String[] rdfAlternatives = new String[]{ TIMBUCTOO_NAMESPACE + inverseName };

    LOG.info("setting rdfUri: \"{}\" and rdfAlternatives [{}] for relationType", rdfUri, rdfAlternatives);
    vertex.property(RDF_URI_PROP, rdfUri);
    vertex.property(RDF_SYNONYM_PROP, rdfAlternatives);

    LOG.info("indexing rdfUri: \"{}\" and rdfAlternatives [{}] for relationType", rdfUri, rdfAlternatives);
    org.neo4j.graphdb.Node neo4jNode = graphDatabase.getNodeById((Long) vertex.id());
    rdfIndex.add(neo4jNode, RelationTypeService.RELATIONTYPE_INDEX_NAME, rdfUri);
    rdfIndex.add(neo4jNode, RelationTypeService.RELATIONTYPE_INDEX_NAME, rdfAlternatives[0]);
  });

  transaction.commit();
  transaction.close();
}
 
Example #8
Source File: FulltextIndex.java    From ongdb-lab-apoc with Apache License 2.0 4 votes vote down vote up
private <T extends PropertyContainer> void indexEntityProperties(T pc, List<String> propKeys, org.neo4j.graphdb.index.Index<T> index) {
    Map<String, Object> properties = pc.getProperties(propKeys.toArray(new String[propKeys.size()]));
    indexEntityWithMap(pc, properties, index);
}
 
Example #9
Source File: FulltextIndex.java    From ongdb-lab-apoc with Apache License 2.0 4 votes vote down vote up
/**
 * @param
 * @return
 * @Description: TODO(开始添加索引)
 */
private void populate(Index<Node> index, List<String> config, Consumer<IndexStats> result) {
    String[] structure = config.toArray(new String[config.size()]);
    Map<LabelProperty, Counter> stats = new HashMap<>();
    Transaction tx = db.beginTx();
    try {
        int batch = 0;
        for (Node node : db.getAllNodes()) {
            boolean indexed = false;

            // 给节点的每个标签都添加索引
            for (Label label : node.getLabels()) {
                String[] keys = structure;
                if (keys == null) continue;
                indexed = true;

                // 通过标签控制为哪些属性添加索引 (默认为所有节点属性添加索引)
                Map<String, Object> properties = keys.length == 0 ? node.getAllProperties() : node.getProperties(keys);

                for (Map.Entry<String, Object> entry : properties.entrySet()) {
                    Object value = entry.getValue();

                    // 添加索引
                    index.add(node, entry.getKey(), value.toString());
                    if (value instanceof Number) {
                        value = ValueContext.numeric(((Number) value).doubleValue());
                    }

                    // 节点标签下添加索引
                    index.add(node, label.name() + "." + entry.getKey(), value);

                    stats.computeIfAbsent(new LabelProperty(label.name(), entry.getKey()), x -> new Counter()).count++;
                }
            }
            if (indexed) {
                if (++batch == 50_000) {
                    batch = 0;
                    tx.success();
                    tx.close();
                    tx = db.beginTx();
                }
            }
        }
        tx.success();
    } finally {
        tx.close();
    }
    stats.forEach((key, counter) -> result.accept(key.stats(counter)));
}
 
Example #10
Source File: Neo4jIndexHandler.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
private Index<Node> getQuickSearchIndex(Collection collection) {
  // Add the config below, to make sure the index is case insensitive.
  Map<String, String> indexConfig = MapUtil.stringMap(IndexManager.PROVIDER, "lucene", "type", "fulltext");
  return indexManager().forNodes(getQuicksearchIndexName(collection), indexConfig);
}
 
Example #11
Source File: Neo4jIndexHandler.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void deleteQuickSearchIndex(Collection collection) {
  Index<Node> index = getQuickSearchIndex(collection);
  index.delete();
}
 
Example #12
Source File: Neo4jIndexHandler.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
private Index<Node> getIdIndex() {
  return indexManager().forNodes(ID_INDEX);
}
 
Example #13
Source File: FulltextIndex.java    From ongdb-lab-apoc with Apache License 2.0 2 votes vote down vote up
/**
 * @param
 * @return
 * @Description: TODO(索引预配置)
 */
private Index<Node> getNodeIndex(String name, Map<String, String> config) {
    IndexManager mgr = db.index();
    return config == null ? mgr.forNodes(name) : mgr.forNodes(name, config);
}