1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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 );
|