Skip to content

Commit 3d72edb

Browse files
authored
Merge pull request #3347 from liyun95/v2.6.x
update ranker docs
2 parents 9473de9 + 6d9dd10 commit 3d72edb

File tree

5 files changed

+698
-325
lines changed

5 files changed

+698
-325
lines changed

site/en/userGuide/embeddings-reranking/reranking/boost-ranker.md

Lines changed: 220 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,14 @@ In this section, you will see examples of how to use Boost Ranker to influence t
388388

389389
Before passing a Boost Ranker as the reranker of a search request, you should properly define the Boost Ranker as a reranking function as follows:
390390

391+
<div class="multipleCode">
392+
<a href="#python">Python</a>
393+
<a href="#java">Java</a>
394+
<a href="#go">Go</a>
395+
<a href="#javascript">NodeJS</a>
396+
<a href="#bash">cURL</a>
397+
</div>
398+
391399
```python
392400
from pymilvus import Function, FunctionType
393401

@@ -407,6 +415,46 @@ ranker = Function(
407415
)
408416
```
409417

418+
```java
419+
import io.milvus.v2.service.vector.request.ranker.BoostRanker;
420+
421+
BoostRanker ranker = BoostRanker.builder()
422+
.name("boost")
423+
.filter("doctype == \"abstract\"")
424+
.weight(5.0f)
425+
.randomScoreField("id")
426+
.randomScoreSeed(126)
427+
.build();
428+
```
429+
430+
```go
431+
// go
432+
```
433+
434+
```javascript
435+
import {FunctionType} from '@zilliz/milvus2-sdk-node';
436+
437+
const ranker = {
438+
name: "boost",
439+
input_field_names: [],
440+
type: FunctionType.RERANK,
441+
params: {
442+
reranker: "boost",
443+
filter: "doctype == 'abstract'",
444+
random_score: {
445+
seed: 126,
446+
field: "id",
447+
},
448+
weight: 0.5,
449+
},
450+
};
451+
452+
```
453+
454+
```bash
455+
# restful
456+
```
457+
410458
<table>
411459
<tr>
412460
<th><p>Parameter</p></th>
@@ -418,12 +466,12 @@ ranker = Function(
418466
<td><p><code>name</code></p></td>
419467
<td><p>Yes</p></td>
420468
<td><p>Unique identifier for this Function</p></td>
421-
<td><p><code>"rrf"</code></p></td>
469+
<td><p><code>"boost"</code></p></td>
422470
</tr>
423471
<tr>
424472
<td><p><code>input_field_names</code></p></td>
425473
<td><p>Yes</p></td>
426-
<td><p>List of vector fields to apply the function to (must be empty for RRF Ranker)</p></td>
474+
<td><p>List of vector fields to apply the function to (must be empty for Boost Ranker)</p></td>
427475
<td><p><code>[]</code></p></td>
428476
</tr>
429477
<tr>
@@ -462,6 +510,14 @@ ranker = Function(
462510

463511
Once the Boost Ranker function is ready, you can reference it in a search request. The following example assumes that you have already created a collection that has the following fields: **id**, **vector**, and **doctype**.
464512

513+
<div class="multipleCode">
514+
<a href="#python">Python</a>
515+
<a href="#java">Java</a>
516+
<a href="#go">Go</a>
517+
<a href="#javascript">NodeJS</a>
518+
<a href="#bash">cURL</a>
519+
</div>
520+
465521
```python
466522
from pymilvus import MilvusClient
467523

@@ -475,20 +531,84 @@ client = MilvusClient(
475531

476532
# Conduct a similarity search using the created ranker
477533
client.search(
478-
data=[-0.619954382375778, 0.4479436794798608, -0.17493894838751745, -0.4248030059917294, -0.8648452746018911],
534+
collection_name="my_collection",
535+
data=[[-0.619954382375778, 0.4479436794798608, -0.17493894838751745, -0.4248030059917294, -0.8648452746018911]],
479536
anns_field="vector",
480537
params={},
481538
output_field=["doctype"],
482539
ranker=ranker
483540
)
484541
```
485542

543+
```java
544+
import io.milvus.v2.client.ConnectConfig;
545+
import io.milvus.v2.client.MilvusClientV2;
546+
import io.milvus.v2.service.vector.request.SearchReq;
547+
import io.milvus.v2.service.vector.response.SearchResp;
548+
import io.milvus.v2.service.vector.request.data.FloatVec;
549+
550+
MilvusClientV2 client = new MilvusClientV2(ConnectConfig.builder()
551+
.uri("http://localhost:19530")
552+
.token("root:Milvus")
553+
.build());
554+
555+
SearchResp searchReq = client.search(SearchReq.builder()
556+
.collectionName("my_collection")
557+
.data(Collections.singletonList(new FloatVec(new float[]{-0.619954f, 0.447943f, -0.174938f, -0.424803f, -0.864845f})))
558+
.annsField("vector")
559+
.outputFields(Collections.singletonList("doctype"))
560+
.functionScore(FunctionScore.builder()
561+
.addFunction(ranker)
562+
.build())
563+
.build());
564+
SearchResp searchResp = client.search(searchReq);
565+
```
566+
567+
```go
568+
// go
569+
```
570+
571+
```javascript
572+
import { MilvusClient } from '@zilliz/milvus2-sdk-node';
573+
574+
// Connect to the Milvus server
575+
const client = new MilvusClient({
576+
address: 'localhost:19530',
577+
token: 'root:Milvus'
578+
});
579+
580+
// Assume you have a collection set up
581+
582+
// Conduct a similarity search
583+
const searchResults = await client.search({
584+
collection_name: 'my_collection',
585+
data: [-0.619954382375778, 0.4479436794798608, -0.17493894838751745, -0.4248030059917294, -0.8648452746018911],
586+
anns_field: 'vector',
587+
output_fields: ['doctype'],
588+
rerank: ranker,
589+
});
590+
591+
console.log('Search results:', searchResults);
592+
```
593+
594+
```bash
595+
# restful
596+
```
597+
486598
### Search with multiple Boost Rankers
487599

488600
You can combine multiple Boost Rankers in a single search to influence the search results. To do so, create several Boost Rankers, reference them in a **FunctionScore** instance, and use the **FunctionScore** instance as the ranker in the search request.
489601

490602
The following example shows how to modify the scores of all identified entities by applying a weight between **0.8** and **1.2**.
491603

604+
<div class="multipleCode">
605+
<a href="#python">Python</a>
606+
<a href="#java">Java</a>
607+
<a href="#go">Go</a>
608+
<a href="#javascript">NodeJS</a>
609+
<a href="#bash">cURL</a>
610+
</div>
611+
492612
```python
493613
from pymilvus import MilvusClient, Function, FunctionType, FunctionScore
494614

@@ -523,22 +643,114 @@ ranker = FunctionScore(
523643
fix_weight_ranker,
524644
random_weight_ranker
525645
],
526-
params: {
527-
"boost_mode": "Multiply"
646+
params={
647+
"boost_mode": "Multiply",
528648
"function_mode": "Sum"
529649
}
530650
)
531651

532652
# Conduct a similarity search using the created Function Score
533653
client.search(
534-
data=[-0.619954382375778, 0.4479436794798608, -0.17493894838751745, -0.4248030059917294, -0.8648452746018911],
654+
collection_name="my_collection",
655+
data=[[-0.619954382375778, 0.4479436794798608, -0.17493894838751745, -0.4248030059917294, -0.8648452746018911]],
535656
anns_field="vector",
536657
params={},
537658
output_field=["doctype"],
538659
ranker=ranker
539660
)
540661
```
541662

663+
```java
664+
import io.milvus.common.clientenum.FunctionType;
665+
import io.milvus.v2.service.collection.request.CreateCollectionReq;
666+
667+
CreateCollectionReq.Function fixWeightRanker = CreateCollectionReq.Function.builder()
668+
.functionType(FunctionType.RERANK)
669+
.name("boost")
670+
.param("reranker", "boost")
671+
.param("weight", "0.8")
672+
.build();
673+
674+
CreateCollectionReq.Function randomWeightRanker = CreateCollectionReq.Function.builder()
675+
.functionType(FunctionType.RERANK)
676+
.name("boost")
677+
.param("reranker", "boost")
678+
.param("weight", "0.4")
679+
.param("random_score", "{\"seed\": 126}")
680+
.build();
681+
682+
Map<String, String> params = new HashMap<>();
683+
params.put("boost_mode","Multiply");
684+
params.put("function_mode","Sum");
685+
FunctionScore ranker = FunctionScore.builder()
686+
.addFunction(fixWeightRanker)
687+
.addFunction(randomWeightRanker)
688+
.params(params)
689+
.build()
690+
691+
SearchResp searchReq = client.search(SearchReq.builder()
692+
.collectionName("my_collection")
693+
.data(Collections.singletonList(new FloatVec(new float[]{-0.619954f, 0.447943f, -0.174938f, -0.424803f, -0.864845f})))
694+
.annsField("vector")
695+
.outputFields(Collections.singletonList("doctype"))
696+
.addFunction(ranker)
697+
.build());
698+
SearchResp searchResp = client.search(searchReq);
699+
```
700+
701+
```go
702+
// go
703+
```
704+
705+
```javascript
706+
import {FunctionType} from '@zilliz/milvus2-sdk-node';
707+
708+
const fix_weight_ranker = {
709+
name: "boost",
710+
input_field_names: [],
711+
type: FunctionType.RERANK,
712+
params: {
713+
reranker: "boost",
714+
weight: 0.8,
715+
},
716+
};
717+
718+
const random_weight_ranker = {
719+
name: "boost",
720+
input_field_names: [],
721+
type: FunctionType.RERANK,
722+
params: {
723+
reranker: "boost",
724+
random_score: {
725+
seed: 126,
726+
},
727+
weight: 0.4,
728+
},
729+
};
730+
731+
const ranker = {
732+
functions: [fix_weight_ranker, random_weight_ranker],
733+
params: {
734+
boost_mode: "Multiply",
735+
function_mode: "Sum",
736+
},
737+
};
738+
739+
await client.search({
740+
collection_name: "my_collection",
741+
data: [[-0.619954382375778, 0.4479436794798608, -0.17493894838751745, -0.4248030059917294, -0.8648452746018911]],
742+
anns_field: "vector",
743+
params: {},
744+
output_field: ["doctype"],
745+
ranker: ranker
746+
});
747+
748+
```
749+
750+
```bash
751+
# restful
752+
```
753+
542754
Specifically, there are two Boost Rankers: one applies a fixed weight to all found entities, while the other assigns a random weight to them. Then, we reference these two rankers in a **FunctionScore**, which also defines how the weights influence the scores of the found entities.
543755

544756
The following table lists the parameters required to create a **FunctionScore** instance.
@@ -559,13 +771,13 @@ The following table lists the parameters required to create a **FunctionScore**
559771
<tr>
560772
<td><p><code>params.boost_mode</code></p></td>
561773
<td><p>No</p></td>
562-
<td><p>Specifies how the specified weights influence the scores of any matching entities.</p><p>Possible values are:</p><ul><li><p><code>Multiple</code></p><p>Indicates that the weighted value is equal to the original score of a matching entity multiplied by the specified weight. </p><p>This is the default value.</p></li><li><p><code>Sum</code></p><p>Indicates that the weighted value is equal to the sum of the original score of a matching entity and the specified weight</p></li></ul></td>
774+
<td><p>Specifies how the specified weights influence the scores of any matching entities.</p><p>Possible values are:</p><ul><li><p><code>Multiply</code></p><p>Indicates that the weighted value is equal to the original score of a matching entity multiplied by the specified weight. </p><p>This is the default value.</p></li><li><p><code>Sum</code></p><p>Indicates that the weighted value is equal to the sum of the original score of a matching entity and the specified weight</p></li></ul></td>
563775
<td><p><code>"Sum"</code></p></td>
564776
</tr>
565777
<tr>
566778
<td><p><code>params.function_mode</code></p></td>
567779
<td><p>No</p></td>
568-
<td><p>Specifies how the weighted values from various Boost Rankers are processed.</p><p>Possible values are:</p><ul><li><p><code>Multiplify</code></p><p>Indicates that the final score of a matching entity is equal to the product of the weighted values from all Boost Rankers.</p><p>This is the default value.</p></li><li><p><code>Sum</code></p><p>Indicates that the final score of a matching entity is equal to the sum of the weighted values from all Boost Rankers.</p></li></ul></td>
780+
<td><p>Specifies how the weighted values from various Boost Rankers are processed.</p><p>Possible values are:</p><ul><li><p><code>Multiply</code></p><p>Indicates that the final score of a matching entity is equal to the product of the weighted values from all Boost Rankers.</p><p>This is the default value.</p></li><li><p><code>Sum</code></p><p>Indicates that the final score of a matching entity is equal to the sum of the weighted values from all Boost Rankers.</p></li></ul></td>
569781
<td><p><code>"Sum"</code></p></td>
570782
</tr>
571783
</table>

site/en/userGuide/embeddings-reranking/reranking/exponential-decay.md

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -159,20 +159,18 @@ ranker = Function(
159159
```
160160

161161
```java
162-
import io.milvus.common.clientenum.FunctionType;
163-
import io.milvus.v2.service.collection.request.CreateCollectionReq;
164-
165-
CreateCollectionReq.Function ranker = CreateCollectionReq.Function.builder()
166-
.functionType(FunctionType.RERANK)
167-
.name("news_recency")
168-
.inputFieldNames(Collections.singletonList("publish_time"))
169-
.param("reranker", "decay")
170-
.param("function", "exp")
171-
.param("origin", String.valueOf(System.currentTimeMillis()))
172-
.param("offset", String.valueOf(3 * 60 * 60))
173-
.param("decay", "0.5")
174-
.param("scale", String.valueOf(24 * 60 * 60))
175-
.build();
162+
import io.milvus.v2.service.vector.request.ranker.DecayRanker;
163+
164+
DecayRanker ranker = DecayRanker.builder()
165+
.name("news_recency")
166+
.inputFieldNames(Collections.singletonList("publish_time"))
167+
.function("exp")
168+
.origin(System.currentTimeMillis())
169+
.offset(3 * 60 * 60)
170+
.decay(0.5)
171+
.scale(24 * 60 * 60)
172+
.build();
173+
176174
```
177175

178176
```javascript

site/en/userGuide/embeddings-reranking/reranking/linear-decay.md

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -166,20 +166,18 @@ ranker = Function(
166166
```
167167

168168
```java
169-
import io.milvus.common.clientenum.FunctionType;
170-
import io.milvus.v2.service.collection.request.CreateCollectionReq;
171-
172-
CreateCollectionReq.Function ranker = CreateCollectionReq.Function.builder()
173-
.functionType(FunctionType.RERANK)
174-
.name("event_relevance")
175-
.inputFieldNames(Collections.singletonList("event_date"))
176-
.param("reranker", "decay")
177-
.param("function", "linear")
178-
.param("origin", String.valueOf(System.currentTimeMillis()/1000))
179-
.param("offset", String.valueOf(12 * 60 * 60))
180-
.param("decay", "0.5")
181-
.param("scale", String.valueOf(7 * 24 * 60 * 60))
182-
.build();
169+
import io.milvus.v2.service.vector.request.ranker.DecayRanker;
170+
171+
DecayRanker ranker = DecayRanker.builder()
172+
.name("event_relevance")
173+
.inputFieldNames(Collections.singletonList("event_date"))
174+
.function("linear")
175+
.origin(System.currentTimeMillis())
176+
.offset(12 * 60 * 60)
177+
.decay(0.5)
178+
.scale(7 * 24 * 60 * 60)
179+
.build();
180+
183181
```
184182

185183
```javascript

0 commit comments

Comments
 (0)