public function testToArray() { $range = new Elastica_Query_Range(); $field = array('from' => 20, 'to' => 40); $range->addField('age', $field); $expectedArray = array('range' => array('age' => $field)); $this->assertEquals($expectedArray, $range->toArray()); }
private static function _build_query($option) { $bool = null; if ($option['must']) { foreach ($option['must'] as $field => $values) { if ($values = !is_array($values) ? array($values) : $values) { foreach ($values as $value) { $bool = !$bool ? new Elastica_Query_Bool() : $bool; $text = new Elastica_Query_Match(); $text->setMatch($field, $value); $bool->addMust($text); } } } } if ($option['must_not']) { foreach ($option['must_not'] as $field => $values) { if ($values = !is_array($values) ? array($values) : $values) { foreach ($values as $value) { $bool = !$bool ? new Elastica_Query_Bool() : $bool; $text = new Elastica_Query_Match(); $text->setMatch($field, $value); $bool->addMustNot($text); } } } } if ($option['should']) { foreach ($option['should'] as $field => $values) { if ($values = !is_array($values) ? array($values) : $values) { foreach ($values as $value) { $bool = !$bool ? new Elastica_Query_Bool() : $bool; $text = new Elastica_Query_Match(); $text->setMatch($field, $value); $bool->addShould($text); } } } } if ($option['range']) { foreach ($option['range'] as $field => $values) { $bool = !$bool ? new Elastica_Query_Bool() : $bool; $text = new Elastica_Query_Range(); $text->addField($field, $values); $bool->addMust($text); } } $query = $bool ? new Elastica_Query($bool) : new Elastica_Query(); if ($option['sort']) { $query->setSort($option['sort']); } if ($option['offset']) { $query->setFrom($option['offset']); } if ($option['select']) { $query->setFields($option['select']); } if ($option['limit'] > 0) { $query->setSize($option['limit']); } if ($option['script_fields']) { foreach ($option['script_fields'] as $name => $script_field) { if (!(isset($script_field['script']) && $script_field['script'])) { continue; } $script = new Elastica_Script($script_field['script']); if (isset($script_field['params']) && $script_field['params']) { $script->setParams($script_field['params']); } $query->addScriptField($name, $script); } } return $query; }