UNPKG

2.4 kBJavaScriptView Raw
1
2var 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
13var bun = require('bun');
14
15function Backend( client, index, type ){
16 this.client = client;
17 this._index = index;
18 this._type = type;
19}
20
21Backend.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
29Backend.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
39Backend.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
48Backend.prototype.search = function( query, opts, cb ){
49 this._search( query, opts, extractor.search( cb ) );
50}
51
52// A private search method which returns the raw result from ES
53Backend.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
61Backend.prototype.createPullStream = function(){
62 var stream = createBulkIndexStream( this._index, this._type );
63 return bun([ stream, this.client.stream ]);
64}
65
66// Find the nearest document to the supplied centroid
67Backend.prototype.reverseGeo = function( centroid, opts, cb ){
68 var query = reverseGeoQuery( centroid, merge( { size: 1 }, opts || {} ) );
69 this.search( query, opts, cb );
70}
71
72// Perform a fields only reverse geocode to retrieve the admin heirachy
73Backend.prototype.findAdminHeirachy = function( centroid, opts, cb ){
74 var fields = [ 'admin0', 'admin1', 'admin2' ];
75 var query = reverseGeoQuery( centroid, merge( { size: 1 }, opts || {} ) );
76
77 // only include documents which contain valid admin values
78 query.query.filtered.filter.bool.must.unshift({ exists: { field: fields } });
79
80 // Only return fields related to admin hierarchy in results
81 query.fields = fields;
82
83 this._search( query, opts, extractor.fields( fields, cb ) );
84}
85
86module.exports = Backend;
\No newline at end of file