yandex-tank-ammo-generator
Version:
Cartridge generation for Yandex tank
113 lines (112 loc) • 3.21 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("./index");
class BoolBuilder {
constructor() {
this._query = {
bool: {
must: [],
must_not: [],
should: [],
},
};
}
addBool(type, bool) {
this._query.bool[type].push(bool.build());
return this;
}
/**
* Проверка на наличие добавленых критериев
* @return {boolean}
*/
isNotEmty() {
return this._query.bool.must.length > 0 || this._query.bool.must_not.length > 0 || this._query.bool.should.length > 0;
}
/**
* Указать диапазон поиска по индексу
* @param type
* @param field
* @param value
* @return {this}
*/
addRange(type, field, value) {
this._query.bool[type].push({
[index_1.IFilterTypes.RANGE]: { [field]: value },
});
return this;
}
/**
* Указать критерий поиска по индексу (термин, например articul = 10001)
* @param type
* @param field
* @param value
* @return {this}
*/
addTerm(type, field, value) {
this._query.bool[type].push({
[index_1.IFilterTypes.BY_TERMIN]: { [field]: value },
});
return this;
}
/**
* Текстовый поиск
* @param type
* @param field
* @param value
* @return {this}
*/
addMatch(type, field, value) {
this._query.bool[type].push({
[index_1.IFilterTypes.MATCH]: { [field]: value },
});
return this;
}
addMatchText(type, field, value) {
this._query.bool[type].push({
[index_1.IFilterTypes.MATCH]: {
[field]: {
query: value,
fuzziness: 'AUTO',
operator: 'and',
},
},
});
return this;
}
/**
* Фраза для поиска
* @param type
* @param field
* @param value
* @return {this}
*/
addMatchPhrase(type, field, value) {
this._query.bool[type].push({
[index_1.IFilterTypes.MATCH_PHRASE]: { [field]: value },
});
return this;
}
/**
* Указать массив критериев поиска (аналогия выше) по индексу
* @param type
* @param field
* @param values
* @return {this}
*/
addTermsArray(type, field, values) {
this._query.bool[type].push({
[index_1.IFilterTypes.BY_ARRAY_TERMS]: { [field]: values },
});
return this;
}
build() {
const cond = {
bool: {},
};
this._query.bool.must.length > 0 && (cond.bool['must'] = this._query.bool.must);
this._query.bool.must_not.length > 0 && (cond.bool['must_not'] = this._query.bool.must_not);
this._query.bool.should.length > 0 && (cond.bool['should'] = this._query.bool.should);
return cond;
}
}
exports.BoolBuilder = BoolBuilder;