2  * Copyright 2010-2013 Ben Birch
 
   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
 
   8  *   http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  18         var data = app.ns("data");
 
  19         var ux = app.ns("ux");
 
  21         data.BoolQuery = ux.Observable.extend({
 
  23                         size: 50                // size of pages to return
 
  30                                 query: { bool: { must: [], must_not: [], should: [] } },
 
  32                                 size: this.config.size,
 
  36                         this.defaultClause = this.addClause();
 
  38                 setSize: function(size) {
 
  39                         this.search.size = parseInt( size, 10 );
 
  41                 setPage: function(page) {
 
  42                         this.search.from = this.config.size * (page - 1) + 1;
 
  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);
 
  54                         this.fire("queryChanged", this, { uqid: uqid, search: this.search} );
 
  55                         return uqid; // returns reference to inner query object to allow fast updating
 
  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
 
  63                                 bool.splice(clauseIdx, 1);
 
  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") {
 
  74                                 var missing = {}, filter = {};
 
  75                                 missing["field"] = field;
 
  76                                 filter["missing"] = missing
 
  77                                 query["filter"] = filter;
 
  79                                 query[field.substring(field.indexOf(".")+1)] = value;
 
  82                         this.search.query.bool[bool].push(clause);
 
  86                         return JSON.stringify(this.search);