public static function addDocuments(\ElasticSearchClient $client, $num = 3, $tag = 'cool') { $options = array('refresh' => true); while ($num-- > 0) { $doc = array('title' => "One cool document {$tag}", 'rank' => rand(1, 10)); $client->index($doc, $num + 1, $options); } return $client; }
public function testMapFields() { $client = new \ElasticSearchClient(); $client->init(); $client->configuration(array('index' => 'test-index', 'type' => 'test-type')); $client->index(array('tweet' => 'ElasticSearch is awesome')); $response = $client->map(array('tweet' => array('type' => 'string'))); $this->assert->array($response)->isNotEmpty(); }
public function executeSearch(sfWebRequest $request) { $term = strtolower($request->getParameter('q')); $this->q = $term; $transport = new ElasticSearchTransportHTTP('localhost', 9200); $search = new ElasticSearchClient($transport, "cotinga", "item"); $dsl = array('facets' => array('subjects' => array('terms' => array('field' => 'dc.subject.untouched', 'size' => 10)), 'types' => array('terms' => array('field' => 'dc.type', 'size' => 5)), 'language' => array('terms' => array('field' => 'dc.language.iso.untouched', 'size' => 10)), 'creator' => array('terms' => array('field' => 'dc.creator.untouched', 'size' => 10))), 'size' => 50); $filterSubject = $request->getParameter('filters'); if ($filterSubject) { $dsl['query'] = array('filtered' => array('query' => array('dis_max' => array('queries' => array(0 => array('term' => array('dc.title' => $term)), 1 => array('prefix' => array('dc.title' => $term)), 2 => array('prefix' => array('dc.subject' => $term))))))); $values = array(); foreach ($filterSubject as $subject) { foreach ($subject as $field => $value) { $values[] = $value; } } $dsl['query']['filtered']['filter'] = array('query' => array('field' => array('dc.subject' => implode(' AND ', $values)))); } else { $dsl['query'] = array('dis_max' => array('queries' => array(0 => array('term' => array('dc.title' => $term)), 1 => array('prefix' => array('dc.title' => $term)), 2 => array('prefix' => array('dc.subject' => $term)), 3 => array('term' => array('dc.description.abstract' => $term))))); } $this->dsl = $dsl; $results = $search->search($dsl); $this->results = $results; }
public function testBulk() { $esURL = 'http://127.0.0.1:9200/index/type'; putenv("ELASTICSEARCH_URL={$esURL}"); $client = \ElasticSearchClient::connection(); $bulk = $client->beginBulk(); $doc = array('title' => 'First in Line'); $client->index($doc, false, array('refresh' => true)); $doc2 = array('title' => 'Second in Line'); $client->setType('type2'); $client->index($doc2, false); $client->setIndex('index2'); $client->delete(55); $operations = $bulk->getOperations(); $this->assert->integer($bulk->count())->isEqualTo(3)->array($operations[1])->hasSize(2)->array($operations[0])->hasSize(2)->array($operations[0][0])->hasKey('index')->array($operations[0][0]['index'])->hasKey('_refresh')->boolean($operations[0][0]['index']['_refresh'])->isEqualTo(true)->array($operations[1][1])->isEqualTo($doc2)->array($operations[2][0])->hasKey('delete')->array($operations[2][0]['delete'])->hasKey('_id')->integer($operations[2][0]['delete']['_id'])->isEqualTo(55); $payload = '{"index":{"_id":false,"_index":"index","_type":"type","_refresh":true}}' . "\n" . '{"title":"First in Line"}' . "\n" . '{"index":{"_id":false,"_index":"index","_type":"type2"}}' . "\n" . '{"title":"Second in Line"}' . "\n" . '{"delete":{"_id":55,"_index":"index2","_type":"type2"}}' . "\n"; $this->assert->string($bulk->createPayload())->isEqualTo($payload); // Run multiple bulks and make sure all documents are stored $client->beginBulk(); $client->index(array('title' => 'Bulk1'), 1); $client->index(array('title' => 'Bulk2'), 2); $client->commitBulk(); $client->beginBulk(); $client->index(array('title' => 'Bulk3'), 3); $client->index(array('title' => 'Bulk4'), 4); $client->commitBulk(); sleep(1); $resp = $client->search('title:Bulk*'); $this->assert->array($resp)->hasKey('hits')->array($resp['hits'])->hasKey('total')->integer($resp['hits']['total'])->isEqualTo(4); putenv("ELASTICSEARCH_URL"); }
/** * commit this operation */ public function commit() { return $this->client->request('/_bulk', 'POST', $this->createPayload()); }
/** * @expectedException ElasticSearchTransportHTTPException */ public function testSearchThrowExceptionWhenServerDown() { $transport = new ElasticSearchTransportHTTP("localhost", 9300); $search = new ElasticSearchClient($transport, "test-index", "test-type"); $search->search("title:cool"); }