|
| 1 | +/* |
| 2 | + * Copyright 2024 dengliming. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.github.dengliming.redismodule.examples.vectorset; |
| 18 | + |
| 19 | +import io.github.dengliming.redismodule.vectorset.VectorSet; |
| 20 | +import io.github.dengliming.redismodule.vectorset.args.AddArgs; |
| 21 | +import io.github.dengliming.redismodule.vectorset.args.SimilarArgs; |
| 22 | +import io.github.dengliming.redismodule.vectorset.client.VectorSetClient; |
| 23 | +import io.github.dengliming.redismodule.vectorset.model.Similarity; |
| 24 | +import org.redisson.config.Config; |
| 25 | + |
| 26 | +public class Examples { |
| 27 | + |
| 28 | + public static void main(String[] args) { |
| 29 | + Config config = new Config(); |
| 30 | + config.useSingleServer().setAddress("redis://127.0.0.1:6379"); |
| 31 | + VectorSetClient client = new VectorSetClient(config); |
| 32 | + |
| 33 | + VectorSet movies = client.getVectorSet("movies"); |
| 34 | + movies.add("matrix", new double[]{0.9, 0.1, 0.0}, new AddArgs().attributes("{\"year\":1999,\"genre\":\"sci-fi\"}")); |
| 35 | + movies.add("inception", new double[]{0.8, 0.2, 0.1}, new AddArgs().attributes("{\"year\":2010,\"genre\":\"sci-fi\"}")); |
| 36 | + movies.add("amelie", new double[]{0.0, 0.2, 0.9}, new AddArgs().attributes("{\"year\":2001,\"genre\":\"romance\"}")); |
| 37 | + |
| 38 | + // nearest neighbours of a query vector, with scores and attributes |
| 39 | + for (Similarity hit : movies.similar(new double[]{1.0, 0.0, 0.0}, new SimilarArgs().withScores().withAttribs().count(2))) { |
| 40 | + System.out.println(hit.getElement() + " " + hit.getScore() + " " + hit.getAttributes()); |
| 41 | + } |
| 42 | + |
| 43 | + // neighbours of an existing element, filtered on attributes |
| 44 | + for (Similarity hit : movies.similarTo("matrix", new SimilarArgs().filter(".year > 2000"))) { |
| 45 | + System.out.println("after 2000: " + hit.getElement()); |
| 46 | + } |
| 47 | + |
| 48 | + System.out.println(movies.cardinality() + " vectors of " + movies.dimension() + " dimensions"); |
| 49 | + System.out.println(movies.members()); |
| 50 | + |
| 51 | + movies.delete(); |
| 52 | + client.shutdown(); |
| 53 | + } |
| 54 | +} |
0 commit comments