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");
20 data.QueryDataSourceInterface = data.DataSourceInterface.extend({
22 metadata: null, // (required) instanceof app.data.MetaData, the cluster metadata
23 query: null // (required) instanceof app.data.Query the data source
27 this.config.query.on("results", this._results_handler.bind(this) );
28 this.config.query.on("resultsWithParents", this._load_parents.bind(this) );
30 _results_handler: function(query, res) {
31 this._getSummary(res);
33 var sort = query.search.sort[0] || { "_score": { reverse: false }};
34 var sortField = Object.keys(sort)[0];
35 this.sort = { column: sortField, dir: (sort[sortField].reverse ? "asc" : "desc") };
36 this._getData(res, this.config.metadata);
37 this.fire("data", this);
39 _load_parents: function(query, res) {
40 query.loadParents(res, this.config.metadata);
42 _getData: function(res, metadata) {
43 var metaColumns = ["_index", "_type", "_id", "_score"];
44 var columns = this.columns = [].concat(metaColumns);
46 this.data = res.hits.hits.map(function(hit) {
47 var row = (function(path, spec, row) {
48 for(var prop in spec) {
49 if(acx.isObject(spec[prop])) {
50 arguments.callee(path.concat(prop), spec[prop], row);
51 } else if(acx.isArray(spec[prop])) {
52 if(spec[prop].length) {
53 arguments.callee(path.concat(prop), spec[prop][0], row)
56 var dpath = path.concat(prop).join(".");
57 if(metadata.paths[dpath]) {
58 var field_name = metadata.paths[dpath].field_name;
59 if(! columns.contains(field_name)) {
60 columns.push(field_name);
62 row[field_name] = (spec[prop] === null ? "null" : spec[prop] ).toString();
64 // TODO: field not in metadata index
69 })([ hit._index, hit._type ], hit._source, {});
70 metaColumns.forEach(function(n) { row[n] = hit[n]; });
72 if (typeof hit._parent!= "undefined") {
73 (function(prefix, path, spec, row) {
74 for(var prop in spec) {
75 if(acx.isObject(spec[prop])) {
76 arguments.callee(prefix, path.concat(prop), spec[prop], row);
77 } else if(acx.isArray(spec[prop])) {
78 if(spec[prop].length) {
79 arguments.callee(prefix, path.concat(prop), spec[prop][0], row)
82 var dpath = path.concat(prop).join(".");
83 if(metadata.paths[dpath]) {
84 var field_name = metadata.paths[dpath].field_name;
85 var column_name = prefix+"."+field_name;
86 if(! columns.contains(column_name)) {
87 columns.push(column_name);
89 row[column_name] = (spec[prop] === null ? "null" : spec[prop] ).toString();
91 // TODO: field not in metadata index
95 })(hit._parent._type,[hit._parent._index, hit._parent._type], hit._parent._source, row);