Skip to content

Commit

Permalink
Add sorting to search
Browse files Browse the repository at this point in the history
  • Loading branch information
habdelra committed Apr 5, 2024
1 parent fc27aba commit 230eebd
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 2 deletions.
99 changes: 99 additions & 0 deletions packages/host/tests/unit/query-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1173,4 +1173,103 @@ module('Unit | query', function (hooks) {
assert.strictEqual(meta.page.total, 1, 'the total results meta is correct');
assert.deepEqual(getIds(cards), [mango.id], 'results are correct');
});

test('can sort search results', async function (assert) {
let { mango, vangogh, ringo } = testCards;
await setupIndex(client, [
{
card: mango,
data: {
search_doc: {
name: 'Mango',
},
},
},
{
card: vangogh,
data: {
search_doc: {
name: 'Van Gogh',
},
},
},
{
card: ringo,
data: {
search_doc: {
name: 'Ringo',
},
},
},
]);

let { cards, meta } = await client.search(
{
sort: [
{
on: { module: `${testRealmURL}person`, name: 'Person' },
by: 'name',
},
],
},
loader,
);

assert.strictEqual(meta.page.total, 3, 'the total results meta is correct');
assert.deepEqual(
getIds(cards),
[mango.id, ringo.id, vangogh.id],
'results are correct',
);
});

test('can sort descending', async function (assert) {
let { mango, vangogh, ringo } = testCards;
await setupIndex(client, [
{
card: mango,
data: {
search_doc: {
name: 'Mango',
},
},
},
{
card: vangogh,
data: {
search_doc: {
name: 'Van Gogh',
},
},
},
{
card: ringo,
data: {
search_doc: {
name: 'Ringo',
},
},
},
]);

let { cards, meta } = await client.search(
{
sort: [
{
on: { module: `${testRealmURL}person`, name: 'Person' },
by: 'name',
direction: 'desc',
},
],
},
loader,
);

assert.strictEqual(meta.page.total, 3, 'the total results meta is correct');
assert.deepEqual(
getIds(cards),
[vangogh.id, ringo.id, mango.id],
'results are correct',
);
});
});
24 changes: 22 additions & 2 deletions packages/runtime-common/indexer/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
type EqFilter,
type NotFilter,
type ContainsFilter,
type Sort,
} from '../query';
import { type SerializedError } from '../error';
import { type DBAdapter } from '../db';
Expand Down Expand Up @@ -190,7 +191,7 @@ export class IndexerDBClient {
// which could have conflicting loaders. It is up to the caller to provide the
// loader that we should be using.
async search(
{ filter }: Query,
{ filter, sort }: Query,
loader: Loader,
opts?: QueryOptions,
// TODO this should be returning a CardCollectionDocument--handle that in
Expand All @@ -217,7 +218,7 @@ export class IndexerDBClient {
// use a default sort for deterministic ordering, refactor this after
// adding sort support to the query
'GROUP BY card_url',
'ORDER BY card_url',
...this.orderExpression(sort),
];
let queryCount = [
'SELECT count(DISTINCT card_url) as total',
Expand All @@ -241,6 +242,25 @@ export class IndexerDBClient {
return { cards, meta };
}

private orderExpression(sort: Sort | undefined): CardExpression {
if (!sort) {
return ['ORDER BY card_url'];
}
return [
'ORDER BY',
...separatedByCommas([
...sort.map((s) => [
// intentionally not using field arity here--not sure what it means to
// sort via a plural field
fieldQuery(s.by, s.on, 'sort'),
s.direction ?? 'asc',
]),
// we include 'card_url' as the final sort key for deterministic results
['card_url'],
]),
];
}

private filterCondition(filter: Filter, onRef: CodeRef): CardExpression {
if ('type' in filter) {
return this.typeCondition(filter.type);
Expand Down

0 comments on commit 230eebd

Please sign in to comment.