From 67b11e6226a8843aca8babe930d606e17e9eeefc Mon Sep 17 00:00:00 2001 From: Stanislav Romanov Date: Sat, 22 Oct 2022 15:48:19 +0300 Subject: [PATCH] Add json schema for search params --- .../admin/indexes/item/documents/index.js | 23 +++-- app/models/index.js | 4 +- .../admin/indexes/item/documents/index.js | 86 +++++++------------ .../admin/indexes/item/documents/index.hbs | 2 +- app/utils/json-schema.js | 19 ++++ tests/unit/utils/json-schema-test.js | 10 +++ 6 files changed, 76 insertions(+), 68 deletions(-) create mode 100644 app/utils/json-schema.js create mode 100644 tests/unit/utils/json-schema-test.js diff --git a/app/controllers/admin/indexes/item/documents/index.js b/app/controllers/admin/indexes/item/documents/index.js index 0d9952d..a56f66d 100644 --- a/app/controllers/admin/indexes/item/documents/index.js +++ b/app/controllers/admin/indexes/item/documents/index.js @@ -7,16 +7,17 @@ import { inject as service } from '@ember/service'; export default class AdminIndexesItemDocumentsIndexController extends Controller { @service router; - @tracked q = null; + @tracked q = ''; @tracked limit = 20; - @tracked page = 1; - @tracked dataView = 'table'; - @tracked isAdvancedSearch = false; - @tracked sort; - @tracked hiddenFields = []; + @tracked offset = 0; + @tracked sort = null; @tracked attributesToRetrieve = []; - queryParams = ['q', 'page', 'limit', 'sort']; + @tracked hiddenFields = []; + @tracked dataView = 'table'; + @tracked isAdvancedSearch = false; + + queryParams = ['q', 'limit', 'offset', 'sort', 'attributesToRetrieve']; Magnify = Magnify; @@ -26,6 +27,14 @@ export default class AdminIndexesItemDocumentsIndexController extends Controller json: CodeJson, }; + get search() { + return this.queryParams.reduce((properties, item) => { + properties[item] = this[item]; + + return properties; + }, {}); + } + get attributes() { if (this.attributesToRetrieve.length > 0) { return this.attributesToRetrieve; diff --git a/app/models/index.js b/app/models/index.js index a76d49e..4e37f97 100644 --- a/app/models/index.js +++ b/app/models/index.js @@ -65,9 +65,7 @@ export default class Index { }); } - search(q, options = {}) { - options.q = q; - + search(options) { return query(`${this.path}/search`, { method: 'POST', body: JSON.stringify(options), diff --git a/app/routes/admin/indexes/item/documents/index.js b/app/routes/admin/indexes/item/documents/index.js index 46522bb..1d8da38 100644 --- a/app/routes/admin/indexes/item/documents/index.js +++ b/app/routes/admin/indexes/item/documents/index.js @@ -1,5 +1,6 @@ import Route from '@ember/routing/route'; import { hash } from 'rsvp'; +import { type } from 'meiliadmin/utils/json-schema'; const uri = 'https://docs.meilisearch.com/reference/api/search'; const searchSchema = { @@ -10,31 +11,31 @@ const searchSchema = { $id: uri, type: 'object', properties: { - q: { - type: 'string', + q: type('string'), + offset: type('integer'), + limit: type('integer'), + filter: { + oneOf: [type('array').items('string'), type('null')], }, - // minWordSizeForTypos: { - // type: 'object', - // properties: { - // oneTypo: { type: 'integer' }, - // twoTypos: { type: 'integer' }, - // }, - // additionalProperties: false, - // }, - // disableOnWords: { - // type: 'array', - // items: { - // type: 'string', - // }, - // uniqueItems: true, - // }, - // disableOnAttributes: { - // type: 'array', - // items: { - // type: 'string', - // }, - // uniqueItems: true, - // }, + facets: { + oneOf: [type('array').items('string'), type('null')], + }, + attributesToRetrieve: type('array').items('string'), + attributesToCrop: { + oneOf: [type('array').items('string'), type('null')], + }, + cropLength: type('integer'), + cropMarker: type('string'), + attributesToHighlight: { + oneOf: [type('array').items('string'), type('null')], + }, + highlightPreTag: type('string'), + highlightPostTag: type('string'), + showMatchesPosition: type('boolean'), + sort: { + oneOf: [type('array').items('string'), type('null')], + }, + matchingStrategy: type('string'), }, additionalProperties: false, required: ['q'], @@ -45,56 +46,27 @@ export default class AdminIndexesItemDocumentsIndexRoute extends Route { queryParams = { q: { refreshModel: true }, limit: { refreshModel: true }, - page: { refreshModel: true }, + offset: { refreshModel: true }, sort: { refreshModel: true }, attributesToRetrieve: { refreshModel: true }, }; - async model({ q, page, limit, sort, attributesToRetrieve }) { - const offset = limit * --page; + async model(options) { const index = this.modelFor('admin.indexes.item'); - const options = { offset, limit }; - - if (sort) { - options.sort = [sort]; - } - - if (attributesToRetrieve.length > 0) { - options.attributesToRetrieve = attributesToRetrieve; - } return hash({ index, searchSchema, - search: { q: '' }, - data: index.search(q, options), + data: index.search(options), sortableAttributes: index.getSortableAttributes(), displayedAttributes: index.getSetting('displayed-attributes'), stats: index.getStats(), }); } - // setupController(controller, model) { - // super.setupController(controller, model); - - // if (controller.attributesToRetrieve.length === 0) { - // if ( - // model.displayedAttributes.length > 1 && - // model.displayedAttributes[0] !== '*' - // ) { - // controller.attributes = model.displayedAttributes; - // } else { - // controller.attributes = Object.keys(model.stats.fieldDistribution); - // } - // } - // } - resetController(controller, isExiting) { - // console.log('resetController', isExiting); if (isExiting) { - controller.q = null; - controller.page = 1; - // controller.attributes = []; + // controller.q = ''; } window.scrollTo(0, 0); diff --git a/app/templates/admin/indexes/item/documents/index.hbs b/app/templates/admin/indexes/item/documents/index.hbs index 9ed53ce..a34b3af 100644 --- a/app/templates/admin/indexes/item/documents/index.hbs +++ b/app/templates/admin/indexes/item/documents/index.hbs @@ -22,7 +22,7 @@ {{#if this.isAdvancedSearch}} { + return new Property(type); +}; diff --git a/tests/unit/utils/json-schema-test.js b/tests/unit/utils/json-schema-test.js new file mode 100644 index 0000000..27ccde6 --- /dev/null +++ b/tests/unit/utils/json-schema-test.js @@ -0,0 +1,10 @@ +import jsonSchema from 'meiliadmin/utils/json-schema'; +import { module, test } from 'qunit'; + +module('Unit | Utility | json-schema', function () { + // TODO: Replace this with your real tests. + test('it works', function (assert) { + let result = jsonSchema(); + assert.ok(result); + }); +});