1 |
|
2 | var merge = require('merge'),
|
3 | createBulkIndexStream = require('./bulkIndexStream'),
|
4 | reverseGeoQuery = require('./query/geo_distance'),
|
5 | extractor = {
|
6 | fields: require('./extractor/fields'),
|
7 | mget: require('./extractor/mget'),
|
8 | search: require('./extractor/search'),
|
9 | get: require('./extractor/get'),
|
10 | put: require('./extractor/put')
|
11 | };
|
12 |
|
13 | var bun = require('bun');
|
14 |
|
15 | function Backend( client, index, type ){
|
16 | this.client = client;
|
17 | this._index = index;
|
18 | this._type = type;
|
19 | }
|
20 |
|
21 | Backend.prototype.get = function( key, opts, cb ){
|
22 | this.client.get({
|
23 | index: this._index,
|
24 | type: this._type,
|
25 | id: key
|
26 | }, extractor.get( cb ) );
|
27 | }
|
28 |
|
29 | Backend.prototype.mget = function( ids, opts, cb ){
|
30 | this.client.mget({
|
31 | index: this._index,
|
32 | type: this._type,
|
33 | body: {
|
34 | ids: ids
|
35 | }
|
36 | }, extractor.mget( cb ) );
|
37 | }
|
38 |
|
39 | Backend.prototype.put = function( key, val, opts, cb ){
|
40 | this.client.index({
|
41 | index: this._index,
|
42 | type: this._type,
|
43 | id: key,
|
44 | body: val
|
45 | }, extractor.put( cb ) );
|
46 | }
|
47 |
|
48 | Backend.prototype.search = function( query, opts, cb ){
|
49 | this._search( query, opts, extractor.search( cb ) );
|
50 | }
|
51 |
|
52 |
|
53 | Backend.prototype._search = function( query, opts, cb ){
|
54 | this.client.search({
|
55 | index: this._index,
|
56 | type: this._type,
|
57 | body: query
|
58 | }, cb );
|
59 | }
|
60 |
|
61 | Backend.prototype.createPullStream = function(){
|
62 | var stream = createBulkIndexStream( this._index, this._type );
|
63 | return bun([ stream, this.client.stream ]);
|
64 | }
|
65 |
|
66 |
|
67 | Backend.prototype.reverseGeo = function( centroid, opts, cb ){
|
68 | var query = reverseGeoQuery( centroid, merge( { size: 1 }, opts || {} ) );
|
69 | this.search( query, opts, cb );
|
70 | }
|
71 |
|
72 |
|
73 | Backend.prototype.findAdminHeirachy = function( centroid, opts, cb ){
|
74 | var fields = [ 'admin0', 'admin1', 'admin2' ];
|
75 | var query = reverseGeoQuery( centroid, merge( { size: 1 }, opts || {} ) );
|
76 |
|
77 |
|
78 | query.query.filtered.filter.bool.must.unshift({ exists: { field: fields } });
|
79 |
|
80 |
|
81 | query.fields = fields;
|
82 |
|
83 | this._search( query, opts, extractor.fields( fields, cb ) );
|
84 | }
|
85 |
|
86 | module.exports = Backend; |
\ | No newline at end of file |