Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / search / fulltextstorage / PhabricatorElasticsearchQueryBuilder.php
blob0f835eb72674da9c70f4e98f17bed17c404526b5
1 <?php
3 class PhabricatorElasticsearchQueryBuilder {
4 protected $name;
5 protected $clauses = array();
8 public function getClauses($termkey = null) {
9 $clauses = $this->clauses;
10 if ($termkey == null) {
11 return $clauses;
13 if (isset($clauses[$termkey])) {
14 return $clauses[$termkey];
16 return array();
19 public function getClauseCount($clausekey) {
20 if (isset($this->clauses[$clausekey])) {
21 return count($this->clauses[$clausekey]);
22 } else {
23 return 0;
27 public function addExistsClause($field) {
28 return $this->addClause('filter', array(
29 'exists' => array(
30 'field' => $field,
32 ));
35 public function addTermsClause($field, $values) {
36 return $this->addClause('filter', array(
37 'terms' => array(
38 $field => array_values($values),
40 ));
43 public function addMustClause($clause) {
44 return $this->addClause('must', $clause);
47 public function addFilterClause($clause) {
48 return $this->addClause('filter', $clause);
51 public function addShouldClause($clause) {
52 return $this->addClause('should', $clause);
55 public function addMustNotClause($clause) {
56 return $this->addClause('must_not', $clause);
59 public function addClause($clause, $terms) {
60 $this->clauses[$clause][] = $terms;
61 return $this;
64 public function toArray() {
65 $clauses = $this->getClauses();
66 return $clauses;
67 $cleaned = array();
68 foreach ($clauses as $clause => $subclauses) {
69 if (is_array($subclauses) && count($subclauses) == 1) {
70 $cleaned[$clause] = array_shift($subclauses);
71 } else {
72 $cleaned[$clause] = $subclauses;
75 return $cleaned;