Skip to content

Commit

Permalink
feat(mongo): allow setting weights on index
Browse files Browse the repository at this point in the history
Closes #4172
  • Loading branch information
B4nan committed Apr 23, 2023
1 parent 8e8bc38 commit 299b188
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
33 changes: 24 additions & 9 deletions docs/docs/usage-with-mongo.md
Expand Up @@ -120,17 +120,32 @@ Alternatively you can call `ensureIndexes()` method on the `SchemaGenerator`:
await orm.getSchemaGenerator().ensureIndexes();
```

> You can pass additional index/unique options via `options` parameter:
>
> `@Unique({ options: { partialFilterExpression: { name: { $exists: true } } }})`
You can pass additional index/unique options via `options` parameter:

> You can also create text indexes by passing `type` parameter:
>
> `@Index({ properties: ['name', 'caption'], type: 'text' })`
```ts
@Unique({ options: { partialFilterExpression: { name: { $exists: true } } }})
```

> If you provide only `options` in the index definition, it will be used as is, this allows to define any kind of index:
>
> `@Index({ options: { point: '2dsphere', title: -1 } })`
You can also create text indexes by passing `type` parameter:

```ts
@Index({ properties: ['name', 'caption'], type: 'text' })
```

If you provide only `options` in the index definition, it will be used as is, this allows to define any kind of index:

```ts
@Index({ options: { point: '2dsphere', title: -1 } })
```

To set index weights, you can pass a tuple to the `options` parameter:

```ts
@Index({ options: [
{ title: 'text', perex: 'text', key: 1 },
{ weights: { title: 10, perex: 5 } },
] })
```

## Native collection methods

Expand Down
4 changes: 4 additions & 0 deletions packages/mongodb/src/MongoSchemaGenerator.ts
Expand Up @@ -142,6 +142,10 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator<MongoDriver> {
const properties = Utils.flatten(Utils.asArray(index.properties).map(prop => meta.properties[prop].fieldNames));
const collection = this.connection.getCollection(meta.name!);

if (Array.isArray(index.options) && index.options.length === 2 && properties.length === 0) {
return res.push([collection.collectionName, collection.createIndex(index.options[0], index.options[1])]);
}

if (index.options && properties.length === 0) {
return res.push([collection.collectionName, collection.createIndex(index.options)]);
}
Expand Down
15 changes: 14 additions & 1 deletion tests/entities/FooBar.ts
@@ -1,8 +1,21 @@
import { ObjectId } from 'bson';
import { ArrayType, Entity, JsonType, OneToOne, PrimaryKey, Property, SerializedPrimaryKey } from '@mikro-orm/core';
import {
ArrayType,
Entity,
Index,
JsonType,
OneToOne,
PrimaryKey,
Property,
SerializedPrimaryKey,
} from '@mikro-orm/core';
import { FooBaz } from './FooBaz';

@Entity()
@Index({ options: [
{ name: 'text', str: 'text', baz: 1 },
{ weights: { name: 10, str: 5 } },
] })
export default class FooBar {

@PrimaryKey()
Expand Down

0 comments on commit 299b188

Please sign in to comment.