Skip to content

Commit 6998fdb

Browse files
committed
suggest examples
1 parent c2056e8 commit 6998fdb

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,38 @@ const revisions = await session.advanced.revisions.getFor("users/1");
699699
// id: 'users/1' } ]
700700
```
701701

702+
### Suggestions
703+
```javascript
704+
// Having data:
705+
// [ User {
706+
// name: 'John',
707+
// age: 30,
708+
// registeredAt: 2017-11-10T23:00:00.000Z,
709+
// kids: [Array],
710+
// id: 'users/1-A' },
711+
// User {
712+
// name: 'Stefanie',
713+
// age: 25,
714+
// registeredAt: 2015-07-29T22:00:00.000Z,
715+
// id: 'users/2-A' } ]
716+
717+
// and a static index like:
718+
class UsersIndex extends AbstractIndexCreationTask {
719+
constructor() {
720+
super();
721+
this.map = "from doc in docs.Users select new { doc.name }";
722+
this.suggestion("name");
723+
}
724+
}
725+
726+
// ...
727+
728+
const session = store.openSession();
729+
const suggestionQueryResult = await session.query({ collection: "users" })
730+
.suggestUsing(x => x.byField("name", "Jon"))
731+
.execute();
732+
// { name: { name: 'name', suggestions: [ 'john' ] } }
733+
```
702734

703735

704736
## Using object literals for entities

test/Documents/ReadmeSamples.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
IDocumentSession,
1212
QueryStatistics,
1313
StreamQueryStatistics,
14+
AbstractIndexCreationTask,
1415
} from "../../src";
1516
import { TypeUtil } from "../../src/Utility/TypeUtil";
1617

@@ -406,6 +407,27 @@ describe("Readme query samples", function () {
406407
});
407408
});
408409

410+
it("can suggest", async () => {
411+
class UsersIndex extends AbstractIndexCreationTask {
412+
constructor() {
413+
super();
414+
this.map = "from doc in docs.Users select new { doc.name }";
415+
this.suggestion("name");
416+
}
417+
}
418+
419+
await store.executeIndex(new UsersIndex());
420+
await testContext.waitForIndexing(store);
421+
422+
{
423+
const session = store.openSession();
424+
const suggestionQueryResult = await session.query({ indexName: "UsersIndex" })
425+
.suggestUsing(x => x.byField("name", "Jon"))
426+
.execute();
427+
assert.strictEqual(suggestionQueryResult.name.suggestions.length, 1);
428+
}
429+
});
430+
409431
});
410432

411433
describe("with revisions set up", function() {

0 commit comments

Comments
 (0)