Add license information
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / main / resources / elasticsearch / plugins / head / src / app / data / boolQuery.js
1 /**
2  * Copyright 2010-2013 Ben Birch
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this software except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 (function( app ) {
17
18         var data = app.ns("data");
19         var ux = app.ns("ux");
20
21         data.BoolQuery = ux.Observable.extend({
22                 defaults: {
23                         size: 50                // size of pages to return
24                 },
25                 init: function() {
26                         this._super();
27                         this.refuid = 0;
28                         this.refmap = {};
29                         this.search = {
30                                 query: { bool: { must: [], must_not: [], should: [] } },
31                                 from: 0,
32                                 size: this.config.size,
33                                 sort: [],
34                                 aggs: {}
35                         };
36                         this.defaultClause = this.addClause();
37                 },
38                 setSize: function(size) {
39                         this.search.size = parseInt( size, 10 );
40                 },
41                 setPage: function(page) {
42                         this.search.from = this.config.size * (page - 1) + 1;
43                 },
44                 addClause: function(value, field, op, bool) {
45                         bool = bool || "should";
46                         op = op || "match_all";
47                         field = field || "_all";
48                         var clause = this._setClause(value, field, op, bool);
49                         var uqid = "q-" + this.refuid++;
50                         this.refmap[uqid] = { clause: clause, value: value, field: field, op: op, bool: bool };
51                         if(this.search.query.bool.must.length + this.search.query.bool.should.length > 1) {
52                                 this.removeClause(this.defaultClause);
53                         }
54                         this.fire("queryChanged", this, { uqid: uqid, search: this.search} );
55                         return uqid; // returns reference to inner query object to allow fast updating
56                 },
57                 removeClause: function(uqid) {
58                         var ref = this.refmap[uqid],
59                                 bool = this.search.query.bool[ref.bool];
60                         var clauseIdx = bool.indexOf(ref.clause);
61                         // Check that this clause hasn't already been removed
62                         if (clauseIdx >=0) {
63                                 bool.splice(clauseIdx, 1);
64                         }
65                 },
66                 _setClause: function(value, field, op, bool) {
67                         var clause = {}, query = {};
68                         if(op === "match_all") {
69                         } else if(op === "query_string") {
70                                 query["default_field"] = field;
71                                 query["query"] = value;
72                         } else if(op === "missing") {
73                                 op = "constant_score"
74                                 var missing = {}, filter = {};
75                                 missing["field"] = field;
76                                 filter["missing"] = missing
77                                 query["filter"] = filter;
78                         } else {
79                                 query[field.substring(field.indexOf(".")+1)] = value;
80                         }
81                         clause[op] = query;
82                         this.search.query.bool[bool].push(clause);
83                         return clause;
84                 },
85                 getData: function() {
86                         return JSON.stringify(this.search);
87                 }
88         });
89
90 })( this.app );