think_elasticsearch
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -33,22 +33,21 @@ 'use strict'; | ||
_esmodel.prototype.init.call(this, config); | ||
this.safe = true; | ||
this.safe = false; | ||
this.index = 'apsystem'; | ||
this.type = 'book'; | ||
this._mapping = { | ||
userid: { | ||
user_name: { | ||
type: 'string' | ||
}, | ||
username: { | ||
nick_name: { | ||
type: 'string' | ||
}, | ||
source: 'string', | ||
info: { | ||
idNo: { type: 'string' }, | ||
real_name: { type: 'string' }, | ||
address: { type: 'string' }, | ||
phonenum: { type: 'string' }, | ||
email: { type: 'string' }, | ||
qq: { type: 'string' }, | ||
wechat_unionid: { type: 'string' }, //微信unionid | ||
sina_weibo: { type: 'string' } }, | ||
class: { | ||
type: 'string' | ||
}, | ||
goods: { | ||
type: 'string' | ||
}, | ||
price: { | ||
type: 'integer' | ||
}, | ||
create_time: { | ||
@@ -55,0 +54,0 @@ type: 'second' |
@@ -12,23 +12,21 @@ /** | ||
super.init(config); | ||
this.safe = true; | ||
this.safe = false; | ||
this.index = 'apsystem'; | ||
this.type = 'book'; | ||
this._mapping = { | ||
userid: { | ||
user_name: { | ||
type: 'string' | ||
}, | ||
username: { | ||
nick_name: { | ||
type: 'string' | ||
}, | ||
source: 'string', | ||
info: { | ||
idNo: {type: 'string'}, | ||
real_name: {type: 'string'}, | ||
address: {type: 'string'}, | ||
phonenum: {type: 'string'}, | ||
email: {type: 'string'}, | ||
qq: {type: 'string'}, | ||
wechat_unionid: {type: 'string'},//微信unionid | ||
sina_weibo: {type: 'string'},//新浪微博 | ||
class: { | ||
type: 'string' | ||
}, | ||
goods: { | ||
type: 'string' | ||
}, | ||
price: { | ||
type: 'integer' | ||
}, | ||
create_time: { | ||
@@ -35,0 +33,0 @@ type: 'second' |
@@ -241,4 +241,5 @@ 'use strict'; | ||
this.queryObj.body = data; | ||
this.queryObj.body = _lib2.default.extend(this.queryObj.body, data); | ||
return this.socket().connect().then(function (conn) { | ||
console.log(_this3.queryObj); | ||
return conn[optype](_this3.queryObj); | ||
@@ -413,2 +414,23 @@ }).then(function (data) { | ||
/** | ||
* 更新数据,根据条件match,filter更新暂时没在ES文档中找到 | ||
* @param data | ||
* @param options | ||
*/ | ||
_class.prototype.update = function update(data, options) { | ||
//判断是否有更新条件,无更新条件使用update,有更新条件使用updateByQuery | ||
this.queryBuilder(options, 'update', data); | ||
//let op; | ||
//if (lib.isEmpty(options.id) && !lib.isEmpty(options.match) || !lib.isEmpty(options.filter)) { | ||
// op = 'updateByQuery'; | ||
// | ||
//} else { | ||
// op = 'update'; | ||
// //data = {doc: data} | ||
//} | ||
return this.execute({ doc: data }, 'update'); | ||
}; | ||
/** | ||
* 查询 | ||
@@ -473,3 +495,3 @@ * @param options | ||
add: { index: true, type: true, id: true }, | ||
update: { index: true, type: true, where: true }, | ||
update: { index: true, type: true, where: true, match: true, filter: true, id: true }, | ||
delete: { index: true, type: true, where: true }, | ||
@@ -517,7 +539,3 @@ count: { index: true, type: true, aggs: true, match: true, limit: true, filter: true, where: true }, | ||
_class.prototype.builderId = function builderId(optionsId, data) { | ||
if (_lib2.default.isNumber(optionsId)) { | ||
this.queryObj.id = optionsId; | ||
} else if (_lib2.default.isString(optionsId) && data[optionsId]) { | ||
this.queryObj.id = data[optionsId]; | ||
} | ||
this.queryObj.id = optionsId; | ||
}; | ||
@@ -524,0 +542,0 @@ |
@@ -146,3 +146,3 @@ 'use strict'; | ||
if (!this.safe) { | ||
if (!(this.safe === false)) { | ||
_context2.next = 17; | ||
@@ -418,3 +418,4 @@ break; | ||
var _ref2 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee3(data, options) { | ||
var parsedOptions; | ||
var parsedOptions, __data; | ||
return _regenerator2.default.wrap(function _callee3$(_context3) { | ||
@@ -429,5 +430,6 @@ while (1) { | ||
parsedOptions = _context3.sent; | ||
__data = _lib2.default.extend({}, data); | ||
//__data = await this._checkData(__data); | ||
this.__data = _lib2.default.extend({}, data); | ||
return _context3.abrupt('return', this.adapter().add(this.__data, parsedOptions)); | ||
return _context3.abrupt('return', this.adapter().add(__data, parsedOptions)); | ||
@@ -488,3 +490,4 @@ case 5: | ||
/** | ||
* 查询一条记录 | ||
* 更新记录,ES更新分为直接以id为条件更新或指定条件更新,不提供无条件更新 | ||
* @param data | ||
* @param options | ||
@@ -494,5 +497,6 @@ */ | ||
_class.prototype.find = function () { | ||
var _ref4 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee5(options) { | ||
var parsedOptions; | ||
_class.prototype.update = function () { | ||
var _ref4 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee5(data, options) { | ||
var parsedOption, __data; | ||
return _regenerator2.default.wrap(function _callee5$(_context5) { | ||
@@ -506,6 +510,20 @@ while (1) { | ||
case 2: | ||
parsedOptions = _context5.sent; | ||
return _context5.abrupt('return', this.adapter().find(parsedOptions)); | ||
parsedOption = _context5.sent; | ||
__data = _lib2.default.extend({}, data); | ||
case 4: | ||
if (!(_lib2.default.isEmpty(__data.id) && _lib2.default.isEmpty(parsedOption.match) && _lib2.default.isEmpty(parsedOption.filter))) { | ||
_context5.next = 6; | ||
break; | ||
} | ||
return _context5.abrupt('return', this.error('ES必须指定条件更新')); | ||
case 6: | ||
if (!_lib2.default.isEmpty(__data.id)) { | ||
parsedOption.id = __data.id; | ||
delete __data.id; | ||
} | ||
return _context5.abrupt('return', this.adapter().update(__data, parsedOption)); | ||
case 8: | ||
case 'end': | ||
@@ -518,17 +536,16 @@ return _context5.stop(); | ||
function find(_x5) { | ||
function update(_x5, _x6) { | ||
return _ref4.apply(this, arguments); | ||
} | ||
return find; | ||
return update; | ||
}(); | ||
/** | ||
* 查询结果集 | ||
* 查询一条记录 | ||
* @param options | ||
* @returns {*} | ||
*/ | ||
_class.prototype.select = function () { | ||
_class.prototype.find = function () { | ||
var _ref5 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee6(options) { | ||
@@ -545,3 +562,3 @@ var parsedOptions; | ||
parsedOptions = _context6.sent; | ||
return _context6.abrupt('return', this.adapter().select(parsedOptions)); | ||
return _context6.abrupt('return', this.adapter().find(parsedOptions)); | ||
@@ -556,16 +573,17 @@ case 4: | ||
function select(_x6) { | ||
function find(_x7) { | ||
return _ref5.apply(this, arguments); | ||
} | ||
return select; | ||
return find; | ||
}(); | ||
/** | ||
* 查询数量 | ||
* 查询结果集 | ||
* @param options | ||
* @returns {*} | ||
*/ | ||
_class.prototype.count = function () { | ||
_class.prototype.select = function () { | ||
var _ref6 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee7(options) { | ||
@@ -582,3 +600,3 @@ var parsedOptions; | ||
parsedOptions = _context7.sent; | ||
return _context7.abrupt('return', this.adapter().count(parsedOptions)); | ||
return _context7.abrupt('return', this.adapter().select(parsedOptions)); | ||
@@ -593,18 +611,18 @@ case 4: | ||
function count(_x7) { | ||
function select(_x8) { | ||
return _ref6.apply(this, arguments); | ||
} | ||
return count; | ||
return select; | ||
}(); | ||
/** | ||
* 求和 | ||
* @param field | ||
* 查询数量 | ||
* @param options | ||
*/ | ||
_class.prototype.sum = function () { | ||
var _ref7 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee8(field, options) { | ||
var parsedOptions, res; | ||
_class.prototype.count = function () { | ||
var _ref7 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee8(options) { | ||
var parsedOptions; | ||
return _regenerator2.default.wrap(function _callee8$(_context8) { | ||
@@ -614,24 +632,10 @@ while (1) { | ||
case 0: | ||
if (!(field === undefined)) { | ||
_context8.next = 2; | ||
break; | ||
} | ||
_context8.next = 2; | ||
return this._parseOptions(options); | ||
return _context8.abrupt('return', this.error('need sum field')); | ||
case 2: | ||
this.aggs({ sum: { sum: { field: field } } }); | ||
_context8.next = 5; | ||
return this._parseOptions(options); | ||
case 5: | ||
parsedOptions = _context8.sent; | ||
_context8.next = 8; | ||
return this.adapter().count(parsedOptions); | ||
return _context8.abrupt('return', this.adapter().count(parsedOptions)); | ||
case 8: | ||
res = _context8.sent; | ||
return _context8.abrupt('return', res.aggregations.sum.value); | ||
case 10: | ||
case 4: | ||
case 'end': | ||
@@ -644,11 +648,11 @@ return _context8.stop(); | ||
function sum(_x8, _x9) { | ||
function count(_x9) { | ||
return _ref7.apply(this, arguments); | ||
} | ||
return sum; | ||
return count; | ||
}(); | ||
/** | ||
* 平均值 | ||
* 求和 | ||
* @param field | ||
@@ -658,3 +662,3 @@ */ | ||
_class.prototype.avg = function () { | ||
_class.prototype.sum = function () { | ||
var _ref8 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee9(field, options) { | ||
@@ -671,6 +675,6 @@ var parsedOptions, res; | ||
return _context9.abrupt('return', this.error('need avg field')); | ||
return _context9.abrupt('return', this.error('need sum field')); | ||
case 2: | ||
this.aggs({ avg: { avg: { field: field } } }); | ||
this.aggs({ sum: { sum: { field: field } } }); | ||
_context9.next = 5; | ||
@@ -686,3 +690,3 @@ return this._parseOptions(options); | ||
res = _context9.sent; | ||
return _context9.abrupt('return', res.aggregations.avg.value); | ||
return _context9.abrupt('return', res.aggregations.sum.value); | ||
@@ -697,11 +701,11 @@ case 10: | ||
function avg(_x10, _x11) { | ||
function sum(_x10, _x11) { | ||
return _ref8.apply(this, arguments); | ||
} | ||
return avg; | ||
return sum; | ||
}(); | ||
/** | ||
* min | ||
* 平均值 | ||
* @param field | ||
@@ -711,3 +715,3 @@ */ | ||
_class.prototype.min = function () { | ||
_class.prototype.avg = function () { | ||
var _ref9 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee10(field, options) { | ||
@@ -724,6 +728,6 @@ var parsedOptions, res; | ||
return _context10.abrupt('return', this.error('need min field')); | ||
return _context10.abrupt('return', this.error('need avg field')); | ||
case 2: | ||
this.aggs({ min: { min: { field: field } } }); | ||
this.aggs({ avg: { avg: { field: field } } }); | ||
_context10.next = 5; | ||
@@ -739,3 +743,3 @@ return this._parseOptions(options); | ||
res = _context10.sent; | ||
return _context10.abrupt('return', res.aggregations.min.value); | ||
return _context10.abrupt('return', res.aggregations.avg.value); | ||
@@ -750,11 +754,11 @@ case 10: | ||
function min(_x12, _x13) { | ||
function avg(_x12, _x13) { | ||
return _ref9.apply(this, arguments); | ||
} | ||
return min; | ||
return avg; | ||
}(); | ||
/** | ||
* 最大值 | ||
* min | ||
* @param field | ||
@@ -764,3 +768,3 @@ */ | ||
_class.prototype.max = function () { | ||
_class.prototype.min = function () { | ||
var _ref10 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee11(field, options) { | ||
@@ -777,6 +781,6 @@ var parsedOptions, res; | ||
return _context11.abrupt('return', this.error('need max field')); | ||
return _context11.abrupt('return', this.error('need min field')); | ||
case 2: | ||
this.aggs({ max: { max: { field: field } } }); | ||
this.aggs({ min: { min: { field: field } } }); | ||
_context11.next = 5; | ||
@@ -792,3 +796,3 @@ return this._parseOptions(options); | ||
res = _context11.sent; | ||
return _context11.abrupt('return', res.aggregations.max.value); | ||
return _context11.abrupt('return', res.aggregations.min.value); | ||
@@ -803,6 +807,55 @@ case 10: | ||
function max(_x14, _x15) { | ||
function min(_x14, _x15) { | ||
return _ref10.apply(this, arguments); | ||
} | ||
return min; | ||
}(); | ||
/** | ||
* 最大值 | ||
* @param field | ||
*/ | ||
_class.prototype.max = function () { | ||
var _ref11 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee12(field, options) { | ||
var parsedOptions, res; | ||
return _regenerator2.default.wrap(function _callee12$(_context12) { | ||
while (1) { | ||
switch (_context12.prev = _context12.next) { | ||
case 0: | ||
if (!(field === undefined)) { | ||
_context12.next = 2; | ||
break; | ||
} | ||
return _context12.abrupt('return', this.error('need max field')); | ||
case 2: | ||
this.aggs({ max: { max: { field: field } } }); | ||
_context12.next = 5; | ||
return this._parseOptions(options); | ||
case 5: | ||
parsedOptions = _context12.sent; | ||
_context12.next = 8; | ||
return this.adapter().count(parsedOptions); | ||
case 8: | ||
res = _context12.sent; | ||
return _context12.abrupt('return', res.aggregations.max.value); | ||
case 10: | ||
case 'end': | ||
return _context12.stop(); | ||
} | ||
} | ||
}, _callee12, this); | ||
})); | ||
function max(_x16, _x17) { | ||
return _ref11.apply(this, arguments); | ||
} | ||
return max; | ||
@@ -832,2 +885,13 @@ }(); | ||
/** | ||
* 数据检测 | ||
* @param data | ||
* @private | ||
*/ | ||
_class.prototype._checkData = function _checkData(data) { | ||
//判断字段默认类型合法性,此处有个问题,对于ES的mapping可关闭或开启mapping中未设置的类型 | ||
}; | ||
return _class; | ||
@@ -834,0 +898,0 @@ }(_base3.default); |
{ | ||
"name": "think_elasticsearch", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "基于elasticsearch.js的elasticsearch工具", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -149,4 +149,5 @@ /** | ||
execute(data, optype = 'create') { | ||
this.queryObj.body = data; | ||
this.queryObj.body = lib.extend(this.queryObj.body, data); | ||
return this.socket().connect().then(conn=> { | ||
console.log(this.queryObj) | ||
return conn[optype](this.queryObj); | ||
@@ -304,2 +305,21 @@ }).then(data=> { | ||
/** | ||
* 更新数据,根据条件match,filter更新暂时没在ES文档中找到 | ||
* @param data | ||
* @param options | ||
*/ | ||
update(data, options) { | ||
//判断是否有更新条件,无更新条件使用update,有更新条件使用updateByQuery | ||
this.queryBuilder(options, 'update', data); | ||
//let op; | ||
//if (lib.isEmpty(options.id) && !lib.isEmpty(options.match) || !lib.isEmpty(options.filter)) { | ||
// op = 'updateByQuery'; | ||
// | ||
//} else { | ||
// op = 'update'; | ||
// //data = {doc: data} | ||
//} | ||
return this.execute({doc: data}, 'update'); | ||
} | ||
/** | ||
* 查询 | ||
@@ -353,3 +373,3 @@ * @param options | ||
add: {index: true, type: true, id: true}, | ||
update: {index: true, type: true, where: true}, | ||
update: {index: true, type: true, where: true, match: true, filter: true, id: true}, | ||
delete: {index: true, type: true, where: true}, | ||
@@ -392,7 +412,3 @@ count: {index: true, type: true, aggs: true, match: true, limit: true, filter: true, where: true}, | ||
builderId(optionsId, data) { | ||
if (lib.isNumber(optionsId)) { | ||
this.queryObj.id = optionsId; | ||
} else if (lib.isString(optionsId) && data[optionsId]) { | ||
this.queryObj.id = data[optionsId]; | ||
} | ||
this.queryObj.id = optionsId; | ||
} | ||
@@ -399,0 +415,0 @@ |
@@ -75,3 +75,3 @@ /** | ||
}; | ||
if (this.safe) {//允许数据迁移 | ||
if (this.safe === false) {//允许数据迁移 | ||
if (await this.adapter().existsType(this.index, this.type)) { | ||
@@ -272,4 +272,5 @@ //判断ES文档中的mappgin和model中的model是否一致,如果不一致,则需要update mapping | ||
let parsedOptions = await this._parseOptions(options); | ||
this.__data = lib.extend({}, data); | ||
return this.adapter().add(this.__data, parsedOptions); | ||
let __data = lib.extend({}, data); | ||
//__data = await this._checkData(__data); | ||
return this.adapter().add(__data, parsedOptions); | ||
} | ||
@@ -289,2 +290,18 @@ | ||
/** | ||
* 更新记录,ES更新分为直接以id为条件更新或指定条件更新,不提供无条件更新 | ||
* @param data | ||
* @param options | ||
*/ | ||
async update(data, options) { | ||
let parsedOption = await this._parseOptions(options); | ||
let __data = lib.extend({}, data); | ||
if (lib.isEmpty(__data.id) && lib.isEmpty(parsedOption.match) && lib.isEmpty(parsedOption.filter)) return this.error('ES必须指定条件更新'); | ||
if (!lib.isEmpty(__data.id)) { | ||
parsedOption.id = __data.id; | ||
delete __data.id; | ||
} | ||
return this.adapter().update(__data, parsedOption); | ||
} | ||
/** | ||
* 查询一条记录 | ||
@@ -383,2 +400,11 @@ * @param options | ||
} | ||
/** | ||
* 数据检测 | ||
* @param data | ||
* @private | ||
*/ | ||
_checkData(data) { | ||
//判断字段默认类型合法性,此处有个问题,对于ES的mapping可关闭或开启mapping中未设置的类型 | ||
} | ||
} |
@@ -20,4 +20,4 @@ /** | ||
id: '1', | ||
username: 'ES', | ||
create_time: Math.ceil(new Date().getTime() / 1000),//除以1000是因为使用的是second,表示的10为时间戳 | ||
user_name: 'ES', | ||
create_time: Math.ceil(new Date().getTime() / 1000),//除以1000是因为使用的是second,表示的10位时间戳 | ||
update_time: Math.ceil(new Date().getTime() / 1000) | ||
@@ -24,0 +24,0 @@ }; |
@@ -24,9 +24,9 @@ /** | ||
}) | ||
.match({ | ||
or: [{goods: 'java'}, {goods: 'meat'}, {price: {gt: 1000}}], | ||
not: [{goods: 'java'}, {goods: 'meat'}, {price: {gt: 1000}}], | ||
}) | ||
//.match({ | ||
// or: [{goods: 'java'}, {goods: 'meat'}, {price: {gt: 1000}}], | ||
// not: [{goods: 'java'}, {goods: 'meat'}, {price: {gt: 1000}}], | ||
//}) | ||
.select().then(res => { | ||
//console.log(res.hits.hits) | ||
console.log(res.hits.hits) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
636803
63
4684