/files//1.1.1/elastic.js
JavaScript | 1574 lines | 680 code | 196 blank | 698 comment | 169 complexity | 95ad09602d46247b28a2ef45984d99cf MD5 | raw file
- /*! elastic.js - v1.1.1 - 2013-05-24
- * https://github.com/fullscale/elastic.js
- * Copyright (c) 2013 FullScale Labs, LLC; Licensed MIT */
- /**
- @namespace
- @name ejs
- @desc All elastic.js modules are organized under the ejs namespace.
- */
- (function () {
- 'use strict';
- var
- // save reference to global object
- // `window` in browser
- // `exports` on server
- root = this,
-
- // save the previous version of ejs
- _ejs = root && root.ejs,
- // from underscore.js, used in utils
- ArrayProto = Array.prototype,
- ObjProto = Object.prototype,
- slice = ArrayProto.slice,
- toString = ObjProto.toString,
- hasOwnProp = ObjProto.hasOwnProperty,
- nativeForEach = ArrayProto.forEach,
- nativeIsArray = Array.isArray,
- nativeIndexOf = ArrayProto.indexOf,
- breaker = {},
- has,
- each,
- extend,
- indexOf,
- genClientParams,
- genParamStr,
- isArray,
- isObject,
- isString,
- isNumber,
- isFunction,
- isEJSObject, // checks if valid ejs object
- isQuery, // checks valid ejs Query object
- isRescore, // checks valid ejs Rescore object
- isFilter, // checks valid ejs Filter object
- isFacet, // checks valid ejs Facet object
- isScriptField, // checks valid ejs ScriptField object
- isGeoPoint, // checks valid ejs GeoPoint object
- isIndexedShape, // checks valid ejs IndexedShape object
- isShape, // checks valid ejs Shape object
- isSort, // checks valid ejs Sort object
- isHighlight, // checks valid ejs Highlight object
- isSuggest, // checks valid ejs Suggest object
- isGenerator, // checks valid ejs Generator object
- isClusterHealth, // checks valid ejs ClusterHealth object
- isClusterState, // checks valid ejs ClusterState object
- isNodeStats, // checks valid ejs NodeStats object
- isNodeInfo, // checks valid ejs NodeInfo object
- isRequest, // checks valid ejs Request object
- isMultiSearchRequest, // checks valid ejs MultiSearchRequest object
-
- // create ejs object
- ejs;
-
- if (typeof exports !== 'undefined') {
- ejs = exports;
- } else {
- ejs = root.ejs = {};
- }
- /* Utility methods, most of which are pulled from underscore.js. */
-
- // Shortcut function for checking if an object has a given property directly
- // on itself (in other words, not on a prototype).
- has = function (obj, key) {
- return hasOwnProp.call(obj, key);
- };
-
- // The cornerstone, an `each` implementation, aka `forEach`.
- // Handles objects with the built-in `forEach`, arrays, and raw objects.
- // Delegates to **ECMAScript 5**'s native `forEach` if available.
- each = function (obj, iterator, context) {
- if (obj == null) {
- return;
- }
- if (nativeForEach && obj.forEach === nativeForEach) {
- obj.forEach(iterator, context);
- } else if (obj.length === +obj.length) {
- for (var i = 0, l = obj.length; i < l; i++) {
- if (iterator.call(context, obj[i], i, obj) === breaker) {
- return;
- }
- }
- } else {
- for (var key in obj) {
- if (has(obj, key)) {
- if (iterator.call(context, obj[key], key, obj) === breaker) {
- return;
- }
- }
- }
- }
- };
-
- // Extend a given object with all the properties in passed-in object(s).
- extend = function (obj) {
- each(slice.call(arguments, 1), function (source) {
- for (var prop in source) {
- obj[prop] = source[prop];
- }
- });
- return obj;
- };
- // Returns the index at which value can be found in the array, or -1 if
- // value is not present in the array.
- indexOf = function (array, item) {
- if (array == null) {
- return -1;
- }
-
- var i = 0, l = array.length;
- if (nativeIndexOf && array.indexOf === nativeIndexOf) {
- return array.indexOf(item);
- }
-
- for (; i < l; i++) {
- if (array[i] === item) {
- return i;
-
- }
- }
-
- return -1;
- };
-
- // Converts the stored params into parameters that will be passed
- // to a client. Certain parameter are skipped, and others require
- // special processing before being sent to the client.
- genClientParams = function (params, excludes) {
- var
- clientParams = {},
- param,
- paramVal;
-
- for (param in params) {
- if (!has(params, param)) {
- continue;
- }
-
- // skip params that don't go in the query string
- if (indexOf(excludes, param) !== -1) {
- continue;
- }
-
- // process all other params
- paramVal = params[param];
- if (isArray(paramVal)) {
- paramVal = paramVal.join();
- }
-
- clientParams[param] = paramVal;
- }
-
- return clientParams;
- };
-
- // converts client params to a string param1=val1¶m2=val1
- genParamStr = function (params, excludes) {
- var
- clientParams = genClientParams(params, excludes),
- parts = [],
- p;
-
- for (p in clientParams) {
- if (!has(clientParams, p)) {
- continue;
- }
-
- parts.push(p + '=' + encodeURIComponent(clientParams[p]));
- }
-
- return parts.join('&');
- };
-
- // Is a given value an array?
- // Delegates to ECMA5's native Array.isArray
- // switched to ===, not sure why underscore used ==
- isArray = nativeIsArray || function (obj) {
- return toString.call(obj) === '[object Array]';
- };
- // Is a given variable an object?
- isObject = function (obj) {
- return obj === Object(obj);
- };
-
- // switched to ===, not sure why underscore used ==
- isString = function (obj) {
- return toString.call(obj) === '[object String]';
- };
-
- // switched to ===, not sure why underscore used ==
- isNumber = function (obj) {
- return toString.call(obj) === '[object Number]';
- };
-
- // switched to ===, not sure why underscore used ==
- if (typeof (/./) !== 'function') {
- isFunction = function (obj) {
- return typeof obj === 'function';
- };
- } else {
- isFunction = function (obj) {
- return toString.call(obj) === '[object Function]';
- };
- }
-
- // Is a given value an ejs object?
- // Yes if object and has "_type", "_self", and "toString" properties
- isEJSObject = function (obj) {
- return (isObject(obj) &&
- has(obj, '_type') &&
- has(obj, '_self') &&
- has(obj, 'toString'));
- };
-
- isQuery = function (obj) {
- return (isEJSObject(obj) && obj._type() === 'query');
- };
- isRescore = function (obj) {
- return (isEJSObject(obj) && obj._type() === 'rescore');
- };
- isFilter = function (obj) {
- return (isEJSObject(obj) && obj._type() === 'filter');
- };
-
- isFacet = function (obj) {
- return (isEJSObject(obj) && obj._type() === 'facet');
- };
-
- isScriptField = function (obj) {
- return (isEJSObject(obj) && obj._type() === 'script field');
- };
-
- isGeoPoint = function (obj) {
- return (isEJSObject(obj) && obj._type() === 'geo point');
- };
-
- isIndexedShape = function (obj) {
- return (isEJSObject(obj) && obj._type() === 'indexed shape');
- };
-
- isShape = function (obj) {
- return (isEJSObject(obj) && obj._type() === 'shape');
- };
-
- isSort = function (obj) {
- return (isEJSObject(obj) && obj._type() === 'sort');
- };
-
- isHighlight = function (obj) {
- return (isEJSObject(obj) && obj._type() === 'highlight');
- };
-
- isSuggest = function (obj) {
- return (isEJSObject(obj) && obj._type() === 'suggest');
- };
-
- isGenerator = function (obj) {
- return (isEJSObject(obj) && obj._type() === 'generator');
- };
-
- isClusterHealth = function (obj) {
- return (isEJSObject(obj) && obj._type() === 'cluster health');
- };
-
- isClusterState = function (obj) {
- return (isEJSObject(obj) && obj._type() === 'cluster state');
- };
-
- isNodeStats = function (obj) {
- return (isEJSObject(obj) && obj._type() === 'node stats');
- };
-
- isNodeInfo = function (obj) {
- return (isEJSObject(obj) && obj._type() === 'node info');
- };
-
- isRequest = function (obj) {
- return (isEJSObject(obj) && obj._type() === 'request');
- };
- isMultiSearchRequest = function (obj) {
- return (isEJSObject(obj) && obj._type() === 'multi search request');
- };
-
- /**
- @class
- <p>The DateHistogram facet works with time-based values by building a histogram across time
- intervals of the <code>value</code> field. Each value is <em>rounded</em> into an interval (or
- placed in a bucket), and statistics are provided per interval/bucket (count and total).</p>
- <p>Facets are similar to SQL <code>GROUP BY</code> statements but perform much
- better. You can also construct several <em>"groups"</em> at once by simply
- specifying multiple facets.</p>
- <div class="alert-message block-message info">
- <p>
- <strong>Tip: </strong>
- For more information on faceted navigation, see
- <a href="http://en.wikipedia.org/wiki/Faceted_classification">this</a>
- Wikipedia article on Faceted Classification.
- </p>
- </div>
- @name ejs.DateHistogramFacet
- @desc
- <p>A facet which returns the N most frequent terms within a collection
- or set of collections.</p>
- @param {String} name The name which be used to refer to this facet. For instance,
- the facet itself might utilize a field named <code>doc_authors</code>. Setting
- <code>name</code> to <code>Authors</code> would allow you to refer to the
- facet by that name, possibly simplifying some of the display logic.
- */
- ejs.DateHistogramFacet = function (name) {
- /**
- The internal facet object.
- @member ejs.DateHistogramFacet
- @property {Object} facet
- */
- var facet = {};
- facet[name] = {
- date_histogram: {}
- };
- return {
- /**
- Sets the field to be used to construct the this facet.
- @member ejs.DateHistogramFacet
- @param {String} fieldName The field name whose data will be used to construct the facet.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- field: function (fieldName) {
- if (fieldName == null) {
- return facet[name].date_histogram.field;
- }
-
- facet[name].date_histogram.field = fieldName;
- return this;
- },
- /**
- Allows you to specify a different key field to be used to group intervals.
- @member ejs.DateHistogramFacet
- @param {String} fieldName The name of the field to be used.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- keyField: function (fieldName) {
- if (fieldName == null) {
- return facet[name].date_histogram.key_field;
- }
-
- facet[name].date_histogram.key_field = fieldName;
- return this;
- },
-
- /**
- Allows you to specify a different value field to aggrerate over.
- @member ejs.DateHistogramFacet
- @param {String} fieldName The name of the field to be used.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- valueField: function (fieldName) {
- if (fieldName == null) {
- return facet[name].date_histogram.value_field;
- }
-
- facet[name].date_histogram.value_field = fieldName;
- return this;
- },
-
- /**
- Sets the bucket interval used to calculate the distribution.
- @member ejs.DateHistogramFacet
- @param {String} timeInterval The bucket interval. Valid values are <code>year, month, week, day, hour,</code> and <code>minute</code>.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- interval: function (timeInterval) {
- if (timeInterval == null) {
- return facet[name].date_histogram.interval;
- }
-
- facet[name].date_histogram.interval = timeInterval;
- return this;
- },
- /**
- <p>By default, time values are stored in UTC format.<p>
- <p>This method allows users to set a time zone value that is then used
- to compute intervals before rounding on the interval value. Equalivent to
- <coe>preZone</code>. Use <code>preZone</code> if possible. The
- value is an offset from UTC.<p>
-
- <p>For example, to use EST you would set the value to <code>-5</code>.</p>
- @member ejs.DateHistogramFacet
- @param {Integer} tz An offset value from UTC.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- timeZone: function (tz) {
- if (tz == null) {
- return facet[name].date_histogram.time_zone;
- }
-
- facet[name].date_histogram.time_zone = tz;
- return this;
- },
- /**
- <p>By default, time values are stored in UTC format.<p>
- <p>This method allows users to set a time zone value that is then used to
- compute intervals before rounding on the interval value. The value is an
- offset from UTC.<p>
-
- <p>For example, to use EST you would set the value to <code>-5</code>.</p>
- @member ejs.DateHistogramFacet
- @param {Integer} tz An offset value from UTC.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- preZone: function (tz) {
- if (tz == null) {
- return facet[name].date_histogram.pre_zone;
- }
-
- facet[name].date_histogram.pre_zone = tz;
- return this;
- },
-
- /**
- <p>Enables large date interval conversions (day and up).</p>
- <p>Set to true to enable and then set the <code>interval</code> to an
- interval greater than a day.</p>
-
- @member ejs.DateHistogramFacet
- @param {Boolean} trueFalse A valid boolean value.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- preZoneAdjustLargeInterval: function (trueFalse) {
- if (trueFalse == null) {
- return facet[name].date_histogram.pre_zone_adjust_large_interval;
- }
-
- facet[name].date_histogram.pre_zone_adjust_large_interval = trueFalse;
- return this;
- },
-
- /**
- <p>By default, time values are stored in UTC format.<p>
- <p>This method allows users to set a time zone value that is then used to compute
- intervals after rounding on the interval value. The value is an offset from UTC.
- The tz offset value is simply added to the resulting bucket's date value.<p>
-
- <p>For example, to use EST you would set the value to <code>-5</code>.</p>
- @member ejs.DateHistogramFacet
- @param {Integer} tz An offset value from UTC.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- postZone: function (tz) {
- if (tz == null) {
- return facet[name].date_histogram.post_zone;
- }
-
- facet[name].date_histogram.post_zone = tz;
- return this;
- },
- /**
- Set's a specific pre-rounding offset. Format is 1d, 1h, etc.
- @member ejs.DateHistogramFacet
- @param {String} offset The offset as a string (1d, 1h, etc)
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- preOffset: function (offset) {
- if (offset == null) {
- return facet[name].date_histogram.pre_offset;
- }
-
- facet[name].date_histogram.pre_offset = offset;
- return this;
- },
-
- /**
- Set's a specific post-rounding offset. Format is 1d, 1h, etc.
- @member ejs.DateHistogramFacet
- @param {String} offset The offset as a string (1d, 1h, etc)
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- postOffset: function (offset) {
- if (offset == null) {
- return facet[name].date_histogram.post_offset;
- }
-
- facet[name].date_histogram.post_offset = offset;
- return this;
- },
-
- /**
- <p>The date histogram works on numeric values (since time is stored
- in milliseconds since the epoch in UTC).<p>
- <p>But, sometimes, systems will store a different resolution (like seconds since UTC)
- in a numeric field. The factor parameter can be used to change the value in the field
- to milliseconds to actual do the relevant rounding, and then be applied again to get to
- the original unit.</p>
- <p>For example, when storing in a numeric field seconds resolution,
- the factor can be set to 1000.<p>
- @member ejs.DateHistogramFacet
- @param {Integer} f The conversion factor.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- factor: function (f) {
- if (f == null) {
- return facet[name].date_histogram.factor;
- }
-
- facet[name].date_histogram.factor = f;
- return this;
- },
-
- /**
- Allows you modify the <code>value</code> field using a script. The modified value
- is then used to compute the statistical data.
- @member ejs.DateHistogramFacet
- @param {String} scriptCode A valid script string to execute.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- valueScript: function (scriptCode) {
- if (scriptCode == null) {
- return facet[name].date_histogram.value_script;
- }
-
- facet[name].date_histogram.value_script = scriptCode;
- return this;
- },
- /**
- <p>Sets the type of ordering that will be performed on the date
- buckets. Valid values are:<p>
-
- <dl>
- <dd><code>time</code> - the default, sort by the buckets start time in milliseconds.</dd>
- <dd><code>count</code> - sort by the number of items in the bucket</dd>
- <dd><code>total</code> - sort by the sum/total of the items in the bucket</dd>
- <dl>
-
- @member ejs.DateHistogramFacet
- @param {String} o The ordering method: time, count, or total.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- order: function (o) {
- if (o == null) {
- return facet[name].date_histogram.order;
- }
-
- o = o.toLowerCase();
- if (o === 'time' || o === 'count' || o === 'total') {
- facet[name].date_histogram.order = o;
- }
-
- return this;
- },
-
- /**
- The script language being used. Currently supported values are
- <code>javascript</code>, <code>groovy</code>, and <code>mvel</code>.
- @member ejs.DateHistogramFacet
- @param {String} language The language of the script.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- lang: function (language) {
- if (language == null) {
- return facet[name].date_histogram.lang;
- }
-
- facet[name].date_histogram.lang = language;
- return this;
- },
- /**
- Sets parameters that will be applied to the script. Overwrites
- any existing params.
- @member ejs.DateHistogramFacet
- @param {Object} p An object where the keys are the parameter name and
- values are the parameter value.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- params: function (p) {
- if (p == null) {
- return facet[name].date_histogram.params;
- }
-
- facet[name].date_histogram.params = p;
- return this;
- },
-
- /**
- <p>Allows you to reduce the documents used for computing facet results.</p>
- @member ejs.DateHistogramFacet
- @param {Object} oFilter A valid <code>Filter</code> object.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- facetFilter: function (oFilter) {
- if (oFilter == null) {
- return facet[name].facet_filter;
- }
-
- if (!isFilter(oFilter)) {
- throw new TypeError('Argument must be a Filter');
- }
-
- facet[name].facet_filter = oFilter._self();
- return this;
- },
- /**
- <p>Computes values across the entire index</p>
- @member ejs.DateHistogramFacet
- @param {Boolean} trueFalse Calculate facet counts globally or not.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- global: function (trueFalse) {
- if (trueFalse == null) {
- return facet[name].global;
- }
-
- facet[name].global = trueFalse;
- return this;
- },
- /**
- <p>Sets the mode the facet will use.<p>
-
- <dl>
- <dd><code>collector</code></dd>
- <dd><code>post</code></dd>
- <dl>
-
- @member ejs.DateHistogramFacet
- @param {String} m The mode: collector or post.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- mode: function (m) {
- if (m == null) {
- return facet[name].mode;
- }
-
- m = m.toLowerCase();
- if (m === 'collector' || m === 'post') {
- facet[name].mode = m;
- }
-
- return this;
- },
-
- /**
- <p>Computes values across the the specified scope</p>
- @deprecated since elasticsearch 0.90
- @member ejs.DateHistogramFacet
- @param {String} scope The scope name to calculate facet counts with.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- scope: function (scope) {
- return this;
- },
-
- /**
- <p>Enables caching of the <code>facetFilter</code></p>
- @member ejs.DateHistogramFacet
- @param {Boolean} trueFalse If the facetFilter should be cached or not
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- cacheFilter: function (trueFalse) {
- if (trueFalse == null) {
- return facet[name].cache_filter;
- }
-
- facet[name].cache_filter = trueFalse;
- return this;
- },
-
- /**
- <p>Sets the path to the nested document if faceting against a
- nested field.</p>
- @member ejs.DateHistogramFacet
- @param {String} path The nested path
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- nested: function (path) {
- if (path == null) {
- return facet[name].nested;
- }
-
- facet[name].nested = path;
- return this;
- },
-
- /**
- <p>Allows you to serialize this object into a JSON encoded string.</p>
- @member ejs.DateHistogramFacet
- @returns {String} returns this object as a serialized JSON string.
- */
- toString: function () {
- return JSON.stringify(facet);
- },
- /**
- The type of ejs object. For internal use only.
-
- @member ejs.DateHistogramFacet
- @returns {String} the type of object
- */
- _type: function () {
- return 'facet';
- },
-
- /**
- <p>Retrieves the internal <code>facet</code> object. This is typically used by
- internal API functions so use with caution.</p>
- @member ejs.DateHistogramFacet
- @returns {String} returns this object's internal <code>facet</code> property.
- */
- _self: function () {
- return facet;
- }
- };
- };
- /**
- @class
- <p>The FilterFacet allows you to specify any valid <code>Filter</code> and
- have the number of matching hits returned as the value.</p>
- <p>Facets are similar to SQL <code>GROUP BY</code> statements but perform much
- better. You can also construct several <em>"groups"</em> at once by simply
- specifying multiple facets.</p>
- <div class="alert-message block-message info">
- <p>
- <strong>Tip: </strong>
- For more information on faceted navigation, see
- <a href="http://en.wikipedia.org/wiki/Faceted_classification">this</a>
- Wikipedia article on Faceted Classification.
- </p>
- </div>
- @name ejs.FilterFacet
- @desc
- <p>A facet that return a count of the hits matching the given filter.</p>
- @param {String} name The name which be used to refer to this facet. For instance,
- the facet itself might utilize a field named <code>doc_authors</code>. Setting
- <code>name</code> to <code>Authors</code> would allow you to refer to the
- facet by that name, possibly simplifying some of the display logic.
- */
- ejs.FilterFacet = function (name) {
- /**
- The internal facet object.
- @member ejs.FilterFacet
- @property {Object} facet
- */
- var facet = {};
- facet[name] = {};
- return {
- /**
- <p>Sets the filter to be used for this facet.</p>
- @member ejs.FilterFacet
- @param {Object} oFilter A valid <code>Query</code> object.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- filter: function (oFilter) {
- if (oFilter == null) {
- return facet[name].filter;
- }
-
- if (!isFilter(oFilter)) {
- throw new TypeError('Argument must be a Filter');
- }
-
- facet[name].filter = oFilter._self();
- return this;
- },
- /**
- <p>Allows you to reduce the documents used for computing facet results.</p>
- @member ejs.FilterFacet
- @param {Object} oFilter A valid <code>Filter</code> object.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- facetFilter: function (oFilter) {
- if (oFilter == null) {
- return facet[name].facet_filter;
- }
-
- if (!isFilter(oFilter)) {
- throw new TypeError('Argument must be a Filter');
- }
-
- facet[name].facet_filter = oFilter._self();
- return this;
- },
- /**
- <p>Computes values across the entire index</p>
- @member ejs.FilterFacet
- @param {Boolean} trueFalse Calculate facet counts globally or not.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- global: function (trueFalse) {
- if (trueFalse == null) {
- return facet[name].global;
- }
-
- facet[name].global = trueFalse;
- return this;
- },
-
- /**
- <p>Sets the mode the facet will use.<p>
-
- <dl>
- <dd><code>collector</code></dd>
- <dd><code>post</code></dd>
- <dl>
-
- @member ejs.FilterFacet
- @param {String} m The mode: collector or post.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- mode: function (m) {
- if (m == null) {
- return facet[name].mode;
- }
-
- m = m.toLowerCase();
- if (m === 'collector' || m === 'post') {
- facet[name].mode = m;
- }
-
- return this;
- },
-
- /**
- <p>Computes values across the the specified scope</p>
- @deprecated since elasticsearch 0.90
- @member ejs.FilterFacet
- @param {String} scope The scope name to calculate facet counts with.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- scope: function (scope) {
- return this;
- },
-
- /**
- <p>Enables caching of the <code>facetFilter</code></p>
- @member ejs.FilterFacet
- @param {Boolean} trueFalse If the facetFilter should be cached or not
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- cacheFilter: function (trueFalse) {
- if (trueFalse == null) {
- return facet[name].cache_filter;
- }
-
- facet[name].cache_filter = trueFalse;
- return this;
- },
-
- /**
- <p>Sets the path to the nested document if faceting against a
- nested field.</p>
- @member ejs.FilterFacet
- @param {String} path The nested path
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- nested: function (path) {
- if (path == null) {
- return facet[name].nested;
- }
-
- facet[name].nested = path;
- return this;
- },
-
- /**
- <p>Allows you to serialize this object into a JSON encoded string.</p>
- @member ejs.FilterFacet
- @returns {String} returns this object as a serialized JSON string.
- */
- toString: function () {
- return JSON.stringify(facet);
- },
- /**
- The type of ejs object. For internal use only.
-
- @member ejs.FilterFacet
- @returns {String} the type of object
- */
- _type: function () {
- return 'facet';
- },
-
- /**
- <p>Retrieves the internal <code>facet</code> object. This is typically used by
- internal API functions so use with caution.</p>
- @member ejs.FilterFacet
- @returns {String} returns this object's internal <code>facet</code> property.
- */
- _self: function () {
- return facet;
- }
- };
- };
- /**
- @class
- <p>The geoDistanceFacet facet provides information over a range of distances from a
- provided point. This includes the number of hits that fall within each range,
- along with aggregate information (like total).</p>
- <p>Facets are similar to SQL <code>GROUP BY</code> statements but perform much
- better. You can also construct several <em>"groups"</em> at once by simply
- specifying multiple facets.</p>
- <div class="alert-message block-message info">
- <p>
- <strong>Tip: </strong>
- For more information on faceted navigation, see
- <a href="http://en.wikipedia.org/wiki/Faceted_classification">this</a>
- Wikipedia article on Faceted Classification.
- </p>
- </div>
- @name ejs.GeoDistanceFacet
- @desc
- <p>A facet which provides information over a range of distances from a provided point.</p>
- @param {String} name The name which be used to refer to this facet. For instance,
- the facet itself might utilize a field named <code>doc_authors</code>. Setting
- <code>name</code> to <code>Authors</code> would allow you to refer to the
- facet by that name, possibly simplifying some of the display logic.
- */
- ejs.GeoDistanceFacet = function (name) {
- /**
- The internal facet object.
- @member ejs.GeoDistanceFacet
- @property {Object} facet
- */
- var facet = {},
- point = ejs.GeoPoint([0, 0]),
- field = 'location';
- facet[name] = {
- geo_distance: {
- location: point._self(),
- ranges: []
- }
- };
- return {
- /**
- Sets the document field containing the geo-coordinate to be used
- to calculate the distance. Defaults to "location".
- @member ejs.GeoDistanceFacet
- @param {String} fieldName The field name whose data will be used to construct the facet.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- field: function (fieldName) {
- var oldValue = facet[name].geo_distance[field];
-
- if (fieldName == null) {
- return field;
- }
- delete facet[name].geo_distance[field];
- field = fieldName;
- facet[name].geo_distance[fieldName] = oldValue;
-
- return this;
- },
- /**
- Sets the point of origin from where distances will be measured.
- @member ejs.GeoDistanceFacet
- @param {GeoPoint} p A valid GeoPoint object
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- point: function (p) {
- if (p == null) {
- return point;
- }
-
- if (!isGeoPoint(p)) {
- throw new TypeError('Argument must be a GeoPoint');
- }
-
- point = p;
- facet[name].geo_distance[field] = p._self();
- return this;
- },
- /**
- Adds a new bounded range.
- @member ejs.GeoDistanceFacet
- @param {Number} from The lower bound of the range
- @param {Number} to The upper bound of the range
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- addRange: function (from, to) {
- if (arguments.length === 0) {
- return facet[name].geo_distance.ranges;
- }
-
- facet[name].geo_distance.ranges.push({
- from: from,
- to: to
- });
-
- return this;
- },
- /**
- Adds a new unbounded lower limit.
- @member ejs.GeoDistanceFacet
- @param {Number} from The lower limit of the unbounded range
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- addUnboundedFrom: function (from) {
- if (from == null) {
- return facet[name].geo_distance.ranges;
- }
-
- facet[name].geo_distance.ranges.push({
- from: from
- });
-
- return this;
- },
- /**
- Adds a new unbounded upper limit.
- @member ejs.GeoDistanceFacet
- @param {Number} to The upper limit of the unbounded range
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- addUnboundedTo: function (to) {
- if (to == null) {
- return facet[name].geo_distance.ranges;
- }
-
- facet[name].geo_distance.ranges.push({
- to: to
- });
-
- return this;
- },
- /**
- Sets the distance unit. Valid values are "mi" for miles or "km"
- for kilometers. Defaults to "km".
- @member ejs.GeoDistanceFacet
- @param {Number} unit the unit of distance measure.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- unit: function (unit) {
- if (unit == null) {
- return facet[name].geo_distance.unit;
- }
-
- unit = unit.toLowerCase();
- if (unit === 'mi' || unit === 'km') {
- facet[name].geo_distance.unit = unit;
- }
-
- return this;
- },
-
- /**
- How to compute the distance. Can either be arc (better precision)
- or plane (faster). Defaults to arc.
- @member ejs.GeoDistanceFacet
- @param {String} type The execution type as a string.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- distanceType: function (type) {
- if (type == null) {
- return facet[name].geo_distance.distance_type;
- }
- type = type.toLowerCase();
- if (type === 'arc' || type === 'plane') {
- facet[name].geo_distance.distance_type = type;
- }
-
- return this;
- },
- /**
- If the lat/long points should be normalized to lie within their
- respective normalized ranges.
-
- Normalized ranges are:
- lon = -180 (exclusive) to 180 (inclusive) range
- lat = -90 to 90 (both inclusive) range
- @member ejs.GeoDistanceFacet
- @param {String} trueFalse True if the coordinates should be normalized. False otherwise.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- normalize: function (trueFalse) {
- if (trueFalse == null) {
- return facet[name].geo_distance.normalize;
- }
- facet[name].geo_distance.normalize = trueFalse;
- return this;
- },
-
- /**
- Allows you to specify a different value field to aggrerate over.
- @member ejs.GeoDistanceFacet
- @param {String} fieldName The name of the field to be used.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- valueField: function (fieldName) {
- if (fieldName == null) {
- return facet[name].geo_distance.value_field;
- }
-
- facet[name].geo_distance.value_field = fieldName;
- return this;
- },
-
- /**
- Allows you modify the <code>value</code> field using a script. The modified value
- is then used to compute the statistical data.
- @member ejs.GeoDistanceFacet
- @param {String} scriptCode A valid script string to execute.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- valueScript: function (scriptCode) {
- if (scriptCode == null) {
- return facet[name].geo_distance.value_script;
- }
-
- facet[name].geo_distance.value_script = scriptCode;
- return this;
- },
-
- /**
- The script language being used. Currently supported values are
- <code>javascript</code>, <code>groovy</code>, and <code>mvel</code>.
- @member ejs.GeoDistanceFacet
- @param {String} language The language of the script.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- lang: function (language) {
- if (language == null) {
- return facet[name].geo_distance.lang;
- }
-
- facet[name].geo_distance.lang = language;
- return this;
- },
-
- /**
- Sets parameters that will be applied to the script. Overwrites
- any existing params.
- @member ejs.GeoDistanceFacet
- @param {Object} p An object where the keys are the parameter name and
- values are the parameter value.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- params: function (p) {
- if (p == null) {
- return facet[name].geo_distance.params;
- }
-
- facet[name].geo_distance.params = p;
- return this;
- },
-
- /**
- <p>Allows you to reduce the documents used for computing facet results.</p>
- @member ejs.GeoDistanceFacet
- @param {Object} oFilter A valid <code>Filter</code> object.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- facetFilter: function (oFilter) {
- if (oFilter == null) {
- return facet[name].facet_filter;
- }
-
- if (!isFilter(oFilter)) {
- throw new TypeError('Argument must be a Filter');
- }
-
- facet[name].facet_filter = oFilter._self();
- return this;
- },
- /**
- <p>Computes values across the entire index</p>
- @member ejs.GeoDistanceFacet
- @param {Boolean} trueFalse Calculate facet counts globally or not.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- global: function (trueFalse) {
- if (trueFalse == null) {
- return facet[name].global;
- }
-
- facet[name].global = trueFalse;
- return this;
- },
-
- /**
- <p>Sets the mode the facet will use.<p>
-
- <dl>
- <dd><code>collector</code></dd>
- <dd><code>post</code></dd>
- <dl>
-
- @member ejs.GeoDistanceFacet
- @param {String} m The mode: collector or post.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- mode: function (m) {
- if (m == null) {
- return facet[name].mode;
- }
-
- m = m.toLowerCase();
- if (m === 'collector' || m === 'post') {
- facet[name].mode = m;
- }
-
- return this;
- },
-
- /**
- <p>Computes values across the the specified scope</p>
- @deprecated since elasticsearch 0.90
- @member ejs.GeoDistanceFacet
- @param {String} scope The scope name to calculate facet counts with.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- scope: function (scope) {
- return this;
- },
-
- /**
- <p>Enables caching of the <code>facetFilter</code></p>
- @member ejs.GeoDistanceFacet
- @param {Boolean} trueFalse If the facetFilter should be cached or not
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- cacheFilter: function (trueFalse) {
- if (trueFalse == null) {
- return facet[name].cache_filter;
- }
-
- facet[name].cache_filter = trueFalse;
- return this;
- },
-
- /**
- <p>Sets the path to the nested document if faceting against a
- nested field.</p>
- @member ejs.GeoDistanceFacet
- @param {String} path The nested path
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- nested: function (path) {
- if (path == null) {
- return facet[name].nested;
- }
-
- facet[name].nested = path;
- return this;
- },
-
- /**
- <p>Allows you to serialize this object into a JSON encoded string.</p>
- @member ejs.GeoDistanceFacet
- @returns {String} returns this object as a serialized JSON string.
- */
- toString: function () {
- return JSON.stringify(facet);
- },
- /**
- The type of ejs object. For internal use only.
-
- @member ejs.GeoDistanceFacet
- @returns {String} the type of object
- */
- _type: function () {
- return 'facet';
- },
-
- /**
- <p>Retrieves the internal <code>facet</code> object. This is typically used by
- internal API functions so use with caution.</p>
- @member ejs.GeoDistanceFacet
- @returns {String} returns this object's internal <code>facet</code> property.
- */
- _self: function () {
- return facet;
- }
- };
- };
- /**
- @class
- <p>The histogram facet works with numeric data by building a histogram across intervals
- of the field values. Each value is <em>rounded</em> into an interval (or placed in a
- bucket), and statistics are provided per interval/bucket (count and total).</p>
- <p>Facets are similar to SQL <code>GROUP BY</code> statements but perform much
- better. You can also construct several <em>"groups"</em> at once by simply
- specifying multiple facets.</p>
- <div class="alert-message block-message info">
- <p>
- <strong>Tip: </strong>
- For more information on faceted navigation, see
- <a href="http://en.wikipedia.org/wiki/Faceted_classification">this</a>
- Wikipedia article on Faceted Classification.
- </p>
- </div>
- @name ejs.HistogramFacet
- @desc
- <p>A facet which returns the N most frequent terms within a collection
- or set of collections.</p>
- @param {String} name The name which be used to refer to this facet. For instance,
- the facet itself might utilize a field named <code>doc_authors</code>. Setting
- <code>name</code> to <code>Authors</code> would allow you to refer to the
- facet by that name, possibly simplifying some of the display logic.
- */
- ejs.HistogramFacet = function (name) {
- /**
- The internal facet object.
- @member ejs.HistogramFacet
- @property {Object} facet
- */
- var facet = {};
- facet[name] = {
- histogram: {}
- };
- return {
- /**
- Sets the field to be used to construct the this facet.
- @member ejs.HistogramFacet
- @param {String} fieldName The field name whose data will be used to construct the facet.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- field: function (fieldName) {
- if (fieldName == null) {
- return facet[name].histogram.field;
- }
-
- facet[name].histogram.field = fieldName;
- return this;
- },
- /**
- Sets the bucket interval used to calculate the distribution.
- @member ejs.HistogramFacet
- @param {Number} numericInterval The bucket interval in which to group values.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- interval: function (numericInterval) {
- if (numericInterval == null) {
- return facet[name].histogram.interval;
- }
-
- facet[name].histogram.interval = numericInterval;
- return this;
- },
- /**
- Sets the bucket interval used to calculate the distribution based
- on a time value such as "1d", "1w", etc.
- @member ejs.HistogramFacet
- @param {Number} timeInterval The bucket interval in which to group values.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- timeInterval: function (timeInterval) {
- if (timeInterval == null) {
- return facet[name].histogram.time_interval;
- }
-
- facet[name].histogram.time_interval = timeInterval;
- return this;
- },
- /**
- Sets the "from", "start", or lower bounds bucket. For example if
- you have a value of 1023, an interval of 100, and a from value of
- 1500, it will be placed into the 1500 bucket vs. the normal bucket
- of 1000.
- @member ejs.HistogramFacet
- @param {Number} from the lower bounds bucket value.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- from: function (from) {
- if (from == null) {
- return facet[name].histogram.from;
- }
-
- facet[name].histogram.from = from;
- return this;
- },
- /**
- Sets the "to", "end", or upper bounds bucket. For example if
- you have a value of 1023, an interval of 100, and a to value of
- 900, it will be placed into the 900 bucket vs. the normal bucket
- of 1000.
- @member ejs.HistogramFacet
- @param {Number} to the upper bounds bucket value.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- to: function (to) {
- if (to == null) {
- return facet[name].histogram.to;
- }
-
- facet[name].histogram.to = to;
- return this;
- },
-
- /**
- Allows you to specify a different value field to aggrerate over.
- @member ejs.HistogramFacet
- @param {String} fieldName The name of the field to be used.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- valueField: function (fieldName) {
- if (fieldName == null) {
- return facet[name].histogram.value_field;
- }
-
- facet[name].histogram.value_field = fieldName;
- return this;
- },
- /**
- Allows you to specify a different key field to be used to group intervals.
- @member ejs.HistogramFacet
- @param {String} fieldName The name of the field to be used.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- keyField: function (fieldName) {
- if (fieldName == null) {
- return facet[name].histogram.key_field;
- }
-
- facet[name].histogram.key_field = fieldName;
- return this;
- },
- /**
- Allows you modify the <code>value</code> field using a script. The modified value
- is then used to compute the statistical data.
- @member ejs.HistogramFacet
- @param {String} scriptCode A valid script string to execute.
- @returns {Object} returns <code>this</code> so that calls can be chained.
- */
- valueScript: function (scriptCode) {
- if (scriptCode == null) {
- return facet[name].histogram.value_script;
- }
-
- facet[name].histogram.value_script = scriptCode;
-