Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(knext&server): add graph query APIs and extend search API #438

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
4 changes: 0 additions & 4 deletions builder/model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,5 @@
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>com.antgroup.openspg</groupId>
<artifactId>common-util</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import com.antgroup.openspg.cloudext.interfaces.graphstore.model.lpg.schema.operation.SchemaAtomicOperationEnum;
import com.antgroup.openspg.cloudext.interfaces.graphstore.util.TypeNameUtils;
import com.antgroup.openspg.core.schema.model.type.BasicTypeEnum;
import com.antgroup.openspg.server.api.facade.ApiConstants;
import com.antgroup.tugraph.TuGraphDbRpcClient;
import com.google.common.collect.Lists;
import java.io.InputStream;
Expand Down Expand Up @@ -78,13 +77,16 @@ public class TuGraphStoreClient extends BaseLPGGraphStoreClient {
@Getter private final LPGTypeNameConvertor typeNameConvertor;
@Getter private final String connUrl;

private static final String ACCESS_ID = "accessId";
private static final String ACCESS_KEY = "accessKey";
private static final String TIMEOUT = "timeout";

public TuGraphStoreClient(String connUrl, LPGTypeNameConvertor typeNameConvertor) {
UriComponents uriComponents = UriComponentsBuilder.fromUriString(connUrl).build();
this.connUrl = connUrl;
this.graphName = uriComponents.getQueryParams().getFirst(TuGraphConstants.GRAPH_NAME);
this.timeout =
Double.parseDouble(
String.valueOf(uriComponents.getQueryParams().getFirst(ApiConstants.TIMEOUT)));
Double.parseDouble(String.valueOf(uriComponents.getQueryParams().getFirst(TIMEOUT)));
this.client = initTuGraphClient(uriComponents);
this.internalIdGenerator = new NoChangedIdGenerator();
this.typeNameConvertor = typeNameConvertor;
Expand Down Expand Up @@ -149,8 +151,8 @@ public void close() throws Exception {

private TuGraphDbRpcClient initTuGraphClient(UriComponents uriComponents) {
String host = String.format("%s:%s", uriComponents.getHost(), uriComponents.getPort());
String accessId = uriComponents.getQueryParams().getFirst(ApiConstants.ACCESS_ID);
String accessKey = uriComponents.getQueryParams().getFirst(ApiConstants.ACCESS_KEY);
String accessId = uriComponents.getQueryParams().getFirst(ACCESS_ID);
String accessKey = uriComponents.getQueryParams().getFirst(ACCESS_KEY);
TuGraphDbRpcClient client = null;
try {
client = new TuGraphDbRpcClient(host, accessId, accessKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import com.antgroup.openspg.cloudext.interfaces.searchengine.model.request.query.MatchQuery;
import com.antgroup.openspg.cloudext.interfaces.searchengine.model.request.query.QueryGroup;
import com.antgroup.openspg.cloudext.interfaces.searchengine.model.request.query.TermQuery;
import com.antgroup.openspg.common.util.SchemaJsonUtils;
import com.antgroup.openspg.common.util.StringUtils;
import com.antgroup.openspg.server.api.facade.SchemaJsonUtils;
import com.dtflys.forest.http.ForestResponse;
import com.google.gson.reflect.TypeToken;
import java.net.URLEncoder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package com.antgroup.openspg.cloudext.interfaces.graphstore.model.lpg.record;

import com.antgroup.openspg.cloudext.interfaces.graphstore.model.lpg.schema.VertexType;
import com.antgroup.openspg.server.api.facade.ApiConstants;
import com.google.common.collect.Lists;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -35,12 +35,18 @@ public class VertexRecord extends BaseLPGRecord {

private final String id;

private static final long DEFAULT_VERSION = 0L;

public VertexRecord(String id, String vertexType, List<LPGPropertyRecord> properties) {
super(LPGRecordTypeEnum.VERTEX, properties);
this.id = id;
this.vertexType = vertexType;
}

public VertexRecord(String id, String vertexType) {
this(id, vertexType, Lists.newArrayList());
}

@Override
public Map<String, Object> toPropertyMapWithId() {
Map<String, Object> otherProperties = toPropertyMap();
Expand All @@ -55,7 +61,7 @@ public Map<String, TreeMap<Long, Object>> toPropertyMapWithIdAndVersion() {
otherProperties.forEach(
(key, value) -> {
TreeMap<Long, Object> propertyVersion = new TreeMap<>();
propertyVersion.put(ApiConstants.DEFAULT_VERSION, value);
propertyVersion.put(DEFAULT_VERSION, value);
results.put(key, propertyVersion);
});
return results;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
* {@link EdgeTypeName EdgeTypeName} is the unique identifier of {@link EdgeType EdgeType}, and it
Expand All @@ -25,15 +26,16 @@
*/
@Getter
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class EdgeTypeName extends BaseValObj {

private final String startVertexType;
private String startVertexType;

/** The label of edge type */
private final String edgeLabel;
private String edgeLabel;

private final String endVertexType;
private String endVertexType;

public static EdgeTypeName parse(String edgeTypeName) {
String[] splits = edgeTypeName.split("_");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@
package com.antgroup.openspg.cloudext.interfaces.searchengine.model.request.query;

import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;

public class FullTextSearchQuery extends BaseQuery {

@Getter private final String queryString;
@Getter private final List<String> labelConstraints;
@Getter @Setter private Map<String, Object> params;

public FullTextSearchQuery(@NonNull String queryString) {
this(queryString, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@

package com.antgroup.openspg.cloudext.interfaces.searchengine.model.request.query;

import java.util.Map;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;

public class VectorSearchQuery extends BaseQuery {

@Getter private final String label;
@Getter private final String propertyKey;
@Getter private final float[] queryVector;
@Getter private final int efSearch;
@Getter @Setter private Map<String, Object> params;

public VectorSearchQuery(
@NonNull String label, @NonNull String propertyKey, float @NonNull [] queryVector) {
String label, @NonNull String propertyKey, float @NonNull [] queryVector) {
this(label, propertyKey, queryVector, -1);
}

public VectorSearchQuery(
@NonNull String label,
@NonNull String propertyKey,
float @NonNull [] queryVector,
int efSearch) {
String label, @NonNull String propertyKey, float @NonNull [] queryVector, int efSearch) {
this.label = label;
this.propertyKey = propertyKey;
this.queryVector = queryVector;
Expand Down
10 changes: 9 additions & 1 deletion common/util/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<dependencies>
<dependency>
<groupId>com.antgroup.openspg.server</groupId>
<artifactId>api-facade</artifactId>
<artifactId>core-schema-model</artifactId>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
Expand Down Expand Up @@ -59,5 +59,13 @@
<groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>org.danilopianini</groupId>
<artifactId>gson-extras</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

package com.antgroup.openspg.common.util;

import com.antgroup.openspg.server.api.facade.SchemaJsonUtils;
import lombok.extern.slf4j.Slf4j;

@Slf4j
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* or implied.
*/

package com.antgroup.openspg.server.api.facade;
package com.antgroup.openspg.common.util;

import com.antgroup.openspg.core.schema.model.constraint.BaseConstraintItem;
import com.antgroup.openspg.core.schema.model.constraint.ConstraintTypeEnum;
Expand Down
2 changes: 1 addition & 1 deletion python/knext/KNEXT_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.3.20241022.2
0.6.0.20241127.1
2 changes: 1 addition & 1 deletion python/knext/knext/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


__package_name__ = "openspg-knext"
__version__ = "0.0.3.20241022.2"
__version__ = "0.6.0.20241127.1"

from knext.common.env import init_env

Expand Down
2 changes: 1 addition & 1 deletion python/knext/knext/common/rest/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
from knext.reasoner.rest.models import *
from knext.project.rest.models import *
from knext.search.rest.models import *
from knext.graph_algo.rest.models import *
from knext.graph.rest.models import *
from knext.thinker.rest.models import *
58 changes: 58 additions & 0 deletions python/knext/knext/graph/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# coding: utf-8
# Copyright 2023 OpenSPG Authors
#
# Licensed 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.


# flake8: noqa

"""
knext

No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) # noqa: E501

The version of the OpenAPI document: 1.0.0
Generated by: https://openapi-generator.tech
"""


from __future__ import absolute_import

__version__ = "1.0.0"

# import apis into sdk package
from knext.graph.rest.graph_api import GraphApi


# import models into model package
from knext.graph.rest.models.get_page_rank_scores_request import (
GetPageRankScoresRequest,
)
from knext.graph.rest.models.get_page_rank_scores_request_start_nodes import (
GetPageRankScoresRequestStartNodes,
)
from knext.graph.rest.models.page_rank_score_instance import PageRankScoreInstance
from knext.graph.rest.models.delete_vertex_request import DeleteVertexRequest
from knext.graph.rest.models.delete_edge_request import DeleteEdgeRequest
from knext.graph.rest.models.edge_record_instance import EdgeRecordInstance
from knext.graph.rest.models.upsert_vertex_request import UpsertVertexRequest
from knext.graph.rest.models.upsert_edge_request import UpsertEdgeRequest
from knext.graph.rest.models.vertex_record_instance import VertexRecordInstance
from knext.graph.rest.models.writer_graph_request import WriterGraphRequest
from knext.graph.rest.models.edge_record import EdgeRecord
from knext.graph.rest.models.edge_type_name import EdgeTypeName
from knext.graph.rest.models.expend_one_hop_request import ExpendOneHopRequest
from knext.graph.rest.models.lpg_property_record import LpgPropertyRecord
from knext.graph.rest.models.query_vertex_request import QueryVertexRequest
from knext.graph.rest.models.query_vertex_response import QueryVertexResponse
from knext.graph.rest.models.batch_query_vertex_request import BatchQueryVertexRequest
from knext.graph.rest.models.batch_query_vertex_response import BatchQueryVertexResponse
from knext.graph.rest.models.vertex_record import VertexRecord
from knext.graph.rest.models.expend_one_hop_response import ExpendOneHopResponse
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@

from knext.common.base.client import Client
from knext.common.rest import ApiClient, Configuration
from knext.graph_algo import (
from knext.graph import (
rest,
GetPageRankScoresRequest,
GetPageRankScoresRequestStartNodes,
WriterGraphRequest,
QueryVertexRequest,
BatchQueryVertexRequest,
ExpendOneHopRequest,
EdgeTypeName,
)
from knext.graph_algo import rest


class GraphAlgoClient(Client):
class GraphClient(Client):
""" """

def __init__(self, host_addr: str = None, project_id: int = None):
Expand Down Expand Up @@ -67,9 +71,39 @@ def write_graph(self, sub_graph: dict, operation: str, lead_to_builder: bool):
)
self._rest_client.graph_writer_graph_post(writer_graph_request=request)

def query_vertex(self, type_name: str, biz_id: str):
request = QueryVertexRequest(
project_id=self._project_id, type_name=type_name, biz_id=biz_id
)
return self._rest_client.graph_query_vertex_post(query_vertex_request=request)

def batch_query_vertex(self, type_name: str, biz_ids: List[str]):
request = BatchQueryVertexRequest(
project_id=self._project_id, type_name=type_name, biz_ids=biz_ids
)
return self._rest_client.graph_batch_query_vertex_post(
batch_query_vertex_request=request
)

def expend_one_hop(
self,
type_name: str,
biz_id: str,
edge_type_name_constraint: List[EdgeTypeName] = None,
):
request = ExpendOneHopRequest(
project_id=self._project_id,
type_name=type_name,
biz_id=biz_id,
edge_type_name_constraint=edge_type_name_constraint,
)
return self._rest_client.graph_expend_one_hop_post(
expend_one_hop_request=request
)


if __name__ == "__main__":
sc = GraphAlgoClient("http://127.0.0.1:8887", 4)
sc = GraphClient("http://127.0.0.1:8887", 4)
out = sc.calculate_pagerank_scores(
"Entity", [{"name": "Anxiety_and_nervousness", "type": "Entity"}]
)
Expand Down
Loading
Loading