core icon indicating copy to clipboard operation
core copied to clipboard

feat(mongodb): Add MongoDB Atlas Search filter

Open GromNaN opened this issue 5 months ago • 0 comments

Q A
Branch? main
Tickets -
License MIT
Doc PR TODO

This is a work in progress

The MongoDB $search aggregation uses a Lucene-based index for full-text search. This stage must be the first of the aggregation pipeline.

This is different form the existing ODM SearchFilter which uses the standard query operators.

Todo:

  • [ ] Move sort in the $search stage
  • [ ] Move limit in the $search stage
  • [ ] Add tests
  • [ ] Add doc

Usage

#[ApiResource(
    shortName: 'products',
    operations: [
        new GetCollection(
            parameters: [
                'name' => new QueryParameter(
                    schema: ['type' => 'string', 'default' => null],
                    filter: new AtlasSearchFilter(
                        index: 'search_index',
                        operator: 'text',
                        term: 'must'
                    ),
                ),
                'description' => new QueryParameter(
                    schema: ['type' => 'string', 'default' => null],
                    filter: new AtlasSearchFilter(
                        index: 'search_index',
                        operator: 'text',
                        term: 'should',
                    ),
                ),
                'search' => new QueryParameter(
                    schema: ['type' => 'string', 'default' => null],
                    filter: new AtlasSearchFilter(
                        index: 'search_index',
                        operator: 'queryString',
                        term: 'must',
                    ),
                    property: 'name',
                ),
            ]
        ),
    ]
)]
#[MongoDB\Document(collection: 'products')]
#[MongoDB\SearchIndex(name: 'search_index', dynamic: true)]
class Product
{
    #[MongoDB\Id] public string $id;
    #[MongoDB\Field] public ?string $name;
    #[MongoDB\Field] public ?string $description;
    #[MongoDB\Field] public ?string $brand;
}

GromNaN avatar Sep 15 '25 13:09 GromNaN