3 var data = app.ns("data");
6 data.BoolQuery = ux.Observable.extend({
8 size: 50 // size of pages to return
15 query: { bool: { must: [], must_not: [], should: [] } },
17 size: this.config.size,
21 this.defaultClause = this.addClause();
23 setSize: function(size) {
24 this.search.size = parseInt( size, 10 );
26 setPage: function(page) {
27 this.search.from = this.config.size * (page - 1) + 1;
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);
39 this.fire("queryChanged", this, { uqid: uqid, search: this.search} );
40 return uqid; // returns reference to inner query object to allow fast updating
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
48 bool.splice(clauseIdx, 1);
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") {
59 var missing = {}, filter = {};
60 missing["field"] = field;
61 filter["missing"] = missing
62 query["filter"] = filter;
64 query[field.substring(field.indexOf(".")+1)] = value;
67 this.search.query.bool[bool].push(clause);
71 return JSON.stringify(this.search);