Skip to content

Instantly share code, notes, and snippets.

@smithcommajoseph
Created July 26, 2013 16:55
Show Gist options
  • Select an option

  • Save smithcommajoseph/6090437 to your computer and use it in GitHub Desktop.

Select an option

Save smithcommajoseph/6090437 to your computer and use it in GitHub Desktop.
//could look more like
function _getGeoJson(req, res) {
GeoJsonModel.findByOptions(req.params.lsad, req.query)
.then(function success (stream) {
if (req.query.asGeoJSON === true) {
utils.streamApiDataAsGeoJson(res, stream);
} else {
utils.streamApiDataAsArray(res, stream);
}
})
.fail(function error (msg) {
utils.sendApiData(res, null, msg);
});
}
//we know we need something like...
{
"query": {
"filtered": {
"query": {
"field": {
"action" : action
}
},
"filter": {
"and" : {
"filters" : [
{
"term" : { "target" : Number(termId) }
},
{
"term" : { "state_region.name" : parentLsad }
}
],
"_cache": true
}
},
"boost": 1.2
}
},
"facets": {
"facet_result": {
"terms": {
"fields": [
"county"
],
"order": "count",
"size": 100
}
}
}
}
// queryBuilder is a given
// a filter fn could look like this.
// "filter": {
// "and" : {
// "filters" : [
// {
// "term" : { "target" : Number(termId) }
// },
// {
// "term" : { "state_region.name" : parentLsad }
// }
// ],
// "_cache": true
// }
// },
function _addFilters(ob, params) {
var obClone = _.clone(ob),
filtersOb = {
filter: {
and: {
filters: []
}
}
},
frag;
if (params.filters) {
params.filters.forEach(function(filter) {
frag = {term: {target: filter}}
filtersOb.filter.and.filters.push(frag)
})
}
obClone.query.filters = filterOb;
return obClone;
}
Clicks.getActionShowsByLocation = function getActionShowsByLocation(action, lat, lng) {
var deferred = Q.defer();
elastic
.index('clicks')
.find({
"query": {
"filtered" : {
"query" : {
"field" : {
"action" : action
}
},
"filter" : {
"geo_distance" : {
"distance" : "50miles",
"pin.location" : [Number(lat), Number(lng)]
}
}
}
},
"facets" : {
"facet_result": {
"terms": {
"fields": ["target"],
"order": "count",
"size": 15
}
}
}
},
function(err, data) { _resolveOrReject(err, data, action, deferred); });
return deferred.promise;
};
//could be
Clicks.getActionShowsByLocation = function getActionShowsByLocation(action, lat, lng) {
var deferred = Q.defer(),
// query = queryBuilderStuff....;
//
//
//
//
//
//
try {
elastic
.index('clicks')
.find(query, function(err, data) { _resolveOrReject(err, data, action, deferred); });
} catch(err) deferred.reject(err);
return deferred.promise;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment