根據索引、類型、id進行查詢:fetch
client.get({ index:'myindex', type:'mytype', id:1 },function(error, response){// ...});
根據某個查詢條件,查詢某個索引的全部數據this
client.search({ index:'myindex', q:'title:test' },function(error, response){// ...});
複雜一點的查詢:debug
client.search({ index:'myindex', body:{ query:{ match:{ title:'test' } }, facets:{ tags:{ terms:{ field:'tags' } } } } },function(error, response){// ...});
新增時,須要指定索引,類型,和id,還有保存的內容:code
client.create({ index:'myindex', type:'mytype', id:'1', body:{ title:'Test 1', tags:['y','z'], published:true, published_at:'2013-01-01', counter:1 } },function(error, response){// ...});
按照索引,類型和id刪除:索引
client.delete({ index:'myindex', type:'mytype', id:'1' },function(error, response){// ...});
修改操做一般使用update方法:rem
client.update({ index:'myindex', type:'mytype', id:'1', body:{ // put the partial document under the `doc` key doc:{ title:'Updated' } } },function(error, response){// ...})
ESClient也支持一次性執行多個操做:get
client.mget({ body:{ docs:[ { _index:'indexA', _type:'typeA', _id:'1' },{ _index:'indexB', _type:'typeB', _id:'1' },{ _index:'indexC', _type:'typeC', _id:'1' }] } },function(error, response){// ...});
也支持下面的風格:string
client.mget({ index:'myindex', type:'mytype', body:{ ids:[1,2,3]} },function(error, response){// ...});
相似的也能夠同時執行多個查詢:it
client.msearch({ body:[ // match all query, on all indices and types {}, { query:{ match_all:{}}}, // query_string query, on index/mytype { _index:'myindex', _type:'mytype' },{ query:{ query_string:{ query:'"Test 1"'} } }] });
經過上面基本API的使用,基本能夠了解js端對ESclient的操做。固然也能夠使用下面的變成風格調用方法:io
es[method](params) 它相似 es.method(params,回調方法)
在kibana中的_doc_send_to_es.js,使用了以下的封裝:
function (method, validateVersion, body, ignore) { // debugger; var doc = this; // straight assignment will causes undefined values var params = _.pick(this._state, ['id', 'type', 'index']); params.body = body; params.ignore = ignore || [409]; if (validateVersion && params.id) { params.version = doc._getVersion(); } // debugger; return es[method](params) .then(function (resp) { // debugger; if (resp.status === 409) throw new errors.VersionConflict(resp); doc._storeVersion(resp._version); doc.id(resp._id); var docFetchProm; if (method !== 'index') { docFetchProm = doc.fetch(); } else { // we already know what the response will be docFetchProm = Promise.resolve({ _id: resp._id, _index: params.index, _source: body, _type: params.type, _version: doc._getVersion(), found: true }); } // notify pending request for this same document that we have updates docFetchProm.then(function (fetchResp) { // use the key to compair sources var key = doc._versionKey(); // clear the queue and filter out the removed items, pushing the // unmatched ones back in. var respondTo = requestQueue.splice(0).filter(function (req) { var isDoc = req.source._getType() === 'doc'; var keyMatches = isDoc && req.source._versionKey() === key; debugger; // put some request back into the queue if (!keyMatches) { requestQueue.push(req); return false; } return true; }); return courierFetch.fakeFetchThese(respondTo, respondTo.map(function () { return _.cloneDeep(fetchResp); })); }); return resp._id; }) .catch(function (err) { // cast the error throw new errors.RequestFailure(err); }); };
所以使用時,又變成了:
xxx.call(this, 'create', false, body, []);
一層一層封裝了不少,可是隻要慢慢屢清除,就知道怎麼使用了。