|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package io.milvus.v2; |
| 21 | + |
| 22 | +import com.google.gson.JsonObject; |
| 23 | +import io.milvus.common.utils.JsonUtils; |
| 24 | +import io.milvus.v1.CommonUtils; |
| 25 | +import io.milvus.v2.client.ConnectConfig; |
| 26 | +import io.milvus.v2.client.MilvusClientV2; |
| 27 | +import io.milvus.v2.common.ConsistencyLevel; |
| 28 | +import io.milvus.v2.common.DataType; |
| 29 | +import io.milvus.v2.common.IndexParam; |
| 30 | +import io.milvus.v2.service.collection.request.AddFieldReq; |
| 31 | +import io.milvus.v2.service.collection.request.CreateCollectionReq; |
| 32 | +import io.milvus.v2.service.collection.request.DropCollectionReq; |
| 33 | +import io.milvus.v2.service.collection.request.LoadCollectionReq; |
| 34 | +import io.milvus.v2.service.index.request.CreateIndexReq; |
| 35 | +import io.milvus.v2.service.vector.request.InsertReq; |
| 36 | +import io.milvus.v2.service.vector.request.QueryReq; |
| 37 | +import io.milvus.v2.service.vector.response.QueryResp; |
| 38 | + |
| 39 | +import java.util.*; |
| 40 | + |
| 41 | +public class GeometryExample { |
| 42 | + private static final MilvusClientV2 client; |
| 43 | + static { |
| 44 | + client = new MilvusClientV2(ConnectConfig.builder() |
| 45 | + .uri("http://localhost:19530") |
| 46 | + .build()); |
| 47 | + } |
| 48 | + |
| 49 | + private static final String COLLECTION_NAME = "java_sdk_example_geometry_v2"; |
| 50 | + private static final String ID_FIELD = "id"; |
| 51 | + private static final String GEO_FIELD = "geometry"; |
| 52 | + private static final String VECTOR_FIELD = "vector"; |
| 53 | + private static final Integer VECTOR_DIM = 128; |
| 54 | + |
| 55 | + private static void createCollection() { |
| 56 | + CreateCollectionReq.CollectionSchema collectionSchema = CreateCollectionReq.CollectionSchema.builder() |
| 57 | + .build(); |
| 58 | + collectionSchema.addField(AddFieldReq.builder() |
| 59 | + .fieldName(ID_FIELD) |
| 60 | + .dataType(DataType.Int64) |
| 61 | + .isPrimaryKey(true) |
| 62 | + .autoID(true) |
| 63 | + .build()); |
| 64 | + collectionSchema.addField(AddFieldReq.builder() |
| 65 | + .fieldName(GEO_FIELD) |
| 66 | + .dataType(DataType.Geometry) |
| 67 | + .build()); |
| 68 | + collectionSchema.addField(AddFieldReq.builder() |
| 69 | + .fieldName(VECTOR_FIELD) |
| 70 | + .dataType(DataType.FloatVector) |
| 71 | + .dimension(VECTOR_DIM) |
| 72 | + .build()); |
| 73 | + |
| 74 | + client.dropCollection(DropCollectionReq.builder() |
| 75 | + .collectionName(COLLECTION_NAME) |
| 76 | + .build()); |
| 77 | + |
| 78 | + CreateCollectionReq requestCreate = CreateCollectionReq.builder() |
| 79 | + .collectionName(COLLECTION_NAME) |
| 80 | + .collectionSchema(collectionSchema) |
| 81 | + .build(); |
| 82 | + client.createCollection(requestCreate); |
| 83 | + |
| 84 | + List<IndexParam> indexParams = new ArrayList<>(); |
| 85 | + indexParams.add(IndexParam.builder() |
| 86 | + .fieldName(VECTOR_FIELD) |
| 87 | + .indexType(IndexParam.IndexType.AUTOINDEX) |
| 88 | + .metricType(IndexParam.MetricType.COSINE) |
| 89 | + .build()); |
| 90 | + // geometry index no need metric type |
| 91 | + indexParams.add(IndexParam.builder() |
| 92 | + .fieldName(GEO_FIELD) |
| 93 | + .indexType(IndexParam.IndexType.RTREE) |
| 94 | + .build()); |
| 95 | + client.createIndex(CreateIndexReq.builder() |
| 96 | + .collectionName(COLLECTION_NAME) |
| 97 | + .indexParams(indexParams) |
| 98 | + .build()); |
| 99 | + client.loadCollection(LoadCollectionReq.builder() |
| 100 | + .collectionName(COLLECTION_NAME) |
| 101 | + .build()); |
| 102 | + System.out.println("Collection created: " + COLLECTION_NAME); |
| 103 | + } |
| 104 | + |
| 105 | + private static void insertGeometry(String geo) { |
| 106 | + JsonObject row = new JsonObject(); |
| 107 | + row.addProperty(GEO_FIELD, geo); |
| 108 | + row.add(VECTOR_FIELD, JsonUtils.toJsonTree(CommonUtils.generateFloatVector(VECTOR_DIM))); |
| 109 | + |
| 110 | + client.insert(InsertReq.builder() |
| 111 | + .collectionName(COLLECTION_NAME) |
| 112 | + .data(Collections.singletonList(row)) |
| 113 | + .build()); |
| 114 | + System.out.println("Inserted geometry: " + geo); |
| 115 | + } |
| 116 | + |
| 117 | + private static void printRowCount() { |
| 118 | + QueryResp countR = client.query(QueryReq.builder() |
| 119 | + .collectionName(COLLECTION_NAME) |
| 120 | + .outputFields(Collections.singletonList("count(*)")) |
| 121 | + .consistencyLevel(ConsistencyLevel.STRONG) |
| 122 | + .build()); |
| 123 | + System.out.printf("%d rows persisted\n", (long)countR.getQueryResults().get(0).getEntity().get("count(*)")); |
| 124 | + } |
| 125 | + |
| 126 | + private static void query(String filter) { |
| 127 | + System.out.println("==================================================="); |
| 128 | + System.out.println("Query with filter expression: " + filter); |
| 129 | + QueryResp queryResp = client.query(QueryReq.builder() |
| 130 | + .collectionName(COLLECTION_NAME) |
| 131 | + .filter(filter) |
| 132 | + .consistencyLevel(ConsistencyLevel.BOUNDED) |
| 133 | + .outputFields(Collections.singletonList(GEO_FIELD)) |
| 134 | + .build()); |
| 135 | + List<QueryResp.QueryResult> queryResults = queryResp.getQueryResults(); |
| 136 | + System.out.println("Query results:"); |
| 137 | + for (QueryResp.QueryResult result : queryResults) { |
| 138 | + System.out.println(result.getEntity()); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public static void main(String[] args) { |
| 143 | + createCollection(); |
| 144 | + insertGeometry("POINT (1 1)"); |
| 145 | + insertGeometry("LINESTRING (10 10, 10 30, 40 40)"); |
| 146 | + insertGeometry("POLYGON ((0 100, 100 100, 100 50, 0 50, 0 100))"); |
| 147 | + printRowCount(); |
| 148 | + |
| 149 | + query("ST_EQUALS(" + GEO_FIELD + ", 'POINT (1 1)')"); |
| 150 | + query("ST_TOUCHES(" + GEO_FIELD + ", 'LINESTRING (0 50, 0 100)')"); |
| 151 | + query("ST_CONTAINS(" + GEO_FIELD + ", 'POINT (70 70)')"); |
| 152 | + query("ST_CROSSES(" + GEO_FIELD + ", 'LINESTRING (20 0, 20 100)')"); |
| 153 | + query("ST_WITHIN(" + GEO_FIELD + ", 'POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))')"); |
| 154 | + } |
| 155 | +} |
0 commit comments