Add json schema for search params

This commit is contained in:
Stanislav Romanov 2022-10-22 15:48:19 +03:00
parent 8fde373b7a
commit 67b11e6226
6 changed files with 76 additions and 68 deletions

View file

@ -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;

View file

@ -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),

View file

@ -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);

View file

@ -22,7 +22,7 @@
{{#if this.isAdvancedSearch}}
<Form::Element @errors={{this.errors}}>
<MonacoEditor
@value={{json-stringify @model.search}}
@value={{json-stringify this.search}}
@language='json'
@uri={{@model.searchSchema.uri}}
@schema={{@model.searchSchema}}

19
app/utils/json-schema.js Normal file
View file

@ -0,0 +1,19 @@
class Property {
constructor(type) {
if (type) {
this.type = type;
}
return this;
}
items(type) {
this.items = new Property(type);
return this;
}
}
export const type = (type) => {
return new Property(type);
};

View file

@ -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);
});
});