Aggregation.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. <?php
  2. namespace Elastica\QueryBuilder\DSL;
  3. use Elastica\Aggregation\Avg;
  4. use Elastica\Aggregation\AvgBucket;
  5. use Elastica\Aggregation\BucketScript;
  6. use Elastica\Aggregation\Cardinality;
  7. use Elastica\Aggregation\DateHistogram;
  8. use Elastica\Aggregation\DateRange;
  9. use Elastica\Aggregation\ExtendedStats;
  10. use Elastica\Aggregation\Filter as FilterAggregation;
  11. use Elastica\Aggregation\Filters;
  12. use Elastica\Aggregation\GeoDistance;
  13. use Elastica\Aggregation\GeohashGrid;
  14. use Elastica\Aggregation\GlobalAggregation;
  15. use Elastica\Aggregation\Histogram;
  16. use Elastica\Aggregation\IpRange;
  17. use Elastica\Aggregation\Max;
  18. use Elastica\Aggregation\Min;
  19. use Elastica\Aggregation\Missing;
  20. use Elastica\Aggregation\Nested;
  21. use Elastica\Aggregation\Percentiles;
  22. use Elastica\Aggregation\Range;
  23. use Elastica\Aggregation\ReverseNested;
  24. use Elastica\Aggregation\ScriptedMetric;
  25. use Elastica\Aggregation\SerialDiff;
  26. use Elastica\Aggregation\SignificantTerms;
  27. use Elastica\Aggregation\Stats;
  28. use Elastica\Aggregation\Sum;
  29. use Elastica\Aggregation\SumBucket;
  30. use Elastica\Aggregation\Terms;
  31. use Elastica\Aggregation\TopHits;
  32. use Elastica\Aggregation\ValueCount;
  33. use Elastica\Exception\NotImplementedException;
  34. use Elastica\Query\AbstractQuery;
  35. use Elastica\QueryBuilder\DSL;
  36. /**
  37. * elasticsearch aggregation DSL.
  38. *
  39. * @author Manuel Andreo Garcia <andreo.garcia@googlemail.com>
  40. *
  41. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html
  42. */
  43. class Aggregation implements DSL
  44. {
  45. /**
  46. * must return type for QueryBuilder usage.
  47. *
  48. * @return string
  49. */
  50. public function getType()
  51. {
  52. return DSL::TYPE_AGGREGATION;
  53. }
  54. /**
  55. * min aggregation.
  56. *
  57. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-min-aggregation.html
  58. *
  59. * @param string $name
  60. *
  61. * @return Min
  62. */
  63. public function min($name)
  64. {
  65. return new Min($name);
  66. }
  67. /**
  68. * max aggregation.
  69. *
  70. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-max-aggregation.html
  71. *
  72. * @param string $name
  73. *
  74. * @return Max
  75. */
  76. public function max($name)
  77. {
  78. return new Max($name);
  79. }
  80. /**
  81. * sum aggregation.
  82. *
  83. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-sum-aggregation.html
  84. *
  85. * @param string $name
  86. *
  87. * @return Sum
  88. */
  89. public function sum($name)
  90. {
  91. return new Sum($name);
  92. }
  93. /**
  94. * sum bucket aggregation.
  95. *
  96. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-sum-bucket-aggregation.html
  97. *
  98. * @param string $name
  99. * @param string|null $bucketsPath
  100. *
  101. * @return SumBucket
  102. */
  103. public function sum_bucket($name, $bucketsPath = null)
  104. {
  105. return new SumBucket($name, $bucketsPath);
  106. }
  107. /**
  108. * avg aggregation.
  109. *
  110. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-avg-aggregation.html
  111. *
  112. * @param string $name
  113. *
  114. * @return Avg
  115. */
  116. public function avg($name)
  117. {
  118. return new Avg($name);
  119. }
  120. /**
  121. * avg bucket aggregation.
  122. *
  123. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-avg-bucket-aggregation.html
  124. *
  125. * @param string $name
  126. * @param string|null $bucketsPath
  127. *
  128. * @return AvgBucket
  129. */
  130. public function avg_bucket($name, $bucketsPath = null)
  131. {
  132. return new AvgBucket($name, $bucketsPath);
  133. }
  134. /**
  135. * stats aggregation.
  136. *
  137. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-stats-aggregation.html
  138. *
  139. * @param string $name
  140. *
  141. * @return Stats
  142. */
  143. public function stats($name)
  144. {
  145. return new Stats($name);
  146. }
  147. /**
  148. * extended stats aggregation.
  149. *
  150. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-extendedstats-aggregation.html
  151. *
  152. * @param string $name
  153. *
  154. * @return ExtendedStats
  155. */
  156. public function extended_stats($name)
  157. {
  158. return new ExtendedStats($name);
  159. }
  160. /**
  161. * value count aggregation.
  162. *
  163. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html
  164. *
  165. * @param string $name
  166. * @param string $field
  167. *
  168. * @return ValueCount
  169. */
  170. public function value_count($name, $field)
  171. {
  172. return new ValueCount($name, $field);
  173. }
  174. /**
  175. * percentiles aggregation.
  176. *
  177. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-percentile-aggregation.html
  178. *
  179. * @param string $name the name of this aggregation
  180. * @param string $field the field on which to perform this aggregation
  181. *
  182. * @return Percentiles
  183. */
  184. public function percentiles($name, $field = null)
  185. {
  186. return new Percentiles($name, $field);
  187. }
  188. /**
  189. * percentile ranks aggregation.
  190. *
  191. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-percentile-rank-aggregation.html
  192. *
  193. * @param string $name
  194. */
  195. public function percentile_ranks($name)
  196. {
  197. throw new NotImplementedException();
  198. }
  199. /**
  200. * cardinality aggregation.
  201. *
  202. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html
  203. *
  204. * @param string $name
  205. *
  206. * @return Cardinality
  207. */
  208. public function cardinality($name)
  209. {
  210. return new Cardinality($name);
  211. }
  212. /**
  213. * geo bounds aggregation.
  214. *
  215. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-geobounds-aggregation.html
  216. *
  217. * @param string $name
  218. */
  219. public function geo_bounds($name)
  220. {
  221. throw new NotImplementedException();
  222. }
  223. /**
  224. * top hits aggregation.
  225. *
  226. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html
  227. *
  228. * @param string $name
  229. *
  230. * @return TopHits
  231. */
  232. public function top_hits($name)
  233. {
  234. return new TopHits($name);
  235. }
  236. /**
  237. * scripted metric aggregation.
  238. *
  239. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html
  240. *
  241. * @param string $name
  242. * @param string|null $initScript
  243. * @param string|null $mapScript
  244. * @param string|null $combineScript
  245. * @param string|null $reduceScript
  246. *
  247. * @return ScriptedMetric
  248. */
  249. public function scripted_metric($name, $initScript = null, $mapScript = null, $combineScript = null, $reduceScript = null)
  250. {
  251. return new ScriptedMetric($name, $initScript, $mapScript, $combineScript, $reduceScript);
  252. }
  253. /**
  254. * global aggregation.
  255. *
  256. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-global-aggregation.html
  257. *
  258. * @param string $name
  259. *
  260. * @return GlobalAggregation
  261. */
  262. public function global_agg($name)
  263. {
  264. return new GlobalAggregation($name);
  265. }
  266. /**
  267. * filter aggregation.
  268. *
  269. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html
  270. *
  271. * @param string $name
  272. * @param AbstractQuery $filter
  273. *
  274. * @return FilterAggregation
  275. */
  276. public function filter($name, AbstractQuery $filter = null)
  277. {
  278. return new FilterAggregation($name, $filter);
  279. }
  280. /**
  281. * filters aggregation.
  282. *
  283. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filters-aggregation.html
  284. *
  285. * @param string $name
  286. *
  287. * @return Filters
  288. */
  289. public function filters($name)
  290. {
  291. return new Filters($name);
  292. }
  293. /**
  294. * missing aggregation.
  295. *
  296. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-missing-aggregation.html
  297. *
  298. * @param string $name
  299. * @param string $field
  300. *
  301. * @return Missing
  302. */
  303. public function missing($name, $field)
  304. {
  305. return new Missing($name, $field);
  306. }
  307. /**
  308. * nested aggregation.
  309. *
  310. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html
  311. *
  312. * @param string $name
  313. * @param string $path the nested path for this aggregation
  314. *
  315. * @return Nested
  316. */
  317. public function nested($name, $path)
  318. {
  319. return new Nested($name, $path);
  320. }
  321. /**
  322. * reverse nested aggregation.
  323. *
  324. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-reverse-nested-aggregation.html
  325. *
  326. * @param string $name The name of this aggregation
  327. * @param string $path Optional path to the nested object for this aggregation. Defaults to the root of the main document.
  328. *
  329. * @return ReverseNested
  330. */
  331. public function reverse_nested($name, $path = null)
  332. {
  333. return new ReverseNested($name);
  334. }
  335. /**
  336. * children aggregation.
  337. *
  338. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-children-aggregation.html
  339. *
  340. * @param string $name
  341. */
  342. public function children($name)
  343. {
  344. throw new NotImplementedException();
  345. }
  346. /**
  347. * terms aggregation.
  348. *
  349. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html
  350. *
  351. * @param string $name
  352. *
  353. * @return Terms
  354. */
  355. public function terms($name)
  356. {
  357. return new Terms($name);
  358. }
  359. /**
  360. * significant terms aggregation.
  361. *
  362. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-significantterms-aggregation.html
  363. *
  364. * @param string $name
  365. *
  366. * @return SignificantTerms
  367. */
  368. public function significant_terms($name)
  369. {
  370. return new SignificantTerms($name);
  371. }
  372. /**
  373. * range aggregation.
  374. *
  375. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-range-aggregation.html
  376. *
  377. * @param string $name
  378. *
  379. * @return Range
  380. */
  381. public function range($name)
  382. {
  383. return new Range($name);
  384. }
  385. /**
  386. * date range aggregation.
  387. *
  388. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-daterange-aggregation.html
  389. *
  390. * @param string $name
  391. *
  392. * @return DateRange
  393. */
  394. public function date_range($name)
  395. {
  396. return new DateRange($name);
  397. }
  398. /**
  399. * ipv4 range aggregation.
  400. *
  401. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-iprange-aggregation.html
  402. *
  403. * @param string $name
  404. * @param string $field
  405. *
  406. * @return IpRange
  407. */
  408. public function ipv4_range($name, $field)
  409. {
  410. return new IpRange($name, $field);
  411. }
  412. /**
  413. * histogram aggregation.
  414. *
  415. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-histogram-aggregation.html
  416. *
  417. * @param string $name the name of this aggregation
  418. * @param string $field the name of the field on which to perform the aggregation
  419. * @param int $interval the interval by which documents will be bucketed
  420. *
  421. * @return Histogram
  422. */
  423. public function histogram($name, $field, $interval)
  424. {
  425. return new Histogram($name, $field, $interval);
  426. }
  427. /**
  428. * date histogram aggregation.
  429. *
  430. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html
  431. *
  432. * @param string $name the name of this aggregation
  433. * @param string $field the name of the field on which to perform the aggregation
  434. * @param int $interval the interval by which documents will be bucketed
  435. *
  436. * @return DateHistogram
  437. */
  438. public function date_histogram($name, $field, $interval)
  439. {
  440. return new DateHistogram($name, $field, $interval);
  441. }
  442. /**
  443. * geo distance aggregation.
  444. *
  445. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-geodistance-aggregation.html
  446. *
  447. * @param string $name the name if this aggregation
  448. * @param string $field the field on which to perform this aggregation
  449. * @param string|array $origin the point from which distances will be calculated
  450. *
  451. * @return GeoDistance
  452. */
  453. public function geo_distance($name, $field, $origin)
  454. {
  455. return new GeoDistance($name, $field, $origin);
  456. }
  457. /**
  458. * geohash grid aggregation.
  459. *
  460. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-geohashgrid-aggregation.html
  461. *
  462. * @param string $name the name of this aggregation
  463. * @param string $field the field on which to perform this aggregation
  464. *
  465. * @return GeohashGrid
  466. */
  467. public function geohash_grid($name, $field)
  468. {
  469. return new GeohashGrid($name, $field);
  470. }
  471. /**
  472. * bucket script aggregation.
  473. *
  474. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-script-aggregation.html
  475. *
  476. * @param string $name
  477. * @param array|null $bucketsPath
  478. * @param string|null $script
  479. *
  480. * @return BucketScript
  481. */
  482. public function bucket_script($name, $bucketsPath = null, $script = null)
  483. {
  484. return new BucketScript($name, $bucketsPath, $script);
  485. }
  486. /**
  487. * serial diff aggregation.
  488. *
  489. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-serialdiff-aggregation.html
  490. *
  491. * @param string $name
  492. * @param string|null $bucketsPath
  493. *
  494. * @return SerialDiff
  495. */
  496. public function serial_diff($name, $bucketsPath = null)
  497. {
  498. return new SerialDiff($name, $bucketsPath);
  499. }
  500. }