|
| 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.service.vector.request.ranker; |
| 21 | + |
| 22 | +import io.milvus.common.clientenum.FunctionType; |
| 23 | +import io.milvus.common.utils.JsonUtils; |
| 24 | +import io.milvus.v2.service.collection.request.CreateCollectionReq; |
| 25 | +import lombok.experimental.SuperBuilder; |
| 26 | +import org.apache.commons.lang3.StringUtils; |
| 27 | + |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +/** |
| 32 | + * The Decay reranking strategy, which by adjusting search rankings based on numeric field values. |
| 33 | + * Read the doc for more info: https://milvus.io/docs/decay-ranker-overview.md |
| 34 | + * |
| 35 | + * You also can declare a decay ranker by Function |
| 36 | + * CreateCollectionReq.Function rr = CreateCollectionReq.Function.builder() |
| 37 | + * .functionType(FunctionType.RERANK) |
| 38 | + * .name("xxx_boost") |
| 39 | + * .description("random on xxx") |
| 40 | + * .param("reranker", "boost") |
| 41 | + * .param("filter", "xx == 2") |
| 42 | + * .param("weight", "0.5") |
| 43 | + * .param("random_score", "{\"seed\": 123, \"field\": \"id\"}") |
| 44 | + * .build(); |
| 45 | + */ |
| 46 | +@SuperBuilder |
| 47 | +public class BoostRanker extends CreateCollectionReq.Function { |
| 48 | + private String filter; |
| 49 | + private Float weight; |
| 50 | + private Long randomScoreSeed; |
| 51 | + private String randomScoreField; |
| 52 | + |
| 53 | + public FunctionType getFunctionType() { |
| 54 | + return FunctionType.RERANK; |
| 55 | + } |
| 56 | + |
| 57 | + public Map<String, String> getParams() { |
| 58 | + Map<String, String> props = super.getParams(); |
| 59 | + props.put("reranker", "boost"); |
| 60 | + if (!StringUtils.isEmpty(filter)) { |
| 61 | + props.put("filter", filter); |
| 62 | + } |
| 63 | + if (weight != null) { |
| 64 | + props.put("weight", weight.toString()); |
| 65 | + } |
| 66 | + |
| 67 | + Map<String, Object> randomScore = new HashMap<>(); |
| 68 | + if (randomScoreSeed != null) { |
| 69 | + randomScore.put("seed", randomScoreSeed); |
| 70 | + } |
| 71 | + if (!StringUtils.isEmpty(randomScoreField)) { |
| 72 | + randomScore.put("field", randomScoreField); |
| 73 | + } |
| 74 | + if (!randomScore.isEmpty()) { |
| 75 | + props.put("random_score", JsonUtils.toJson(randomScore)); |
| 76 | + } |
| 77 | + return props; |
| 78 | + } |
| 79 | +} |
0 commit comments