Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for scripted fields #90

Merged
merged 1 commit into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 48 additions & 38 deletions factories/docFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,65 @@

function DocFactory() {
var Doc = function(doc, opts) {
var self = this;

var self = this;
angular.copy(doc, self);
self.doc = doc;
self.opts = opts;
};

self.doc = doc;

self.groupedBy = groupedBy;
self.group = group;
self.options = options;
self.version = version;
self.fieldsAttrName = fieldsAttrName;
self.fieldsProperty = fieldsProperty;
Doc.prototype = {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why this change w as made? Is htis a "better" way to do this? Are there other places through the codebase that should be like this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was made so that the methods can be overridden in subclasses. This is a technique called "prototype inheritance", and yes, it should be used everywhere on all "classes" in JavaScript. I believe there are other places in the code base where we already store the methods on the prototype (instead of creating new methods on each instantiated object). However, if you want to "modernize" this, you should instead migrate the code to ES6 classes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for sharing this! I think I'd like to seperate what you've provided into two PR's if I can.. the first is for the support for scripted fields, and the second for resolving this "prototype inheritance" issue..

Doc.prototype.groupedBy = groupedBy;
Doc.prototype.group = group;
Doc.prototype.options = options;
Doc.prototype.version = version;
Doc.prototype.fieldsAttrName = fieldsAttrName;
Doc.prototype.fieldsProperty = fieldsProperty;

function groupedBy () {
if (opts.groupedBy === undefined) {
return null;
} else {
return opts.groupedBy;
}
function groupedBy() {
/*jslint validthis:true*/
var self = this;
if (this.opts.groupedBy === undefined) {
return null;
} else {
return this.opts.groupedBy;
}
}

function options() {
return opts;
}
function options() {
/*jslint validthis:true*/
var self = this;
return this.opts;
}

function group () {
if (opts.group === undefined) {
return null;
} else {
return opts.group;
}
function group() {
/*jslint validthis:true*/
var self = this;
if (this.opts.group === undefined) {
return null;
} else {
return this.opts.group;
}
}

function version () {
if (opts.version === undefined) {
return null;
} else {
return opts.version;
}
function version() {
/*jslint validthis:true*/
var self = this;
if (this.opts.version === undefined) {
return null;
} else {
return this.opts.version;
}
}

function fieldsAttrName() {
return '_source';
}
function fieldsAttrName() {
return '_source';
}

function fieldsProperty() {
return self[self.fieldsAttrName()];
}
};
function fieldsProperty() {
/*jslint validthis:true*/
var self = this;
return self[self.fieldsAttrName()];
}

// Return factory object
return Doc;
Expand Down
17 changes: 12 additions & 5 deletions factories/esDocFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@
Doc.prototype = Object.create(DocFactory.prototype);
Doc.prototype.constructor = Doc; // Reset the constructor

Doc.prototype._url = _url;
Doc.prototype.explain = explain;
Doc.prototype.snippet = snippet;
Doc.prototype.origin = origin;
Doc.prototype.highlight = highlight;
Doc.prototype._url = _url;
Doc.prototype.fieldsProperty = fieldsProperty;
Doc.prototype.explain = explain;
Doc.prototype.snippet = snippet;
Doc.prototype.origin = origin;
Doc.prototype.highlight = highlight;

function _url () {
/*jslint validthis:true*/
Expand All @@ -49,6 +50,12 @@
return esUrlSvc.buildDocUrl(uri, doc);
}

function fieldsProperty() {
/*jslint validthis:true*/
var self = this;
return Object.assign({}, self['_source'], self['fields']);
}

function explain () {
/*jslint validthis:true*/
var self = this;
Expand Down
6 changes: 0 additions & 6 deletions services/esSearcherPreprocessorSvc.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ angular.module('o19s.splainer-search')
}
};

var setFieldsParamName = function() {
self.fieldsParamNames = [ '_source'];
};

function prepare (searcher) {
if (searcher.config === undefined) {
searcher.config = defaultESConfig;
Expand All @@ -114,8 +110,6 @@ angular.module('o19s.splainer-search')
searcher.config = angular.merge({}, defaultESConfig, searcher.config);
}

setFieldsParamName(searcher);

if ( searcher.config.apiMethod === 'post') {
preparePostRequest(searcher);
} else if ( searcher.config.apiMethod === 'get') {
Expand Down