diff --git a/packages/server/__mocks__/node-fetch.js b/packages/server/__mocks__/node-fetch.js index 3cc412b1c6..d023802582 100644 --- a/packages/server/__mocks__/node-fetch.js +++ b/packages/server/__mocks__/node-fetch.js @@ -30,6 +30,17 @@ module.exports = async (url, opts) => { }, 404 ) + } else if (url.includes("_search")) { + return json({ + rows: [ + { + doc: { + _id: "test", + }, + }, + ], + bookmark: "test", + }) } return fetch(url, opts) } diff --git a/packages/server/src/api/controllers/row.js b/packages/server/src/api/controllers/row.js index 537fc5c850..7540cc1894 100644 --- a/packages/server/src/api/controllers/row.js +++ b/packages/server/src/api/controllers/row.js @@ -272,7 +272,7 @@ exports.search = async function(ctx) { } let searchString - if (ctx.query.raw && ctx.query.raw !== "") { + if (ctx.query && ctx.query.raw && ctx.query.raw !== "") { searchString = queryBuilder.complete(query["RAW"]) } else { // make all strings a starts with operation rather than pure equality diff --git a/packages/server/src/api/controllers/search/utils.js b/packages/server/src/api/controllers/search/utils.js index 1032755759..cb58dc2634 100644 --- a/packages/server/src/api/controllers/search/utils.js +++ b/packages/server/src/api/controllers/search/utils.js @@ -34,7 +34,6 @@ class QueryBuilder { fuzzy: {}, range: {}, equal: {}, - meta: {}, ...base, } this.limit = 50 @@ -93,16 +92,18 @@ class QueryBuilder { if (this.query.string) { build(this.query.string, (key, value) => `${key}:${value}*`) } - if (this.query.number) { - build(this.query.number, (key, value) => - value.length == null - ? `${key}:${value}` - : `${key}:[${value[0]} TO ${value[1]}]` + if (this.query.range) { + build( + this.query.range, + (key, value) => `${key}:[${value[0]} TO ${value[1]}]` ) } if (this.query.fuzzy) { build(this.query.fuzzy, (key, value) => `${key}:${value}~`) } + if (this.query.equal) { + build(this.query.equal, (key, value) => `${key}:${value}`) + } if (rawQuery) { output = output.length === 0 ? rawQuery : `&${rawQuery}` } diff --git a/packages/server/src/api/routes/tests/row.spec.js b/packages/server/src/api/routes/tests/row.spec.js index c79f648c51..6a1c309c39 100644 --- a/packages/server/src/api/routes/tests/row.spec.js +++ b/packages/server/src/api/routes/tests/row.spec.js @@ -2,6 +2,9 @@ const { outputProcessing } = require("../../../utilities/rowProcessor") const setup = require("./utilities") const { basicRow } = setup.structures +// mock the fetch for the search system +jest.mock("node-fetch") + describe("/rows", () => { let request = setup.getRequest() let config = setup.getConfig() @@ -303,25 +306,19 @@ describe("/rows", () => { describe("search", () => { it("should run a search on the table", async () => { - const row = await config.createRow() - // add another row that shouldn't be found - await config.createRow({ - ...basicRow(), - name: "Other Contact", - }) const res = await request .post(`/api/${table._id}/rows/search`) .send({ query: { name: "Test", }, - pagination: { pageSize: 25, page: 0 } + pagination: { pageSize: 25 } }) .set(config.defaultHeaders()) .expect('Content-Type', /json/) .expect(200) - expect(res.body.length).toEqual(1) - expect(res.body[0]._id).toEqual(row._id) + expect(res.body.rows.length).toEqual(1) + expect(res.body.bookmark).toBeDefined() }) })