Skip to content

Commit 5e1afd0

Browse files
committed
fix: map query to and from _id alias
1 parent b80a3b5 commit 5e1afd0

File tree

3 files changed

+45
-15
lines changed

3 files changed

+45
-15
lines changed

adapter.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,13 @@ const { Async } = crocks;
5555
* @property {string} key
5656
*
5757
* @typedef {Object} SearchOptions
58-
* @property {Array<string>} fields
59-
* @property {Object} boost
60-
* @property {boolean} prefix
58+
* @property {string} query
59+
* @property {Array<string>} [fields]
60+
* @property {Object} [filter]
6161
*
6262
* @typedef {Object} SearchQuery
6363
* @property {string} index
64-
* @property {string} query
65-
* @property {SearchOptions} [options]
64+
* @property {SearchOptions} q
6665
*
6766
* @typedef {Object} Response
6867
* @property {boolean} ok

adapter_test.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
removeDocPath,
1313
updateDocPath,
1414
} from "./paths.js";
15-
import { moveUnderscoreId } from "./utils.js";
15+
import { moveUnderscoreId, underscoreIdAlias } from "./utils.js";
1616

1717
const headers = createHeaders("admin", "password");
1818

@@ -126,7 +126,7 @@ Deno.test("create index - maps _id", async () => {
126126
method: "PUT",
127127
headers,
128128
body:
129-
'{"mappings":{"properties":{"title":{"type":"text"},"__movedUnderscoreId63__":{"type":"text"}}}}',
129+
`{"mappings":{"properties":{"title":{"type":"text"},"${underscoreIdAlias}":{"type":"text"}}}}`,
130130
}],
131131
});
132132

@@ -540,21 +540,43 @@ Deno.test("query", async () => {
540540
index: "movies",
541541
q: {
542542
query: "gatsby",
543-
fields: ["title"],
543+
fields: ["title", "_id"],
544544
filter: {
545545
rating: 4,
546+
_id: "id-1",
546547
},
547548
},
548549
});
549550

550-
assertObjectMatch(fetch.calls.pop(), {
551+
const call = fetch.calls.pop();
552+
assertObjectMatch(call, {
551553
args: [queryPath(ES, INDEX), {
552554
method: "POST",
553555
headers,
554-
// TODO: Tyler. Assert body here eventually
555556
}],
556557
});
557558

559+
let { args: [, { body }] } = call;
560+
body = JSON.parse(body);
561+
562+
assertObjectMatch(body, {
563+
query: {
564+
bool: {
565+
must: {
566+
multi_match: {
567+
query: "gatsby",
568+
fuzziness: "AUTO",
569+
fields: ["title", underscoreIdAlias],
570+
},
571+
},
572+
filter: [
573+
{ term: { rating: 4 } },
574+
{ term: { [underscoreIdAlias]: "id-1" } },
575+
],
576+
},
577+
},
578+
});
579+
558580
assertEquals(result.ok, true);
559581
assert(result.matches);
560582
assertEquals(result.matches.length, 2);

utils.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ const {
3535
toPairs,
3636
} = R;
3737

38+
export const underscoreIdAlias = "__movedUnderscoreId63__";
39+
3840
const isDefined = complement(isNil);
3941
const isEmptyObject = allPass([
4042
complement(is(Array)), // not an array
@@ -176,13 +178,13 @@ const swap = (old, cur) =>
176178

177179
export const moveUnderscoreId = ifElse(
178180
has("_id"),
179-
swap("_id", "__movedUnderscoreId63__"),
181+
swap("_id", underscoreIdAlias),
180182
identity,
181183
);
182184

183185
export const toUnderscoreId = ifElse(
184-
has("__movedUnderscoreId63__"),
185-
swap("__movedUnderscoreId63__", "_id"),
186+
has(underscoreIdAlias),
187+
swap(underscoreIdAlias, "_id"),
186188
identity,
187189
);
188190

@@ -244,11 +246,18 @@ export const queryToEsQuery = ({ query, fields, filter }) => ({
244246
query,
245247
// See https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html#query-dsl-match-query-fuzziness
246248
fuzziness: "AUTO",
247-
fields,
249+
// map _id => underscoreIdAlias
250+
fields: fields
251+
? fields.map((field) => field === "_id" ? underscoreIdAlias : field)
252+
: undefined,
248253
},
249254
},
250255
filter: toPairs(filter).map(
251-
([key, value]) => ({ term: { [key]: value } }),
256+
// map _id => underscoreIdAlias
257+
([key, value]) =>
258+
key === "_id"
259+
? ({ term: { [underscoreIdAlias]: value } })
260+
: ({ term: { [key]: value } }),
252261
),
253262
},
254263
},

0 commit comments

Comments
 (0)