Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions generator/config/stage/scoreFusion.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# yaml-language-server: $schema=../schema.json
name: $scoreFusion
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/scoreFusion/'
type:
- stage
encode: object
description: |
Combines multiple pipelines using relative score fusion to create hybrid search results.
arguments:
-
name: input
type:
- object
description: |
An object with the following required fields:
- input.pipelines: Map from name to input pipeline. Each pipeline must be operating on the same collection. Minimum of one pipeline.
- input.normalization: Normalizes the score to the range 0 to 1 before combining the results. Value can be none, sigmoid or minMaxScaler.
-
name: combination
optional: true
type:
- object
description: |
An object with the following optional fields:
- combination.weights: Map from pipeline name to numbers (non-negative). If unspecified, default weight is 1 for each pipeline.
- combination.method: Specifies method for combining scores. Value can be avg or expression. Default is avg.
- combination.expression: This is the custom expression that is used when combination.method is set to expression.
-
name: scoreDetails
type:
- bool
default: false
description: Set to true to include detailed scoring information.
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/scoreFusion/#examples'
pipeline:
-
$scoreFusion:
input:
pipelines:
searchOne:
-
$vectorSearch:
index: 'vector_index'
path: 'plot_embedding'
queryVector: [-0.0016261312, -0.028070757, -0.011342932]
numCandidates: 150
limit: 10
searchTwo:
-
$search:
index: '<INDEX_NAME>'
text:
query: '<QUERY_TERM>'
path: '<FIELD_NAME>'
normalization: 'sigmoid'
combination:
method: 'expression'
expression:
$sum:
-
$multiply:
- '$$searchOne'
- 10
- '$$searchTwo'
scoreDetails: true
-
$project:
_id: 1
title: 1
plot: 1
scoreDetails:
$meta: 'scoreDetails'
-
$limit: 20
21 changes: 21 additions & 0 deletions src/Builder/Stage/FactoryTrait.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions src/Builder/Stage/FluentFactoryTrait.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions src/Builder/Stage/ScoreFusionStage.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

93 changes: 93 additions & 0 deletions tests/Builder/Stage/Pipelines.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions tests/Builder/Stage/ScoreFusionStageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace MongoDB\Tests\Builder\Stage;

use MongoDB\Builder\Pipeline;
use MongoDB\Builder\Stage;
use MongoDB\Tests\Builder\PipelineTestCase;

/**
* Test $scoreFusion stage
*/
class ScoreFusionStageTest extends PipelineTestCase
{
public function testExample(): void
{
$pipeline = new Pipeline(
Stage::scoreFusion(
input: [
'pipelines' => [
'searchOne' => [
[
'$vectorSearch' => [
'index' => 'vector_index',
'path' => 'plot_embedding',
'queryVector' => [-0.0016261312, -0.028070757, -0.011342932],
'numCandidates' => 150,
'limit' => 10,
],
],
],
'searchTwo' => [
[
'$search' => [
'index' => '<INDEX_NAME>',
'text' => [
'query' => '<QUERY_TERM>',
'path' => '<FIELD_NAME>',
],
],
],
],
],
'normalization' => 'sigmoid',
],
combination: [
'method' => 'expression',
'expression' => [
'$sum' => [
[
'$multiply' => [
'$$searchOne',
10,
],
],
'$$searchTwo',
],
],
],
scoreDetails: true,
),
Stage::project(
_id: 1,
title: 1,
plot: 1,
scoreDetails: ['$meta' => 'scoreDetails'],
),
Stage::limit(20),
);
$this->assertSamePipeline(Pipelines::ScoreFusionExample, $pipeline);
}
}
Loading