782d490748127f2e2bc94ffd9e533e6e976f1752
[ccsdk/features.git] /
1 (function( app ) {
2
3         var data = app.ns("data");
4         var ux = app.ns("ux");
5
6         data.BoolQuery = ux.Observable.extend({
7                 defaults: {
8                         size: 50                // size of pages to return
9                 },
10                 init: function() {
11                         this._super();
12                         this.refuid = 0;
13                         this.refmap = {};
14                         this.search = {
15                                 query: { bool: { must: [], must_not: [], should: [] } },
16                                 from: 0,
17                                 size: this.config.size,
18                                 sort: [],
19                                 aggs: {}
20                         };
21                         this.defaultClause = this.addClause();
22                 },
23                 setSize: function(size) {
24                         this.search.size = parseInt( size, 10 );
25                 },
26                 setPage: function(page) {
27                         this.search.from = this.config.size * (page - 1) + 1;
28                 },
29                 addClause: function(value, field, op, bool) {
30                         bool = bool || "should";
31                         op = op || "match_all";
32                         field = field || "_all";
33                         var clause = this._setClause(value, field, op, bool);
34                         var uqid = "q-" + this.refuid++;
35                         this.refmap[uqid] = { clause: clause, value: value, field: field, op: op, bool: bool };
36                         if(this.search.query.bool.must.length + this.search.query.bool.should.length > 1) {
37                                 this.removeClause(this.defaultClause);
38                         }
39                         this.fire("queryChanged", this, { uqid: uqid, search: this.search} );
40                         return uqid; // returns reference to inner query object to allow fast updating
41                 },
42                 removeClause: function(uqid) {
43                         var ref = this.refmap[uqid],
44                                 bool = this.search.query.bool[ref.bool];
45                         var clauseIdx = bool.indexOf(ref.clause);
46                         // Check that this clause hasn't already been removed
47                         if (clauseIdx >=0) {
48                                 bool.splice(clauseIdx, 1);
49                         }
50                 },
51                 _setClause: function(value, field, op, bool) {
52                         var clause = {}, query = {};
53                         if(op === "match_all") {
54                         } else if(op === "query_string") {
55                                 query["default_field"] = field;
56                                 query["query"] = value;
57                         } else if(op === "missing") {
58                                 op = "constant_score"
59                                 var missing = {}, filter = {};
60                                 missing["field"] = field;
61                                 filter["missing"] = missing
62                                 query["filter"] = filter;
63                         } else {
64                                 query[field.substring(field.indexOf(".")+1)] = value;
65                         }
66                         clause[op] = query;
67                         this.search.query.bool[bool].push(clause);
68                         return clause;
69                 },
70                 getData: function() {
71                         return JSON.stringify(this.search);
72                 }
73         });
74
75 })( this.app );