Create a gist now

Instantly share code, notes, and snippets.

@htmldrum /ESDSL.js
Last active Aug 29, 2015

What would you like to do?
Map NestedQuerySet to ElasticSearchQuery DSL
(function(){
define([ 'underscore' ], function( _ ){
var MSES = {
// Generic function to encode a QueryHierarchy to an ES JSON query
// Recursive, Erlang style
// qh ->
parse: function( obj ){ // parse/1
var tmp = undefined;
var ES;
if( this._is_obj( obj ) ){
tmp = this._parse( obj, undefined );
if( tmp.length ) {
ES = {
body: {
query: {
bool: {
must: tmp.slice( 0 )
}
}
}
};
} else {
ES = undefined;
}
} else {
throw "Unhandled Exception";
}
return ES;
},
_parse: function( o, path ){ // parse/2
if( this._is_obj( o ) ){
var q = [];
var tmp;
var tmp_path;
_.each( o, function( v, k, l ){
if( this._is_arr( v ) ){
if( v.length ){
q = q.concat( v );
}
} else if( this._is_obj( v ) ) {
if( _.isUndefined( path ) ){
tmp_path = k.toString(); //accolades
} else {
tmp_path = path + '.' + k.toString();
}
tmp = this._parse( v, tmp_path ); // [] // Always returns arrays
if( tmp.length ){
q.push({
nested: {
path: tmp_path.toString(),
query: {
bool: {
must: tmp.slice(0)
}
}
}
});
}
} else {
throw "Unhandled Exception: Can only parse objects and arrays";
}
}.bind( this ));
return q.slice( 0 ); // Return copy of array
} else if( this._is_arr( o ) ) {
return o.slice( 0 ); // Return copy of array
} else {
throw "Unhandled Exception: Can only parse objects and arrays";
}
},
// Arrays *are* ojects, I just want the set diff
_is_arr: function( c ){
return ( _.isObject( c ) && _.isArray( c ) );
},
_is_obj: function( c ){
return ( _.isObject( c ) && !_.isArray( c ) );
},
};
return MSES;
});
}());
get_query_object: function(){
var qh = {
created_at: [],
updated_at: [],
name: [],
flight_date: [],
assignee: {
name: []
},
taster: {
name: []
},
editorial_status: {
title: []
}
};
_.each( this.filters, function( f ){
var fid = f[0].toString();
var v = ANDSL.parse( this.state.filter_text[ fid ] );
if( !_.isUndefined( v.type ) ){
switch( fid ){
case 'name':
switch( v.type ){
case 'term':
qh.name.push({ "match": { "flight.name": v.value.toString() }});
break;
case 'range':
null;
break;
case 'multi_term':
_.each( v.value, function( t ){
qh.name.push({ "match": { "flight.name": t.toString() }});
});
break;
default:
throw "Unhandled Exception";
}
break;
case 'taster_name':
switch( v.type ){
case 'term':
qh.taster.name.push({ "match": { "taster.name": v.value.toString() }});
break;
case 'range':
null;
break;
case 'multi_term':
_.each( v.value, function( t ){
qh.taster.name.push({ "match": { "taster.name": t.toString() }});
});
break;
default:
throw "Unhandled Exception";
}
break;
case 'flight_date':
switch( v.type ){
case 'range':
var f = (new Date( v.value[0] ) );
var t = _.isUndefined( v.value[1] ) ? undefined :(new Date( v.value[1] ) );
qh.flight_date.push({ "range": { "flight_date": {
gte: ( [f.getFullYear(),( f.getMonth() + 1 ),f.getDate()].join('-').toString() ),
lte: ( t ? ( [t.getFullYear(),( t.getMonth() + 1 ),t.getDate()].join('-').toString() ) : "now" )
}}})
break;
case 'term':
var f = (new Date( v.value[0] ) );
var t = (new Date( v.value[1] ) );
qh.flight_date.push({ "range": { "flight_date": {
gte: [f.getFullYear(),( f.getMonth() + 1 ),f.getDate()].join('-').toString(),
lte: [t.getFullYear(),( f.getMonth() + 1 ),t.getDate()].join('-').toString()
}}})
break;
case 'multi_term':
break;
}
break;
case 'created_at':
switch( v.type ){
case 'range':
qh.created_at.push({ "range": { "created_at": {
gte: v.value[0],
lte: ( v.value[1] ? v.value[1] : "now" )
}}})
break;
case 'term':
qh.created_at.push({ "range": { "created_at": {
gte: v.value[0],
lte: ( v.value[1] ? v.value[1] : "now" )
}}})
break;
case 'multi_term':
break;
}
break;
case 'updated_at':
break;
case 'assignee_name':
switch( v.type ){
case 'term':
qh.assignee.name.push({ "match": { "assignee.name": v.value.toString() }});
break;
case 'range':
null;
break;
case 'multi_term':
_.each( v.value, function( t ){
qh.assignee.name.push({ "match": { "assignee.name": t.toString() }});
});
break;
default:
throw "Unhandled Exception";
}
break;
case 'editorial_status_title':
switch( v.type ){
case 'term':
qh.editorial_status.title.push({ "match": { "editorial_status.title": v.value.toString() }});
break;
case 'range':
null;
break;
case 'multi_term':
_.each( v.value, function( t ){
qh.editorial_status.title.push({ "match": { "editorial_status.title": t.toString() }});
});
break;
default:
throw "Unhandled Exception";
}
break;
default:
throw "Unhandled Exception";
break;
}
}
}.bind( this ) );
return MSES.parse( qh );
},
<div class="alert alert-info alert-paging" id="summary">
<% if( view_ready ) { %>
Results: <%= result_count %>. Page : <%= ( page_start ) ? ( parseInt( page_start, 10 ) + 1 ) : 1 %> / <%= Math.floor( result_count / page_size ) ? ( Math.floor( result_count / page_size ) + 1 ) : 1 %><br>
<% } else { %>
<center><img id="headerSpinner" style="margin-left:8px;margin-top:3px;" src="/style/img/spinner.gif"/></center>
<% } %>
</div>
<div class="full_page_results_table" style="margin-bottom:50px; padding-top:0px;">
<table style="border:none;" class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th><input class="filter" id="name" value="<%= filter_text['name'] || ''%>" placeholder="Flight Name"></th>
<th><input class="filter" id="taster_name" value="<%= filter_text['taster_name'] || ''%>" placeholder="Taster Name"></th>
<th><input class="filter" id="flight_date" value="<%= filter_text['flight_date'] || ''%>" placeholder="Flight Date"></th>
<th><input class="filter" id="created_at" value="<%= filter_text['created_at'] || ''%>" placeholder="Last Modified"></th>
<th><input class="filter" id="updated_at" value="<%= filter_text['updated_at'] || ''%>" placeholder="Last Updated"></th>
<th><input class="filter" id="assignee_name" value="<%= filter_text['assignee_name'] || ''%>" placeholder="Assignee Name"></th>
<th><input class="filter" id="editorial_status_title" value="<%= filter_text['editorial_status_title'] || ''%>" placeholder="Status"></th>
</tr>
</thead>
<tbody>
<% if( collection.length ){
collection.each( function( a, ndx ){
%>
<%= _.template( item_template )( _.extend( a.attributes, { Io: Io } ) ) %>
<%
});
} else {
%>
<%= '<tr><td style="border: none;"><a href="javascript:">No Results<a></td></tr>' %>
<%
}
%>
</tbody>
</table>
</div>
Owner

htmldrum commented Nov 20, 2014

Updated for bug in path setting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment