PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/files//1.1.1/elastic.js

https://gitlab.com/Mirros/jsdelivr
JavaScript | 1574 lines | 680 code | 196 blank | 698 comment | 169 complexity | 95ad09602d46247b28a2ef45984d99cf MD5 | raw file
  1. /*! elastic.js - v1.1.1 - 2013-05-24
  2. * https://github.com/fullscale/elastic.js
  3. * Copyright (c) 2013 FullScale Labs, LLC; Licensed MIT */
  4. /**
  5. @namespace
  6. @name ejs
  7. @desc All elastic.js modules are organized under the ejs namespace.
  8. */
  9. (function () {
  10. 'use strict';
  11. var
  12. // save reference to global object
  13. // `window` in browser
  14. // `exports` on server
  15. root = this,
  16. // save the previous version of ejs
  17. _ejs = root && root.ejs,
  18. // from underscore.js, used in utils
  19. ArrayProto = Array.prototype,
  20. ObjProto = Object.prototype,
  21. slice = ArrayProto.slice,
  22. toString = ObjProto.toString,
  23. hasOwnProp = ObjProto.hasOwnProperty,
  24. nativeForEach = ArrayProto.forEach,
  25. nativeIsArray = Array.isArray,
  26. nativeIndexOf = ArrayProto.indexOf,
  27. breaker = {},
  28. has,
  29. each,
  30. extend,
  31. indexOf,
  32. genClientParams,
  33. genParamStr,
  34. isArray,
  35. isObject,
  36. isString,
  37. isNumber,
  38. isFunction,
  39. isEJSObject, // checks if valid ejs object
  40. isQuery, // checks valid ejs Query object
  41. isRescore, // checks valid ejs Rescore object
  42. isFilter, // checks valid ejs Filter object
  43. isFacet, // checks valid ejs Facet object
  44. isScriptField, // checks valid ejs ScriptField object
  45. isGeoPoint, // checks valid ejs GeoPoint object
  46. isIndexedShape, // checks valid ejs IndexedShape object
  47. isShape, // checks valid ejs Shape object
  48. isSort, // checks valid ejs Sort object
  49. isHighlight, // checks valid ejs Highlight object
  50. isSuggest, // checks valid ejs Suggest object
  51. isGenerator, // checks valid ejs Generator object
  52. isClusterHealth, // checks valid ejs ClusterHealth object
  53. isClusterState, // checks valid ejs ClusterState object
  54. isNodeStats, // checks valid ejs NodeStats object
  55. isNodeInfo, // checks valid ejs NodeInfo object
  56. isRequest, // checks valid ejs Request object
  57. isMultiSearchRequest, // checks valid ejs MultiSearchRequest object
  58. // create ejs object
  59. ejs;
  60. if (typeof exports !== 'undefined') {
  61. ejs = exports;
  62. } else {
  63. ejs = root.ejs = {};
  64. }
  65. /* Utility methods, most of which are pulled from underscore.js. */
  66. // Shortcut function for checking if an object has a given property directly
  67. // on itself (in other words, not on a prototype).
  68. has = function (obj, key) {
  69. return hasOwnProp.call(obj, key);
  70. };
  71. // The cornerstone, an `each` implementation, aka `forEach`.
  72. // Handles objects with the built-in `forEach`, arrays, and raw objects.
  73. // Delegates to **ECMAScript 5**'s native `forEach` if available.
  74. each = function (obj, iterator, context) {
  75. if (obj == null) {
  76. return;
  77. }
  78. if (nativeForEach && obj.forEach === nativeForEach) {
  79. obj.forEach(iterator, context);
  80. } else if (obj.length === +obj.length) {
  81. for (var i = 0, l = obj.length; i < l; i++) {
  82. if (iterator.call(context, obj[i], i, obj) === breaker) {
  83. return;
  84. }
  85. }
  86. } else {
  87. for (var key in obj) {
  88. if (has(obj, key)) {
  89. if (iterator.call(context, obj[key], key, obj) === breaker) {
  90. return;
  91. }
  92. }
  93. }
  94. }
  95. };
  96. // Extend a given object with all the properties in passed-in object(s).
  97. extend = function (obj) {
  98. each(slice.call(arguments, 1), function (source) {
  99. for (var prop in source) {
  100. obj[prop] = source[prop];
  101. }
  102. });
  103. return obj;
  104. };
  105. // Returns the index at which value can be found in the array, or -1 if
  106. // value is not present in the array.
  107. indexOf = function (array, item) {
  108. if (array == null) {
  109. return -1;
  110. }
  111. var i = 0, l = array.length;
  112. if (nativeIndexOf && array.indexOf === nativeIndexOf) {
  113. return array.indexOf(item);
  114. }
  115. for (; i < l; i++) {
  116. if (array[i] === item) {
  117. return i;
  118. }
  119. }
  120. return -1;
  121. };
  122. // Converts the stored params into parameters that will be passed
  123. // to a client. Certain parameter are skipped, and others require
  124. // special processing before being sent to the client.
  125. genClientParams = function (params, excludes) {
  126. var
  127. clientParams = {},
  128. param,
  129. paramVal;
  130. for (param in params) {
  131. if (!has(params, param)) {
  132. continue;
  133. }
  134. // skip params that don't go in the query string
  135. if (indexOf(excludes, param) !== -1) {
  136. continue;
  137. }
  138. // process all other params
  139. paramVal = params[param];
  140. if (isArray(paramVal)) {
  141. paramVal = paramVal.join();
  142. }
  143. clientParams[param] = paramVal;
  144. }
  145. return clientParams;
  146. };
  147. // converts client params to a string param1=val1&param2=val1
  148. genParamStr = function (params, excludes) {
  149. var
  150. clientParams = genClientParams(params, excludes),
  151. parts = [],
  152. p;
  153. for (p in clientParams) {
  154. if (!has(clientParams, p)) {
  155. continue;
  156. }
  157. parts.push(p + '=' + encodeURIComponent(clientParams[p]));
  158. }
  159. return parts.join('&');
  160. };
  161. // Is a given value an array?
  162. // Delegates to ECMA5's native Array.isArray
  163. // switched to ===, not sure why underscore used ==
  164. isArray = nativeIsArray || function (obj) {
  165. return toString.call(obj) === '[object Array]';
  166. };
  167. // Is a given variable an object?
  168. isObject = function (obj) {
  169. return obj === Object(obj);
  170. };
  171. // switched to ===, not sure why underscore used ==
  172. isString = function (obj) {
  173. return toString.call(obj) === '[object String]';
  174. };
  175. // switched to ===, not sure why underscore used ==
  176. isNumber = function (obj) {
  177. return toString.call(obj) === '[object Number]';
  178. };
  179. // switched to ===, not sure why underscore used ==
  180. if (typeof (/./) !== 'function') {
  181. isFunction = function (obj) {
  182. return typeof obj === 'function';
  183. };
  184. } else {
  185. isFunction = function (obj) {
  186. return toString.call(obj) === '[object Function]';
  187. };
  188. }
  189. // Is a given value an ejs object?
  190. // Yes if object and has "_type", "_self", and "toString" properties
  191. isEJSObject = function (obj) {
  192. return (isObject(obj) &&
  193. has(obj, '_type') &&
  194. has(obj, '_self') &&
  195. has(obj, 'toString'));
  196. };
  197. isQuery = function (obj) {
  198. return (isEJSObject(obj) && obj._type() === 'query');
  199. };
  200. isRescore = function (obj) {
  201. return (isEJSObject(obj) && obj._type() === 'rescore');
  202. };
  203. isFilter = function (obj) {
  204. return (isEJSObject(obj) && obj._type() === 'filter');
  205. };
  206. isFacet = function (obj) {
  207. return (isEJSObject(obj) && obj._type() === 'facet');
  208. };
  209. isScriptField = function (obj) {
  210. return (isEJSObject(obj) && obj._type() === 'script field');
  211. };
  212. isGeoPoint = function (obj) {
  213. return (isEJSObject(obj) && obj._type() === 'geo point');
  214. };
  215. isIndexedShape = function (obj) {
  216. return (isEJSObject(obj) && obj._type() === 'indexed shape');
  217. };
  218. isShape = function (obj) {
  219. return (isEJSObject(obj) && obj._type() === 'shape');
  220. };
  221. isSort = function (obj) {
  222. return (isEJSObject(obj) && obj._type() === 'sort');
  223. };
  224. isHighlight = function (obj) {
  225. return (isEJSObject(obj) && obj._type() === 'highlight');
  226. };
  227. isSuggest = function (obj) {
  228. return (isEJSObject(obj) && obj._type() === 'suggest');
  229. };
  230. isGenerator = function (obj) {
  231. return (isEJSObject(obj) && obj._type() === 'generator');
  232. };
  233. isClusterHealth = function (obj) {
  234. return (isEJSObject(obj) && obj._type() === 'cluster health');
  235. };
  236. isClusterState = function (obj) {
  237. return (isEJSObject(obj) && obj._type() === 'cluster state');
  238. };
  239. isNodeStats = function (obj) {
  240. return (isEJSObject(obj) && obj._type() === 'node stats');
  241. };
  242. isNodeInfo = function (obj) {
  243. return (isEJSObject(obj) && obj._type() === 'node info');
  244. };
  245. isRequest = function (obj) {
  246. return (isEJSObject(obj) && obj._type() === 'request');
  247. };
  248. isMultiSearchRequest = function (obj) {
  249. return (isEJSObject(obj) && obj._type() === 'multi search request');
  250. };
  251. /**
  252. @class
  253. <p>The DateHistogram facet works with time-based values by building a histogram across time
  254. intervals of the <code>value</code> field. Each value is <em>rounded</em> into an interval (or
  255. placed in a bucket), and statistics are provided per interval/bucket (count and total).</p>
  256. <p>Facets are similar to SQL <code>GROUP BY</code> statements but perform much
  257. better. You can also construct several <em>"groups"</em> at once by simply
  258. specifying multiple facets.</p>
  259. <div class="alert-message block-message info">
  260. <p>
  261. <strong>Tip: </strong>
  262. For more information on faceted navigation, see
  263. <a href="http://en.wikipedia.org/wiki/Faceted_classification">this</a>
  264. Wikipedia article on Faceted Classification.
  265. </p>
  266. </div>
  267. @name ejs.DateHistogramFacet
  268. @desc
  269. <p>A facet which returns the N most frequent terms within a collection
  270. or set of collections.</p>
  271. @param {String} name The name which be used to refer to this facet. For instance,
  272. the facet itself might utilize a field named <code>doc_authors</code>. Setting
  273. <code>name</code> to <code>Authors</code> would allow you to refer to the
  274. facet by that name, possibly simplifying some of the display logic.
  275. */
  276. ejs.DateHistogramFacet = function (name) {
  277. /**
  278. The internal facet object.
  279. @member ejs.DateHistogramFacet
  280. @property {Object} facet
  281. */
  282. var facet = {};
  283. facet[name] = {
  284. date_histogram: {}
  285. };
  286. return {
  287. /**
  288. Sets the field to be used to construct the this facet.
  289. @member ejs.DateHistogramFacet
  290. @param {String} fieldName The field name whose data will be used to construct the facet.
  291. @returns {Object} returns <code>this</code> so that calls can be chained.
  292. */
  293. field: function (fieldName) {
  294. if (fieldName == null) {
  295. return facet[name].date_histogram.field;
  296. }
  297. facet[name].date_histogram.field = fieldName;
  298. return this;
  299. },
  300. /**
  301. Allows you to specify a different key field to be used to group intervals.
  302. @member ejs.DateHistogramFacet
  303. @param {String} fieldName The name of the field to be used.
  304. @returns {Object} returns <code>this</code> so that calls can be chained.
  305. */
  306. keyField: function (fieldName) {
  307. if (fieldName == null) {
  308. return facet[name].date_histogram.key_field;
  309. }
  310. facet[name].date_histogram.key_field = fieldName;
  311. return this;
  312. },
  313. /**
  314. Allows you to specify a different value field to aggrerate over.
  315. @member ejs.DateHistogramFacet
  316. @param {String} fieldName The name of the field to be used.
  317. @returns {Object} returns <code>this</code> so that calls can be chained.
  318. */
  319. valueField: function (fieldName) {
  320. if (fieldName == null) {
  321. return facet[name].date_histogram.value_field;
  322. }
  323. facet[name].date_histogram.value_field = fieldName;
  324. return this;
  325. },
  326. /**
  327. Sets the bucket interval used to calculate the distribution.
  328. @member ejs.DateHistogramFacet
  329. @param {String} timeInterval The bucket interval. Valid values are <code>year, month, week, day, hour,</code> and <code>minute</code>.
  330. @returns {Object} returns <code>this</code> so that calls can be chained.
  331. */
  332. interval: function (timeInterval) {
  333. if (timeInterval == null) {
  334. return facet[name].date_histogram.interval;
  335. }
  336. facet[name].date_histogram.interval = timeInterval;
  337. return this;
  338. },
  339. /**
  340. <p>By default, time values are stored in UTC format.<p>
  341. <p>This method allows users to set a time zone value that is then used
  342. to compute intervals before rounding on the interval value. Equalivent to
  343. <coe>preZone</code>. Use <code>preZone</code> if possible. The
  344. value is an offset from UTC.<p>
  345. <p>For example, to use EST you would set the value to <code>-5</code>.</p>
  346. @member ejs.DateHistogramFacet
  347. @param {Integer} tz An offset value from UTC.
  348. @returns {Object} returns <code>this</code> so that calls can be chained.
  349. */
  350. timeZone: function (tz) {
  351. if (tz == null) {
  352. return facet[name].date_histogram.time_zone;
  353. }
  354. facet[name].date_histogram.time_zone = tz;
  355. return this;
  356. },
  357. /**
  358. <p>By default, time values are stored in UTC format.<p>
  359. <p>This method allows users to set a time zone value that is then used to
  360. compute intervals before rounding on the interval value. The value is an
  361. offset from UTC.<p>
  362. <p>For example, to use EST you would set the value to <code>-5</code>.</p>
  363. @member ejs.DateHistogramFacet
  364. @param {Integer} tz An offset value from UTC.
  365. @returns {Object} returns <code>this</code> so that calls can be chained.
  366. */
  367. preZone: function (tz) {
  368. if (tz == null) {
  369. return facet[name].date_histogram.pre_zone;
  370. }
  371. facet[name].date_histogram.pre_zone = tz;
  372. return this;
  373. },
  374. /**
  375. <p>Enables large date interval conversions (day and up).</p>
  376. <p>Set to true to enable and then set the <code>interval</code> to an
  377. interval greater than a day.</p>
  378. @member ejs.DateHistogramFacet
  379. @param {Boolean} trueFalse A valid boolean value.
  380. @returns {Object} returns <code>this</code> so that calls can be chained.
  381. */
  382. preZoneAdjustLargeInterval: function (trueFalse) {
  383. if (trueFalse == null) {
  384. return facet[name].date_histogram.pre_zone_adjust_large_interval;
  385. }
  386. facet[name].date_histogram.pre_zone_adjust_large_interval = trueFalse;
  387. return this;
  388. },
  389. /**
  390. <p>By default, time values are stored in UTC format.<p>
  391. <p>This method allows users to set a time zone value that is then used to compute
  392. intervals after rounding on the interval value. The value is an offset from UTC.
  393. The tz offset value is simply added to the resulting bucket's date value.<p>
  394. <p>For example, to use EST you would set the value to <code>-5</code>.</p>
  395. @member ejs.DateHistogramFacet
  396. @param {Integer} tz An offset value from UTC.
  397. @returns {Object} returns <code>this</code> so that calls can be chained.
  398. */
  399. postZone: function (tz) {
  400. if (tz == null) {
  401. return facet[name].date_histogram.post_zone;
  402. }
  403. facet[name].date_histogram.post_zone = tz;
  404. return this;
  405. },
  406. /**
  407. Set's a specific pre-rounding offset. Format is 1d, 1h, etc.
  408. @member ejs.DateHistogramFacet
  409. @param {String} offset The offset as a string (1d, 1h, etc)
  410. @returns {Object} returns <code>this</code> so that calls can be chained.
  411. */
  412. preOffset: function (offset) {
  413. if (offset == null) {
  414. return facet[name].date_histogram.pre_offset;
  415. }
  416. facet[name].date_histogram.pre_offset = offset;
  417. return this;
  418. },
  419. /**
  420. Set's a specific post-rounding offset. Format is 1d, 1h, etc.
  421. @member ejs.DateHistogramFacet
  422. @param {String} offset The offset as a string (1d, 1h, etc)
  423. @returns {Object} returns <code>this</code> so that calls can be chained.
  424. */
  425. postOffset: function (offset) {
  426. if (offset == null) {
  427. return facet[name].date_histogram.post_offset;
  428. }
  429. facet[name].date_histogram.post_offset = offset;
  430. return this;
  431. },
  432. /**
  433. <p>The date histogram works on numeric values (since time is stored
  434. in milliseconds since the epoch in UTC).<p>
  435. <p>But, sometimes, systems will store a different resolution (like seconds since UTC)
  436. in a numeric field. The factor parameter can be used to change the value in the field
  437. to milliseconds to actual do the relevant rounding, and then be applied again to get to
  438. the original unit.</p>
  439. <p>For example, when storing in a numeric field seconds resolution,
  440. the factor can be set to 1000.<p>
  441. @member ejs.DateHistogramFacet
  442. @param {Integer} f The conversion factor.
  443. @returns {Object} returns <code>this</code> so that calls can be chained.
  444. */
  445. factor: function (f) {
  446. if (f == null) {
  447. return facet[name].date_histogram.factor;
  448. }
  449. facet[name].date_histogram.factor = f;
  450. return this;
  451. },
  452. /**
  453. Allows you modify the <code>value</code> field using a script. The modified value
  454. is then used to compute the statistical data.
  455. @member ejs.DateHistogramFacet
  456. @param {String} scriptCode A valid script string to execute.
  457. @returns {Object} returns <code>this</code> so that calls can be chained.
  458. */
  459. valueScript: function (scriptCode) {
  460. if (scriptCode == null) {
  461. return facet[name].date_histogram.value_script;
  462. }
  463. facet[name].date_histogram.value_script = scriptCode;
  464. return this;
  465. },
  466. /**
  467. <p>Sets the type of ordering that will be performed on the date
  468. buckets. Valid values are:<p>
  469. <dl>
  470. <dd><code>time</code> - the default, sort by the buckets start time in milliseconds.</dd>
  471. <dd><code>count</code> - sort by the number of items in the bucket</dd>
  472. <dd><code>total</code> - sort by the sum/total of the items in the bucket</dd>
  473. <dl>
  474. @member ejs.DateHistogramFacet
  475. @param {String} o The ordering method: time, count, or total.
  476. @returns {Object} returns <code>this</code> so that calls can be chained.
  477. */
  478. order: function (o) {
  479. if (o == null) {
  480. return facet[name].date_histogram.order;
  481. }
  482. o = o.toLowerCase();
  483. if (o === 'time' || o === 'count' || o === 'total') {
  484. facet[name].date_histogram.order = o;
  485. }
  486. return this;
  487. },
  488. /**
  489. The script language being used. Currently supported values are
  490. <code>javascript</code>, <code>groovy</code>, and <code>mvel</code>.
  491. @member ejs.DateHistogramFacet
  492. @param {String} language The language of the script.
  493. @returns {Object} returns <code>this</code> so that calls can be chained.
  494. */
  495. lang: function (language) {
  496. if (language == null) {
  497. return facet[name].date_histogram.lang;
  498. }
  499. facet[name].date_histogram.lang = language;
  500. return this;
  501. },
  502. /**
  503. Sets parameters that will be applied to the script. Overwrites
  504. any existing params.
  505. @member ejs.DateHistogramFacet
  506. @param {Object} p An object where the keys are the parameter name and
  507. values are the parameter value.
  508. @returns {Object} returns <code>this</code> so that calls can be chained.
  509. */
  510. params: function (p) {
  511. if (p == null) {
  512. return facet[name].date_histogram.params;
  513. }
  514. facet[name].date_histogram.params = p;
  515. return this;
  516. },
  517. /**
  518. <p>Allows you to reduce the documents used for computing facet results.</p>
  519. @member ejs.DateHistogramFacet
  520. @param {Object} oFilter A valid <code>Filter</code> object.
  521. @returns {Object} returns <code>this</code> so that calls can be chained.
  522. */
  523. facetFilter: function (oFilter) {
  524. if (oFilter == null) {
  525. return facet[name].facet_filter;
  526. }
  527. if (!isFilter(oFilter)) {
  528. throw new TypeError('Argument must be a Filter');
  529. }
  530. facet[name].facet_filter = oFilter._self();
  531. return this;
  532. },
  533. /**
  534. <p>Computes values across the entire index</p>
  535. @member ejs.DateHistogramFacet
  536. @param {Boolean} trueFalse Calculate facet counts globally or not.
  537. @returns {Object} returns <code>this</code> so that calls can be chained.
  538. */
  539. global: function (trueFalse) {
  540. if (trueFalse == null) {
  541. return facet[name].global;
  542. }
  543. facet[name].global = trueFalse;
  544. return this;
  545. },
  546. /**
  547. <p>Sets the mode the facet will use.<p>
  548. <dl>
  549. <dd><code>collector</code></dd>
  550. <dd><code>post</code></dd>
  551. <dl>
  552. @member ejs.DateHistogramFacet
  553. @param {String} m The mode: collector or post.
  554. @returns {Object} returns <code>this</code> so that calls can be chained.
  555. */
  556. mode: function (m) {
  557. if (m == null) {
  558. return facet[name].mode;
  559. }
  560. m = m.toLowerCase();
  561. if (m === 'collector' || m === 'post') {
  562. facet[name].mode = m;
  563. }
  564. return this;
  565. },
  566. /**
  567. <p>Computes values across the the specified scope</p>
  568. @deprecated since elasticsearch 0.90
  569. @member ejs.DateHistogramFacet
  570. @param {String} scope The scope name to calculate facet counts with.
  571. @returns {Object} returns <code>this</code> so that calls can be chained.
  572. */
  573. scope: function (scope) {
  574. return this;
  575. },
  576. /**
  577. <p>Enables caching of the <code>facetFilter</code></p>
  578. @member ejs.DateHistogramFacet
  579. @param {Boolean} trueFalse If the facetFilter should be cached or not
  580. @returns {Object} returns <code>this</code> so that calls can be chained.
  581. */
  582. cacheFilter: function (trueFalse) {
  583. if (trueFalse == null) {
  584. return facet[name].cache_filter;
  585. }
  586. facet[name].cache_filter = trueFalse;
  587. return this;
  588. },
  589. /**
  590. <p>Sets the path to the nested document if faceting against a
  591. nested field.</p>
  592. @member ejs.DateHistogramFacet
  593. @param {String} path The nested path
  594. @returns {Object} returns <code>this</code> so that calls can be chained.
  595. */
  596. nested: function (path) {
  597. if (path == null) {
  598. return facet[name].nested;
  599. }
  600. facet[name].nested = path;
  601. return this;
  602. },
  603. /**
  604. <p>Allows you to serialize this object into a JSON encoded string.</p>
  605. @member ejs.DateHistogramFacet
  606. @returns {String} returns this object as a serialized JSON string.
  607. */
  608. toString: function () {
  609. return JSON.stringify(facet);
  610. },
  611. /**
  612. The type of ejs object. For internal use only.
  613. @member ejs.DateHistogramFacet
  614. @returns {String} the type of object
  615. */
  616. _type: function () {
  617. return 'facet';
  618. },
  619. /**
  620. <p>Retrieves the internal <code>facet</code> object. This is typically used by
  621. internal API functions so use with caution.</p>
  622. @member ejs.DateHistogramFacet
  623. @returns {String} returns this object's internal <code>facet</code> property.
  624. */
  625. _self: function () {
  626. return facet;
  627. }
  628. };
  629. };
  630. /**
  631. @class
  632. <p>The FilterFacet allows you to specify any valid <code>Filter</code> and
  633. have the number of matching hits returned as the value.</p>
  634. <p>Facets are similar to SQL <code>GROUP BY</code> statements but perform much
  635. better. You can also construct several <em>"groups"</em> at once by simply
  636. specifying multiple facets.</p>
  637. <div class="alert-message block-message info">
  638. <p>
  639. <strong>Tip: </strong>
  640. For more information on faceted navigation, see
  641. <a href="http://en.wikipedia.org/wiki/Faceted_classification">this</a>
  642. Wikipedia article on Faceted Classification.
  643. </p>
  644. </div>
  645. @name ejs.FilterFacet
  646. @desc
  647. <p>A facet that return a count of the hits matching the given filter.</p>
  648. @param {String} name The name which be used to refer to this facet. For instance,
  649. the facet itself might utilize a field named <code>doc_authors</code>. Setting
  650. <code>name</code> to <code>Authors</code> would allow you to refer to the
  651. facet by that name, possibly simplifying some of the display logic.
  652. */
  653. ejs.FilterFacet = function (name) {
  654. /**
  655. The internal facet object.
  656. @member ejs.FilterFacet
  657. @property {Object} facet
  658. */
  659. var facet = {};
  660. facet[name] = {};
  661. return {
  662. /**
  663. <p>Sets the filter to be used for this facet.</p>
  664. @member ejs.FilterFacet
  665. @param {Object} oFilter A valid <code>Query</code> object.
  666. @returns {Object} returns <code>this</code> so that calls can be chained.
  667. */
  668. filter: function (oFilter) {
  669. if (oFilter == null) {
  670. return facet[name].filter;
  671. }
  672. if (!isFilter(oFilter)) {
  673. throw new TypeError('Argument must be a Filter');
  674. }
  675. facet[name].filter = oFilter._self();
  676. return this;
  677. },
  678. /**
  679. <p>Allows you to reduce the documents used for computing facet results.</p>
  680. @member ejs.FilterFacet
  681. @param {Object} oFilter A valid <code>Filter</code> object.
  682. @returns {Object} returns <code>this</code> so that calls can be chained.
  683. */
  684. facetFilter: function (oFilter) {
  685. if (oFilter == null) {
  686. return facet[name].facet_filter;
  687. }
  688. if (!isFilter(oFilter)) {
  689. throw new TypeError('Argument must be a Filter');
  690. }
  691. facet[name].facet_filter = oFilter._self();
  692. return this;
  693. },
  694. /**
  695. <p>Computes values across the entire index</p>
  696. @member ejs.FilterFacet
  697. @param {Boolean} trueFalse Calculate facet counts globally or not.
  698. @returns {Object} returns <code>this</code> so that calls can be chained.
  699. */
  700. global: function (trueFalse) {
  701. if (trueFalse == null) {
  702. return facet[name].global;
  703. }
  704. facet[name].global = trueFalse;
  705. return this;
  706. },
  707. /**
  708. <p>Sets the mode the facet will use.<p>
  709. <dl>
  710. <dd><code>collector</code></dd>
  711. <dd><code>post</code></dd>
  712. <dl>
  713. @member ejs.FilterFacet
  714. @param {String} m The mode: collector or post.
  715. @returns {Object} returns <code>this</code> so that calls can be chained.
  716. */
  717. mode: function (m) {
  718. if (m == null) {
  719. return facet[name].mode;
  720. }
  721. m = m.toLowerCase();
  722. if (m === 'collector' || m === 'post') {
  723. facet[name].mode = m;
  724. }
  725. return this;
  726. },
  727. /**
  728. <p>Computes values across the the specified scope</p>
  729. @deprecated since elasticsearch 0.90
  730. @member ejs.FilterFacet
  731. @param {String} scope The scope name to calculate facet counts with.
  732. @returns {Object} returns <code>this</code> so that calls can be chained.
  733. */
  734. scope: function (scope) {
  735. return this;
  736. },
  737. /**
  738. <p>Enables caching of the <code>facetFilter</code></p>
  739. @member ejs.FilterFacet
  740. @param {Boolean} trueFalse If the facetFilter should be cached or not
  741. @returns {Object} returns <code>this</code> so that calls can be chained.
  742. */
  743. cacheFilter: function (trueFalse) {
  744. if (trueFalse == null) {
  745. return facet[name].cache_filter;
  746. }
  747. facet[name].cache_filter = trueFalse;
  748. return this;
  749. },
  750. /**
  751. <p>Sets the path to the nested document if faceting against a
  752. nested field.</p>
  753. @member ejs.FilterFacet
  754. @param {String} path The nested path
  755. @returns {Object} returns <code>this</code> so that calls can be chained.
  756. */
  757. nested: function (path) {
  758. if (path == null) {
  759. return facet[name].nested;
  760. }
  761. facet[name].nested = path;
  762. return this;
  763. },
  764. /**
  765. <p>Allows you to serialize this object into a JSON encoded string.</p>
  766. @member ejs.FilterFacet
  767. @returns {String} returns this object as a serialized JSON string.
  768. */
  769. toString: function () {
  770. return JSON.stringify(facet);
  771. },
  772. /**
  773. The type of ejs object. For internal use only.
  774. @member ejs.FilterFacet
  775. @returns {String} the type of object
  776. */
  777. _type: function () {
  778. return 'facet';
  779. },
  780. /**
  781. <p>Retrieves the internal <code>facet</code> object. This is typically used by
  782. internal API functions so use with caution.</p>
  783. @member ejs.FilterFacet
  784. @returns {String} returns this object's internal <code>facet</code> property.
  785. */
  786. _self: function () {
  787. return facet;
  788. }
  789. };
  790. };
  791. /**
  792. @class
  793. <p>The geoDistanceFacet facet provides information over a range of distances from a
  794. provided point. This includes the number of hits that fall within each range,
  795. along with aggregate information (like total).</p>
  796. <p>Facets are similar to SQL <code>GROUP BY</code> statements but perform much
  797. better. You can also construct several <em>"groups"</em> at once by simply
  798. specifying multiple facets.</p>
  799. <div class="alert-message block-message info">
  800. <p>
  801. <strong>Tip: </strong>
  802. For more information on faceted navigation, see
  803. <a href="http://en.wikipedia.org/wiki/Faceted_classification">this</a>
  804. Wikipedia article on Faceted Classification.
  805. </p>
  806. </div>
  807. @name ejs.GeoDistanceFacet
  808. @desc
  809. <p>A facet which provides information over a range of distances from a provided point.</p>
  810. @param {String} name The name which be used to refer to this facet. For instance,
  811. the facet itself might utilize a field named <code>doc_authors</code>. Setting
  812. <code>name</code> to <code>Authors</code> would allow you to refer to the
  813. facet by that name, possibly simplifying some of the display logic.
  814. */
  815. ejs.GeoDistanceFacet = function (name) {
  816. /**
  817. The internal facet object.
  818. @member ejs.GeoDistanceFacet
  819. @property {Object} facet
  820. */
  821. var facet = {},
  822. point = ejs.GeoPoint([0, 0]),
  823. field = 'location';
  824. facet[name] = {
  825. geo_distance: {
  826. location: point._self(),
  827. ranges: []
  828. }
  829. };
  830. return {
  831. /**
  832. Sets the document field containing the geo-coordinate to be used
  833. to calculate the distance. Defaults to "location".
  834. @member ejs.GeoDistanceFacet
  835. @param {String} fieldName The field name whose data will be used to construct the facet.
  836. @returns {Object} returns <code>this</code> so that calls can be chained.
  837. */
  838. field: function (fieldName) {
  839. var oldValue = facet[name].geo_distance[field];
  840. if (fieldName == null) {
  841. return field;
  842. }
  843. delete facet[name].geo_distance[field];
  844. field = fieldName;
  845. facet[name].geo_distance[fieldName] = oldValue;
  846. return this;
  847. },
  848. /**
  849. Sets the point of origin from where distances will be measured.
  850. @member ejs.GeoDistanceFacet
  851. @param {GeoPoint} p A valid GeoPoint object
  852. @returns {Object} returns <code>this</code> so that calls can be chained.
  853. */
  854. point: function (p) {
  855. if (p == null) {
  856. return point;
  857. }
  858. if (!isGeoPoint(p)) {
  859. throw new TypeError('Argument must be a GeoPoint');
  860. }
  861. point = p;
  862. facet[name].geo_distance[field] = p._self();
  863. return this;
  864. },
  865. /**
  866. Adds a new bounded range.
  867. @member ejs.GeoDistanceFacet
  868. @param {Number} from The lower bound of the range
  869. @param {Number} to The upper bound of the range
  870. @returns {Object} returns <code>this</code> so that calls can be chained.
  871. */
  872. addRange: function (from, to) {
  873. if (arguments.length === 0) {
  874. return facet[name].geo_distance.ranges;
  875. }
  876. facet[name].geo_distance.ranges.push({
  877. from: from,
  878. to: to
  879. });
  880. return this;
  881. },
  882. /**
  883. Adds a new unbounded lower limit.
  884. @member ejs.GeoDistanceFacet
  885. @param {Number} from The lower limit of the unbounded range
  886. @returns {Object} returns <code>this</code> so that calls can be chained.
  887. */
  888. addUnboundedFrom: function (from) {
  889. if (from == null) {
  890. return facet[name].geo_distance.ranges;
  891. }
  892. facet[name].geo_distance.ranges.push({
  893. from: from
  894. });
  895. return this;
  896. },
  897. /**
  898. Adds a new unbounded upper limit.
  899. @member ejs.GeoDistanceFacet
  900. @param {Number} to The upper limit of the unbounded range
  901. @returns {Object} returns <code>this</code> so that calls can be chained.
  902. */
  903. addUnboundedTo: function (to) {
  904. if (to == null) {
  905. return facet[name].geo_distance.ranges;
  906. }
  907. facet[name].geo_distance.ranges.push({
  908. to: to
  909. });
  910. return this;
  911. },
  912. /**
  913. Sets the distance unit. Valid values are "mi" for miles or "km"
  914. for kilometers. Defaults to "km".
  915. @member ejs.GeoDistanceFacet
  916. @param {Number} unit the unit of distance measure.
  917. @returns {Object} returns <code>this</code> so that calls can be chained.
  918. */
  919. unit: function (unit) {
  920. if (unit == null) {
  921. return facet[name].geo_distance.unit;
  922. }
  923. unit = unit.toLowerCase();
  924. if (unit === 'mi' || unit === 'km') {
  925. facet[name].geo_distance.unit = unit;
  926. }
  927. return this;
  928. },
  929. /**
  930. How to compute the distance. Can either be arc (better precision)
  931. or plane (faster). Defaults to arc.
  932. @member ejs.GeoDistanceFacet
  933. @param {String} type The execution type as a string.
  934. @returns {Object} returns <code>this</code> so that calls can be chained.
  935. */
  936. distanceType: function (type) {
  937. if (type == null) {
  938. return facet[name].geo_distance.distance_type;
  939. }
  940. type = type.toLowerCase();
  941. if (type === 'arc' || type === 'plane') {
  942. facet[name].geo_distance.distance_type = type;
  943. }
  944. return this;
  945. },
  946. /**
  947. If the lat/long points should be normalized to lie within their
  948. respective normalized ranges.
  949. Normalized ranges are:
  950. lon = -180 (exclusive) to 180 (inclusive) range
  951. lat = -90 to 90 (both inclusive) range
  952. @member ejs.GeoDistanceFacet
  953. @param {String} trueFalse True if the coordinates should be normalized. False otherwise.
  954. @returns {Object} returns <code>this</code> so that calls can be chained.
  955. */
  956. normalize: function (trueFalse) {
  957. if (trueFalse == null) {
  958. return facet[name].geo_distance.normalize;
  959. }
  960. facet[name].geo_distance.normalize = trueFalse;
  961. return this;
  962. },
  963. /**
  964. Allows you to specify a different value field to aggrerate over.
  965. @member ejs.GeoDistanceFacet
  966. @param {String} fieldName The name of the field to be used.
  967. @returns {Object} returns <code>this</code> so that calls can be chained.
  968. */
  969. valueField: function (fieldName) {
  970. if (fieldName == null) {
  971. return facet[name].geo_distance.value_field;
  972. }
  973. facet[name].geo_distance.value_field = fieldName;
  974. return this;
  975. },
  976. /**
  977. Allows you modify the <code>value</code> field using a script. The modified value
  978. is then used to compute the statistical data.
  979. @member ejs.GeoDistanceFacet
  980. @param {String} scriptCode A valid script string to execute.
  981. @returns {Object} returns <code>this</code> so that calls can be chained.
  982. */
  983. valueScript: function (scriptCode) {
  984. if (scriptCode == null) {
  985. return facet[name].geo_distance.value_script;
  986. }
  987. facet[name].geo_distance.value_script = scriptCode;
  988. return this;
  989. },
  990. /**
  991. The script language being used. Currently supported values are
  992. <code>javascript</code>, <code>groovy</code>, and <code>mvel</code>.
  993. @member ejs.GeoDistanceFacet
  994. @param {String} language The language of the script.
  995. @returns {Object} returns <code>this</code> so that calls can be chained.
  996. */
  997. lang: function (language) {
  998. if (language == null) {
  999. return facet[name].geo_distance.lang;
  1000. }
  1001. facet[name].geo_distance.lang = language;
  1002. return this;
  1003. },
  1004. /**
  1005. Sets parameters that will be applied to the script. Overwrites
  1006. any existing params.
  1007. @member ejs.GeoDistanceFacet
  1008. @param {Object} p An object where the keys are the parameter name and
  1009. values are the parameter value.
  1010. @returns {Object} returns <code>this</code> so that calls can be chained.
  1011. */
  1012. params: function (p) {
  1013. if (p == null) {
  1014. return facet[name].geo_distance.params;
  1015. }
  1016. facet[name].geo_distance.params = p;
  1017. return this;
  1018. },
  1019. /**
  1020. <p>Allows you to reduce the documents used for computing facet results.</p>
  1021. @member ejs.GeoDistanceFacet
  1022. @param {Object} oFilter A valid <code>Filter</code> object.
  1023. @returns {Object} returns <code>this</code> so that calls can be chained.
  1024. */
  1025. facetFilter: function (oFilter) {
  1026. if (oFilter == null) {
  1027. return facet[name].facet_filter;
  1028. }
  1029. if (!isFilter(oFilter)) {
  1030. throw new TypeError('Argument must be a Filter');
  1031. }
  1032. facet[name].facet_filter = oFilter._self();
  1033. return this;
  1034. },
  1035. /**
  1036. <p>Computes values across the entire index</p>
  1037. @member ejs.GeoDistanceFacet
  1038. @param {Boolean} trueFalse Calculate facet counts globally or not.
  1039. @returns {Object} returns <code>this</code> so that calls can be chained.
  1040. */
  1041. global: function (trueFalse) {
  1042. if (trueFalse == null) {
  1043. return facet[name].global;
  1044. }
  1045. facet[name].global = trueFalse;
  1046. return this;
  1047. },
  1048. /**
  1049. <p>Sets the mode the facet will use.<p>
  1050. <dl>
  1051. <dd><code>collector</code></dd>
  1052. <dd><code>post</code></dd>
  1053. <dl>
  1054. @member ejs.GeoDistanceFacet
  1055. @param {String} m The mode: collector or post.
  1056. @returns {Object} returns <code>this</code> so that calls can be chained.
  1057. */
  1058. mode: function (m) {
  1059. if (m == null) {
  1060. return facet[name].mode;
  1061. }
  1062. m = m.toLowerCase();
  1063. if (m === 'collector' || m === 'post') {
  1064. facet[name].mode = m;
  1065. }
  1066. return this;
  1067. },
  1068. /**
  1069. <p>Computes values across the the specified scope</p>
  1070. @deprecated since elasticsearch 0.90
  1071. @member ejs.GeoDistanceFacet
  1072. @param {String} scope The scope name to calculate facet counts with.
  1073. @returns {Object} returns <code>this</code> so that calls can be chained.
  1074. */
  1075. scope: function (scope) {
  1076. return this;
  1077. },
  1078. /**
  1079. <p>Enables caching of the <code>facetFilter</code></p>
  1080. @member ejs.GeoDistanceFacet
  1081. @param {Boolean} trueFalse If the facetFilter should be cached or not
  1082. @returns {Object} returns <code>this</code> so that calls can be chained.
  1083. */
  1084. cacheFilter: function (trueFalse) {
  1085. if (trueFalse == null) {
  1086. return facet[name].cache_filter;
  1087. }
  1088. facet[name].cache_filter = trueFalse;
  1089. return this;
  1090. },
  1091. /**
  1092. <p>Sets the path to the nested document if faceting against a
  1093. nested field.</p>
  1094. @member ejs.GeoDistanceFacet
  1095. @param {String} path The nested path
  1096. @returns {Object} returns <code>this</code> so that calls can be chained.
  1097. */
  1098. nested: function (path) {
  1099. if (path == null) {
  1100. return facet[name].nested;
  1101. }
  1102. facet[name].nested = path;
  1103. return this;
  1104. },
  1105. /**
  1106. <p>Allows you to serialize this object into a JSON encoded string.</p>
  1107. @member ejs.GeoDistanceFacet
  1108. @returns {String} returns this object as a serialized JSON string.
  1109. */
  1110. toString: function () {
  1111. return JSON.stringify(facet);
  1112. },
  1113. /**
  1114. The type of ejs object. For internal use only.
  1115. @member ejs.GeoDistanceFacet
  1116. @returns {String} the type of object
  1117. */
  1118. _type: function () {
  1119. return 'facet';
  1120. },
  1121. /**
  1122. <p>Retrieves the internal <code>facet</code> object. This is typically used by
  1123. internal API functions so use with caution.</p>
  1124. @member ejs.GeoDistanceFacet
  1125. @returns {String} returns this object's internal <code>facet</code> property.
  1126. */
  1127. _self: function () {
  1128. return facet;
  1129. }
  1130. };
  1131. };
  1132. /**
  1133. @class
  1134. <p>The histogram facet works with numeric data by building a histogram across intervals
  1135. of the field values. Each value is <em>rounded</em> into an interval (or placed in a
  1136. bucket), and statistics are provided per interval/bucket (count and total).</p>
  1137. <p>Facets are similar to SQL <code>GROUP BY</code> statements but perform much
  1138. better. You can also construct several <em>"groups"</em> at once by simply
  1139. specifying multiple facets.</p>
  1140. <div class="alert-message block-message info">
  1141. <p>
  1142. <strong>Tip: </strong>
  1143. For more information on faceted navigation, see
  1144. <a href="http://en.wikipedia.org/wiki/Faceted_classification">this</a>
  1145. Wikipedia article on Faceted Classification.
  1146. </p>
  1147. </div>
  1148. @name ejs.HistogramFacet
  1149. @desc
  1150. <p>A facet which returns the N most frequent terms within a collection
  1151. or set of collections.</p>
  1152. @param {String} name The name which be used to refer to this facet. For instance,
  1153. the facet itself might utilize a field named <code>doc_authors</code>. Setting
  1154. <code>name</code> to <code>Authors</code> would allow you to refer to the
  1155. facet by that name, possibly simplifying some of the display logic.
  1156. */
  1157. ejs.HistogramFacet = function (name) {
  1158. /**
  1159. The internal facet object.
  1160. @member ejs.HistogramFacet
  1161. @property {Object} facet
  1162. */
  1163. var facet = {};
  1164. facet[name] = {
  1165. histogram: {}
  1166. };
  1167. return {
  1168. /**
  1169. Sets the field to be used to construct the this facet.
  1170. @member ejs.HistogramFacet
  1171. @param {String} fieldName The field name whose data will be used to construct the facet.
  1172. @returns {Object} returns <code>this</code> so that calls can be chained.
  1173. */
  1174. field: function (fieldName) {
  1175. if (fieldName == null) {
  1176. return facet[name].histogram.field;
  1177. }
  1178. facet[name].histogram.field = fieldName;
  1179. return this;
  1180. },
  1181. /**
  1182. Sets the bucket interval used to calculate the distribution.
  1183. @member ejs.HistogramFacet
  1184. @param {Number} numericInterval The bucket interval in which to group values.
  1185. @returns {Object} returns <code>this</code> so that calls can be chained.
  1186. */
  1187. interval: function (numericInterval) {
  1188. if (numericInterval == null) {
  1189. return facet[name].histogram.interval;
  1190. }
  1191. facet[name].histogram.interval = numericInterval;
  1192. return this;
  1193. },
  1194. /**
  1195. Sets the bucket interval used to calculate the distribution based
  1196. on a time value such as "1d", "1w", etc.
  1197. @member ejs.HistogramFacet
  1198. @param {Number} timeInterval The bucket interval in which to group values.
  1199. @returns {Object} returns <code>this</code> so that calls can be chained.
  1200. */
  1201. timeInterval: function (timeInterval) {
  1202. if (timeInterval == null) {
  1203. return facet[name].histogram.time_interval;
  1204. }
  1205. facet[name].histogram.time_interval = timeInterval;
  1206. return this;
  1207. },
  1208. /**
  1209. Sets the "from", "start", or lower bounds bucket. For example if
  1210. you have a value of 1023, an interval of 100, and a from value of
  1211. 1500, it will be placed into the 1500 bucket vs. the normal bucket
  1212. of 1000.
  1213. @member ejs.HistogramFacet
  1214. @param {Number} from the lower bounds bucket value.
  1215. @returns {Object} returns <code>this</code> so that calls can be chained.
  1216. */
  1217. from: function (from) {
  1218. if (from == null) {
  1219. return facet[name].histogram.from;
  1220. }
  1221. facet[name].histogram.from = from;
  1222. return this;
  1223. },
  1224. /**
  1225. Sets the "to", "end", or upper bounds bucket. For example if
  1226. you have a value of 1023, an interval of 100, and a to value of
  1227. 900, it will be placed into the 900 bucket vs. the normal bucket
  1228. of 1000.
  1229. @member ejs.HistogramFacet
  1230. @param {Number} to the upper bounds bucket value.
  1231. @returns {Object} returns <code>this</code> so that calls can be chained.
  1232. */
  1233. to: function (to) {
  1234. if (to == null) {
  1235. return facet[name].histogram.to;
  1236. }
  1237. facet[name].histogram.to = to;
  1238. return this;
  1239. },
  1240. /**
  1241. Allows you to specify a different value field to aggrerate over.
  1242. @member ejs.HistogramFacet
  1243. @param {String} fieldName The name of the field to be used.
  1244. @returns {Object} returns <code>this</code> so that calls can be chained.
  1245. */
  1246. valueField: function (fieldName) {
  1247. if (fieldName == null) {
  1248. return facet[name].histogram.value_field;
  1249. }
  1250. facet[name].histogram.value_field = fieldName;
  1251. return this;
  1252. },
  1253. /**
  1254. Allows you to specify a different key field to be used to group intervals.
  1255. @member ejs.HistogramFacet
  1256. @param {String} fieldName The name of the field to be used.
  1257. @returns {Object} returns <code>this</code> so that calls can be chained.
  1258. */
  1259. keyField: function (fieldName) {
  1260. if (fieldName == null) {
  1261. return facet[name].histogram.key_field;
  1262. }
  1263. facet[name].histogram.key_field = fieldName;
  1264. return this;
  1265. },
  1266. /**
  1267. Allows you modify the <code>value</code> field using a script. The modified value
  1268. is then used to compute the statistical data.
  1269. @member ejs.HistogramFacet
  1270. @param {String} scriptCode A valid script string to execute.
  1271. @returns {Object} returns <code>this</code> so that calls can be chained.
  1272. */
  1273. valueScript: function (scriptCode) {
  1274. if (scriptCode == null) {
  1275. return facet[name].histogram.value_script;
  1276. }
  1277. facet[name].histogram.value_script = scriptCode;