diff options
Diffstat (limited to 'sdnr/wt/devicemanager/provider/src/main/resources/elasticsearch/plugins/head/src/app/data/boolQuery.js')
-rw-r--r-- | sdnr/wt/devicemanager/provider/src/main/resources/elasticsearch/plugins/head/src/app/data/boolQuery.js | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/sdnr/wt/devicemanager/provider/src/main/resources/elasticsearch/plugins/head/src/app/data/boolQuery.js b/sdnr/wt/devicemanager/provider/src/main/resources/elasticsearch/plugins/head/src/app/data/boolQuery.js new file mode 100644 index 000000000..782d49074 --- /dev/null +++ b/sdnr/wt/devicemanager/provider/src/main/resources/elasticsearch/plugins/head/src/app/data/boolQuery.js @@ -0,0 +1,75 @@ +(function( app ) { + + var data = app.ns("data"); + var ux = app.ns("ux"); + + data.BoolQuery = ux.Observable.extend({ + defaults: { + size: 50 // size of pages to return + }, + init: function() { + this._super(); + this.refuid = 0; + this.refmap = {}; + this.search = { + query: { bool: { must: [], must_not: [], should: [] } }, + from: 0, + size: this.config.size, + sort: [], + aggs: {} + }; + this.defaultClause = this.addClause(); + }, + setSize: function(size) { + this.search.size = parseInt( size, 10 ); + }, + setPage: function(page) { + this.search.from = this.config.size * (page - 1) + 1; + }, + addClause: function(value, field, op, bool) { + bool = bool || "should"; + op = op || "match_all"; + field = field || "_all"; + var clause = this._setClause(value, field, op, bool); + var uqid = "q-" + this.refuid++; + this.refmap[uqid] = { clause: clause, value: value, field: field, op: op, bool: bool }; + if(this.search.query.bool.must.length + this.search.query.bool.should.length > 1) { + this.removeClause(this.defaultClause); + } + this.fire("queryChanged", this, { uqid: uqid, search: this.search} ); + return uqid; // returns reference to inner query object to allow fast updating + }, + removeClause: function(uqid) { + var ref = this.refmap[uqid], + bool = this.search.query.bool[ref.bool]; + var clauseIdx = bool.indexOf(ref.clause); + // Check that this clause hasn't already been removed + if (clauseIdx >=0) { + bool.splice(clauseIdx, 1); + } + }, + _setClause: function(value, field, op, bool) { + var clause = {}, query = {}; + if(op === "match_all") { + } else if(op === "query_string") { + query["default_field"] = field; + query["query"] = value; + } else if(op === "missing") { + op = "constant_score" + var missing = {}, filter = {}; + missing["field"] = field; + filter["missing"] = missing + query["filter"] = filter; + } else { + query[field.substring(field.indexOf(".")+1)] = value; + } + clause[op] = query; + this.search.query.bool[bool].push(clause); + return clause; + }, + getData: function() { + return JSON.stringify(this.search); + } + }); + +})( this.app );
\ No newline at end of file |