public function testAddIndices() { $client = new Elastica_Client(); $search = new Elastica_Search($client); $indices = array(); $indices[] = $client->getIndex('elastica_test1'); $indices[] = $client->getIndex('elastica_test2'); $search->addIndices($indices); $this->assertEquals(2, count($search->getIndices())); }
/** * Some memory usage stats * * Really simple and quite stupid ... */ public function testServersArray() { $client = new Elastica_Client(); $index = $client->getIndex('test'); $index->create(array(), true); $type = $index->getType('test'); $data = array( 'text1' => 'Very long text for a string', 'text2' => 'But this is not very long', 'text3' => 'random or not random?', ); $startMemory = memory_get_usage(); for ($n = 1; $n < 10; $n++) { $docs = array(); for ($i = 1; $i <= 3000; $i++) { $docs[] = new Elastica_Document(uniqid(), $data); } $type->addDocuments($docs); $docs = array(); } unset($docs); $endMemory = memory_get_usage(); $this->assertLessThan(1.1, $endMemory/$startMemory); }
public function testTextPhrase() { $client = new Elastica_Client(); $index = $client->getIndex('test'); $index->create(array(), true); $type = $index->getType('test'); $doc = new Elastica_Document(1, array('name' => 'Basel-Stadt')); $type->addDocument($doc); $doc = new Elastica_Document(2, array('name' => 'New York')); $type->addDocument($doc); $doc = new Elastica_Document(3, array('name' => 'New Hampshire')); $type->addDocument($doc); $doc = new Elastica_Document(4, array('name' => 'Basel Land')); $type->addDocument($doc); $index->refresh(); $type = 'text_phrase'; $field = 'name'; $query = new Elastica_Query_Text(); $query->setFieldQuery($field, 'Basel New'); $query->setField('operator', 'OR'); $query->setFieldType($field, $type); $resultSet = $index->search($query); $this->assertEquals(4, $resultSet->count()); }
public function testExample() { // Creates a new index 'xodoa' and a type 'user' inside this index $host = 'localhost'; $port = 11211; $client = new Elastica_Client(array('host' => $host, 'port' => $port, 'transport' => 'Memcache')); $index = $client->getIndex('elastica_test1'); $index->create(array(), true); $type = $index->getType('user'); // Adds 1 document to the index $doc1 = new Elastica_Document(1, array('username' => 'hans', 'test' => array('2', '3', '5')) ); $type->addDocument($doc1); // Adds a list of documents with _bulk upload to the index $docs = array(); $docs[] = new Elastica_Document(2, array('username' => 'john', 'test' => array('1', '3', '6')) ); $docs[] = new Elastica_Document(3, array('username' => 'rolf', 'test' => array('2', '3', '7')) ); $type->addDocuments($docs); // Refresh index $index->refresh(); $this->markTestSkipped('Memcache implementation is not finished yet'); $resultSet = $type->search('rolf'); }
public function testGeoPoint() { $client = new Elastica_Client(); $index = $client->getIndex('test'); $index->create(array(), true); $type = $index->getType('test'); // Set mapping $type->setMapping(array('point' => array('type' => 'geo_point'))); // Add doc 1 $doc1 = new Elastica_Document(1, array('name' => 'ruflin')); $doc1->addGeoPoint('point', 17, 19); $type->addDocument($doc1); // Add doc 2 $doc2 = new Elastica_Document(2, array('name' => 'ruflin')); $doc2->addGeoPoint('point', 30, 40); $type->addDocument($doc2); $index->optimize(); $index->refresh(); // Only one point should be in radius $query = new Elastica_Query(); $geoFilter = new Elastica_Filter_GeoDistance('point', array('lat' => 30, 'lon' => 40), '1km'); $query = new Elastica_Query(new Elastica_Query_MatchAll()); $query->setFilter($geoFilter); $this->assertEquals(1, $type->search($query)->count()); // Both points should be inside $query = new Elastica_Query(); $geoFilter = new Elastica_Filter_GeoDistance('point', array('lat' => 30, 'lon' => 40), '40000km'); $query = new Elastica_Query(new Elastica_Query_MatchAll()); $query->setFilter($geoFilter); $index->refresh(); $this->assertEquals(2, $type->search($query)->count()); }
public function testQuery() { $client = new Elastica_Client(); $index = $client->getIndex('test'); $index->create(array(), true); $type = $index->getType('helloworld'); $doc = new Elastica_Document(1, array('name' => 'nicolas ruflin')); $type->addDocument($doc); $doc = new Elastica_Document(2, array('name' => 'ruflin test')); $type->addDocument($doc); $doc = new Elastica_Document(2, array('name' => 'nicolas helloworld')); $type->addDocument($doc); $facet = new Elastica_Facet_Terms('test'); $facet->setField('name'); $query = new Elastica_Query(); $query->addFacet($facet); $query->setQuery(new Elastica_Query_MatchAll()); $index->refresh(); $response = $type->search($query); $facets = $response->getFacets(); $this->assertEquals(3, count($facets['test']['terms'])); }
public function testSetCache() { $client = new Elastica_Client(); $index = $client->getIndex('test'); $index->create(array(), true); $type = $index->getType('test'); $doc = new Elastica_Document(1, array('name' => 'hello world')); $type->addDocument($doc); $doc = new Elastica_Document(2, array('name' => 'nicolas ruflin')); $type->addDocument($doc); $doc = new Elastica_Document(3, array('name' => 'ruflin')); $type->addDocument($doc); $and = new Elastica_Filter_And(); $idsFilter1 = new Elastica_Filter_Ids(); $idsFilter1->setIds(1); $idsFilter2 = new Elastica_Filter_Ids(); $idsFilter2->setIds(1); $and->addFilter($idsFilter1); $and->addFilter($idsFilter2); $index->refresh(); $and->setCached(true); $resultSet = $type->search($and); $this->assertEquals(1, $resultSet->count()); }
public function testFilter() { $client = new Elastica_Client(); $index = $client->getIndex('test'); $index->create(array(), true); $type = $index->getType('helloworld'); $type->addDocument(new Elastica_Document(1, array('color' => 'red'))); $type->addDocument(new Elastica_Document(2, array('color' => 'green'))); $type->addDocument(new Elastica_Document(3, array('color' => 'blue'))); $index->refresh(); $termQuery = new Elastica_Query_Term(array('color' => 'red')); $facet = new Elastica_Facet_Query('test'); $facet->setQuery($termQuery); $query = new Elastica_Query(); $query->addFacet($facet); $resultSet = $type->search($query); $facets = $resultSet->getFacets(); $this->assertEquals(1, $facets['test']['count']); }
public function testFilteredSearch() { $client = new Elastica_Client(); $index = $client->getIndex('test'); $index->create(array(), true); $type = $index->getType('helloworld'); $doc = new Elastica_Document(1, array('name' => 'hello world')); $type->addDocument($doc); $doc = new Elastica_Document(2, array('name' => 'nicolas ruflin')); $type->addDocument($doc); $doc = new Elastica_Document(3, array('name' => 'ruflin')); $type->addDocument($doc); $query = new Elastica_Query_Terms(); $query->setTerms('name', array('nicolas', 'hello')); $index->refresh(); $resultSet = $type->search($query); $this->assertEquals(2, $resultSet->count()); $query->addTerm('ruflin'); $resultSet = $type->search($query); $this->assertEquals(3, $resultSet->count()); }
public function testFilteredSearch() { $client = new Elastica_Client(); $index = $client->getIndex('test'); $index->create(array(), true); $type = $index->getType('helloworld'); $doc = new Elastica_Document(1, array('id' => 1, 'email' => '*****@*****.**', 'username' => 'hanswurst', 'test' => array('2', '3', '5'))); $type->addDocument($doc); $doc = new Elastica_Document(2, array('id' => 2, 'email' => '*****@*****.**', 'username' => 'peter', 'test' => array('2', '3', '5'))); $type->addDocument($doc); $queryString = new Elastica_Query_QueryString('test*'); $filter1 = new Elastica_Filter_Term(); $filter1->addTerm('username', 'peter'); $filter2 = new Elastica_Filter_Term(); $filter2->addTerm('username', 'qwerqwer'); $query1 = new Elastica_Query_Filtered($queryString, $filter1); $query2 = new Elastica_Query_Filtered($queryString, $filter2); $index->refresh(); $resultSet = $type->search($queryString); $this->assertEquals(2, $resultSet->count()); $resultSet = $type->search($query1); $this->assertEquals(1, $resultSet->count()); $resultSet = $type->search($query2); $this->assertEquals(0, $resultSet->count()); }
public function testGeoPoint() { $client = new Elastica_Client(); $index = $client->getIndex('test'); $index->create(array(), true); $type = $index->getType('test'); // Set mapping $type->setMapping(array('point' => array('type' => 'geo_point'))); // Add doc 1 $doc1 = new Elastica_Document(1, array('name' => 'ruflin')); $doc1->addGeoPoint('point', 17, 19); $type->addDocument($doc1); // Add doc 2 $doc2 = new Elastica_Document(2, array('name' => 'ruflin')); $doc2->addGeoPoint('point', 30, 40); $type->addDocument($doc2); $index->refresh(); // Only one point should be in polygon $query = new Elastica_Query(); $points = array(array(16, 16), array(16, 20), array(20, 20), array(20, 16), array(16, 16)); $geoFilter = new Elastica_Filter_GeoPolygon('point', compact('points')); $query = new Elastica_Query(new Elastica_Query_MatchAll()); $query->setFilter($geoFilter); $this->assertEquals(1, $type->search($query)->count()); // Both points should be inside $query = new Elastica_Query(); $points = array(array(16, 16), array(16, 40), array(40, 40), array(40, 16), array(16, 16)); $geoFilter = new Elastica_Filter_GeoPolygon('point', compact('points')); $query = new Elastica_Query(new Elastica_Query_MatchAll()); $query->setFilter($geoFilter); $this->assertEquals(2, $type->search($query)->count()); }
public function testSetSort() { $client = new Elastica_Client(); $index = $client->getIndex('test'); $index->create(array(), true); $type = $index->getType('test'); $doc = new Elastica_Document(1, array('name' => 'hello world')); $type->addDocument($doc); $doc = new Elastica_Document(2, array('firstname' => 'guschti', 'lastname' => 'ruflin')); $type->addDocument($doc); $doc = new Elastica_Document(3, array('firstname' => 'nicolas', 'lastname' => 'ruflin')); $type->addDocument($doc); $queryTerm = new Elastica_Query_Term(); $queryTerm->addTerm('lastname', 'ruflin'); $index->refresh(); $query = Elastica_Query::create($queryTerm); // ASC order $query->setSort(array(array('firstname' => array('order' => 'asc')))); $resultSet = $type->search($query); $this->assertEquals(2, $resultSet->count()); $first = $resultSet->current()->getData(); $second = $resultSet->next()->getData(); $this->assertEquals('guschti', $first['firstname']); $this->assertEquals('nicolas', $second['firstname']); // DESC order $query->setSort(array('firstname' => array('order' => 'desc'))); $resultSet = $type->search($query); $this->assertEquals(2, $resultSet->count()); $first = $resultSet->current()->getData(); $second = $resultSet->next()->getData(); $this->assertEquals('nicolas', $first['firstname']); $this->assertEquals('guschti', $second['firstname']); }
public function testMatchDoc() { $client = new Elastica_Client(array('persistent' => false)); $index = $client->getIndex('elastica_test'); $index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true); $percolator = new Elastica_Percolator($index); $percolatorName = 'percotest'; $query = new Elastica_Query_Term(array('name' => 'ruflin')); $response = $percolator->registerQuery($percolatorName, $query); $this->assertTrue($response->isOk()); $this->assertFalse($response->hasError()); $doc1 = new Elastica_Document(); $doc1->add('name', 'ruflin'); $doc2 = new Elastica_Document(); $doc2->add('name', 'nicolas'); $index = new Elastica_Index($index->getClient(), '_percolator'); $index->refresh(); $matches1 = $percolator->matchDoc($doc1); $this->assertTrue(in_array($percolatorName, $matches1)); $this->assertEquals(1, count($matches1)); $matches2 = $percolator->matchDoc($doc2); $this->assertEmpty($matches2); }
public function testStatisticalWithSetFields() { $client = new Elastica_Client(); $index = $client->getIndex('test'); $index->create(array(), true); $type = $index->getType('helloworld'); $doc = new Elastica_Document(1, array('price' => 10, 'price2' => 20)); $type->addDocument($doc); $doc = new Elastica_Document(2, array('price' => 35, 'price2' => 70)); $type->addDocument($doc); $doc = new Elastica_Document(2, array('price' => 45, 'price2' => 90)); $type->addDocument($doc); $facet = new Elastica_Facet_Statistical('stats'); $facet->setFields(array('price','price2')); $query = new Elastica_Query(); $query->addFacet($facet); $query->setQuery(new Elastica_Query_MatchAll()); $index->refresh(); $response = $type->search($query); $facets = $response->getFacets(); $this->assertEquals(165, $facets['stats']['total']); $this->assertEquals(10, $facets['stats']['min']); $this->assertEquals(90, $facets['stats']['max']); }
public function testGetIdNoSource() { // Creates a new index 'xodoa' and a type 'user' inside this index $indexName = 'xodoa'; $typeName = 'user'; $client = new Elastica_Client(); $index = $client->getIndex($indexName); $index->create(array(), true); $type = $index->getType($typeName); $mapping = new Elastica_Type_Mapping($type); $mapping->disableSource(); $mapping->send(); // Adds 1 document to the index $docId = 3; $doc1 = new Elastica_Document($docId, array('username' => 'hans')); $type->addDocument($doc1); // Refreshes index $index->refresh(); sleep(2); $resultSet = $type->search('hans'); $this->assertEquals(1, $resultSet->count()); $result = $resultSet->current(); $this->assertEquals(array(), $result->getSource()); $this->assertInstanceOf('Elastica_Result', $result); $this->assertEquals($indexName, $result->getIndex()); $this->assertEquals($typeName, $result->getType()); $this->assertEquals($docId, $result->getId()); $this->assertGreaterThan(0, $result->getScore()); $this->assertInternalType('array', $result->getData()); }
public function testQuery() { $client = new Elastica_Client(); $index = $client->getIndex('test'); $index->create(array(), true); $type = $index->getType('test'); $doc = new Elastica_Document(1, array('name' => 'Basel-Stadt')); $type->addDocument($doc); $doc = new Elastica_Document(2, array('name' => 'New York')); $type->addDocument($doc); $doc = new Elastica_Document(3, array('name' => 'Baden')); $type->addDocument($doc); $doc = new Elastica_Document(4, array('name' => 'Baden Baden')); $type->addDocument($doc); $index->refresh(); $type = 'text_phrase'; $field = 'name'; $query = new Elastica_Query_Fuzzy(); $query->addField('name', array('value' => 'Baden')); $resultSet = $index->search($query); $this->assertEquals(2, $resultSet->count()); }
public function testResponse() { $client = new Elastica_Client(); $index = $client->getIndex('test'); $index->create(array(), true); $type = $index->getType('helloworld'); $mapping = new Elastica_Type_Mapping($type, array('name' => array('type' => 'string', 'store' => 'no'), 'dtmPosted' => array('type' => 'date', 'store' => 'no', 'format' => 'yyyy-MM-dd HH:mm:ss'))); $type->setMapping($mapping); $doc = new Elastica_Document(1, array('name' => 'nicolas ruflin', 'dtmPosted' => "2011-06-23 21:53:00")); $type->addDocument($doc); $doc = new Elastica_Document(2, array('name' => 'raul martinez jr', 'dtmPosted' => "2011-06-23 09:53:00")); $type->addDocument($doc); $doc = new Elastica_Document(3, array('name' => 'rachelle clemente', 'dtmPosted' => "2011-07-08 08:53:00")); $type->addDocument($doc); $doc = new Elastica_Document(4, array('name' => 'elastica search', 'dtmPosted' => "2011-07-08 01:53:00")); $type->addDocument($doc); $query = new Elastica_Query(); $query->setQuery(new Elastica_Query_MatchAll()); $index->refresh(); $resultSet = $type->search($query); $engineTime = $resultSet->getResponse()->getEngineTime(); $shardsStats = $resultSet->getResponse()->getShardsStatistics(); $this->assertTrue($engineTime != ''); $this->assertTrue(is_array($shardsStats)); $this->assertArrayHasKey('total', $shardsStats); $this->assertArrayHasKey('successful', $shardsStats); }
public function testTest() { $client = new Elastica_Client(); $index = $client->getIndex('test'); $index->create(array(), true); $type = $index->getType('helloworld'); $mapping = new Elastica_Type_Mapping($type, array('name' => array('type' => 'string', 'store' => 'no'), 'dtmPosted' => array('type' => 'date', 'store' => 'no', 'format' => 'yyyy-MM-dd HH:mm:ss'))); $type->setMapping($mapping); $doc = new Elastica_Document(1, array('name' => 'nicolas ruflin', 'dtmPosted' => "2011-06-23 21:53:00")); $type->addDocument($doc); $doc = new Elastica_Document(2, array('name' => 'raul martinez jr', 'dtmPosted' => "2011-06-23 09:53:00")); $type->addDocument($doc); $doc = new Elastica_Document(3, array('name' => 'rachelle clemente', 'dtmPosted' => "2011-07-08 08:53:00")); $type->addDocument($doc); $doc = new Elastica_Document(4, array('name' => 'elastica search', 'dtmPosted' => "2011-07-08 01:53:00")); $type->addDocument($doc); $facet = new Elastica_Facet_DateHistogram('dateHist1'); $facet->setInterval("day"); $facet->setField("dtmPosted"); $query = new Elastica_Query(); $query->addFacet($facet); $query->setQuery(new Elastica_Query_MatchAll()); $index->refresh(); $response = $type->search($query); $facets = $response->getFacets(); $this->assertEquals(4, $response->getTotalHits()); $this->assertEquals(2, count($facets['dateHist1']['entries'])); }
public function testShouldReturnTheRightNumberOfResult() { $f = new Elastica_Filter_Nested(); $this->assertEquals(array('nested' => array()), $f->toArray()); $q = new Elastica_Query_Terms(); $q->setTerms('hobby', array('guitar')); $f->setPath('hobbies'); $f->setQuery($q); $c = new Elastica_Client(); $s = new Elastica_Search($c); $i = $c->getIndex('elastica_test_filter_nested'); $s->addIndex($i); $r = $s->search($f); $this->assertEquals(1, $r->getTotalHits()); $f = new Elastica_Filter_Nested(); $this->assertEquals(array('nested' => array()), $f->toArray()); $q = new Elastica_Query_Terms(); $q->setTerms('hobby', array('opensource')); $f->setPath('hobbies'); $f->setQuery($q); $c = new Elastica_Client(); $s = new Elastica_Search($c); $i = $c->getIndex('elastica_test_filter_nested'); $s->addIndex($i); $r = $s->search($f); $this->assertEquals(2, $r->getTotalHits()); }
public function testTwoServersSame() { // Creates a new index 'xodoa' and a type 'user' inside this index $client = new Elastica_Client(array('servers' => array( array('host' => 'localhost', 'port' => 9200), array('host' => 'localhost', 'port' => 9200), ))); $index = $client->getIndex('elastica_test1'); $index->create(array(), true); $type = $index->getType('user'); // Adds 1 document to the index $doc1 = new Elastica_Document(1, array('username' => 'hans', 'test' => array('2', '3', '5')) ); $type->addDocument($doc1); // Adds a list of documents with _bulk upload to the index $docs = array(); $docs[] = new Elastica_Document(2, array('username' => 'john', 'test' => array('1', '3', '6')) ); $docs[] = new Elastica_Document(3, array('username' => 'rolf', 'test' => array('2', '3', '7')) ); $type->addDocuments($docs); // Refresh index $index->refresh(); $resultSet = $type->search('rolf'); }
public function testGetSettings() { $indexName = 'test'; $client = new Elastica_Client(); $index = $client->getIndex($indexName); $index->create(array(), true); $status = $index->getStatus(); $settings = $status->getSettings(); $this->assertInternalType('array', $settings); $this->assertTrue(isset($settings['index.number_of_shards'])); }
public function testGetSettings() { $indexName = 'test'; $client = new Elastica_Client(); $index = $client->getIndex($indexName); $index->create(array(), true); $stats = $index->getStats(); $this->assertInstanceOf('Elastica_Index_Stats', $stats); $this->assertTrue($stats->get('ok')); $this->assertEquals(0, $stats->get('_all', 'indices', 'test', 'primaries', 'docs', 'count')); }
public function testServersArray() { $client = new Elastica_Client(); $index = $client->getIndex('test'); $index->create(array(), true); $type = $index->getType('test'); $start = microtime(true); for ($i = 1; $i <= 10000; $i++) { $doc = new Elastica_Document($i, array('test' => 1)); $type->addDocument($doc); } // Refresh index $index->refresh(); $end = microtime(true); //echo $end - $start; }
public function testAliasExists() { $indexName = 'test'; $aliasName = 'test-alias'; $client = new Elastica_Client(); $index1 = $client->getIndex($indexName); $index1->create(array(), true); $status = new Elastica_Status($client); foreach ($status->getIndicesWithAlias($aliasName) as $tmpIndex) { $tmpIndex->removeAlias($aliasName); } $this->assertFalse($status->aliasExists($aliasName)); $index1->addAlias($aliasName); $status->refresh(); $this->assertTrue($status->aliasExists($aliasName)); }
public function testIndexExists() { $indexName = 'elastica_test'; $aliasName = 'elastica_test-alias'; $client = new Elastica_Client(); $index = $client->getIndex($indexName); try { // Make sure index is deleted first $index->delete(); } catch (Elastica_Exception_Response $e) { } $status = new Elastica_Status($client); $this->assertFalse($status->indexExists($indexName)); $index->create(); $status->refresh(); $this->assertTrue($status->indexExists($indexName)); }
public function testNestedMapping() { $client = new Elastica_Client(); $index = $client->getIndex('test'); $index->create(array(), true); $type = $index->getType('test'); $this->markTestIncomplete('nested mapping is not set right yet'); $mapping = new Elastica_Type_Mapping($type, array('test' => array('type' => 'object', 'store' => 'yes', 'properties' => array('user' => array('properties' => array('firstname' => array('type' => 'string', 'store' => 'yes'), 'lastname' => array('type' => 'string', 'store' => 'yes'), 'age' => array('type' => 'integer', 'store' => 'yes'))))))); $type->setMapping($mapping); $doc = new Elastica_Document(1, array('user' => array('firstname' => 'Nicolas', 'lastname' => 'Ruflin', 'age' => 9))); print_r($type->getMapping()); exit; $type->addDocument($doc); $index->refresh(); $resultSet = $type->search('ruflin'); print_r($resultSet); }
/** * Main search controller. * * @param string $key */ function find_search($key) { drupal_add_css(drupal_get_path('module', 'find') . '/css/find_search.css', array('group' => CSS_DEFAULT, 'every_page' => true)); drupal_add_css(drupal_get_path('module', 'find') . '/css/find_print.css', array('group' => CSS_DEFAULT, 'every_page' => true, 'media' => 'print')); drupal_add_js('http://maps.google.com/maps/api/js?sensor=false&libraries=geometry,places', array('group' => JS_LIBRARY)); drupal_add_library('system', 'ui.sortable'); drupal_add_js(drupal_get_path('module', 'area') . '/css/overlay-style.js'); drupal_add_js(drupal_get_path('module', 'area') . '/js/geometry.js'); drupal_add_js(drupal_get_path('module', 'find') . '/js/find.js'); drupal_add_css(drupal_get_path('module', 'gallery') . '/css/jquery.lightbox.css', array('group' => CSS_DEFAULT, 'every_page' => TRUE)); drupal_add_js(drupal_get_path('module', 'gallery') . '/js/jquery.lightbox.js', array('weight' => 100)); drupal_add_js(drupal_get_path('module', 'gallery') . '/js/gallery.lightbox.js'); $variables = array('#theme' => 'find.search'); // fetch $_GET parameters $params = find_query_params(array('search' => '', 'geo' => array(), 'date' => array(), 'class' => array(), 'user' => array(), 'family' => array(), 'genus' => array(), 'town' => array(), 'canton' => array(), 'redlist' => array(), 'habitat' => array(), 'image_type' => array(), 'columns' => array(), 'sort' => array())); $params['uid'] = $GLOBALS['user']->uid; try { $client = new Elastica_Client(); $status = $client->getStatus(); // ping to make sure elasticsearch is running $index = $client->getIndex('naturwerk'); $parameters = new Parameters($params); $variables['#organisms'] = new Organisms($index, $parameters); $variables['#sightings'] = new Sightings($index, $parameters); $variables['#inventories'] = new Inventories($index, $parameters); $variables['#areas'] = new Areas($index, $parameters); $variables['#images'] = new Images($index, $parameters); // make parameters available to JavaScript $parameters = drupal_get_query_parameters(); if (count($parameters) == 0) { $parameters = new stdClass(); // force empty JSON object } drupal_add_js(array('find' => array('url' => url($_GET['q']), 'parameters' => $parameters, 'geo' => $params['geo'])), 'setting'); $variables['#key'] = $key; $variables['#current'] = $variables['#' . $key]; $output = array(); $output['search'] = $variables; return $output; } catch (Elastica_Exception_Client $e) { watchdog('find', $e->getMessage(), array(), WATCHDOG_ERROR); drupal_set_message(t('Search error. Elasticsearch running?'), 'error'); return array(); } }
public static function init($host = '', $index_name = '', $username = '', $password = '') { if (self::$client !== NULL && self::$index !== NULL) { return; } $config = Symphony::Engine()->Configuration()->get('elasticsearch'); if (empty($host)) { $host = $config['host']; } if (empty($index_name)) { $index_name = $config['index-name']; } if (empty($username)) { $username = $config['username']; } if (empty($password)) { $password = $config['password']; } if (empty($host)) { throw new Exception('ElasticSearch "host" not set in configuration.'); } if (empty($index_name)) { throw new Exception('ElasticSearch "index-name" not set in configuration.'); } try { $client = new Elastica_Client(array('url' => $host)); if (!empty($username) && !empty($password)) { $client->addHeader('Authorization', 'Basic ' . base64_encode($username . ':' . $password)); } $client->getStatus(); } catch (Exception $e) { throw new Exception('ElasticSearch client: ' . $e->getMessage()); } $index = $client->getIndex($index_name); if ($auto_create_index) { //if(!$index->exists()) $index->create(array(), TRUE); } self::$client = $client; self::$index = $index; }
public function testHightlightSearch() { $client = new Elastica_Client(); $index = $client->getIndex('test'); $index->create(array(), true); $type = $index->getType('helloworld'); $phrase = 'My name is ruflin'; $doc = new Elastica_Document(1, array('id' => 1, 'phrase' => $phrase, 'username' => 'hanswurst', 'test' => array('2', '3', '5'))); $type->addDocument($doc); $doc = new Elastica_Document(2, array('id' => 2, 'phrase' => $phrase, 'username' => 'peter', 'test' => array('2', '3', '5'))); $type->addDocument($doc); $queryString = new Elastica_Query_QueryString('rufl*'); $query = new Elastica_Query($queryString); $query->setHighlight(array('pre_tags' => array('<em class="highlight">'), 'post_tags' => array('</em>'), 'fields' => array('phrase' => array('fragment_size' => 200, 'number_of_fragments' => 1)))); $index->refresh(); $resultSet = $type->search($query); foreach ($resultSet as $result) { $highlight = $result->getHighlights(); $this->assertEquals(array('phrase' => array(0 => 'My name is <em class="highlight">ruflin</em>')), $highlight); } $this->assertEquals(2, $resultSet->count()); }
public function testQuery() { $client = new Elastica_Client(); $index = $client->getIndex('test'); $index->create(array(), true); $type = $index->getType('helloworld'); $doc = new Elastica_Document(1, array('name' => 'tom', 'paid' => 7)); $type->addDocument($doc); $doc = new Elastica_Document(2, array('name' => 'tom', 'paid' => 2)); $type->addDocument($doc); $doc = new Elastica_Document(3, array('name' => 'tom', 'paid' => 5)); $type->addDocument($doc); $doc = new Elastica_Document(4, array('name' => 'mike', 'paid' => 13)); $type->addDocument($doc); $doc = new Elastica_Document(5, array('name' => 'mike', 'paid' => 1)); $type->addDocument($doc); $doc = new Elastica_Document(6, array('name' => 'mike', 'paid' => 15)); $type->addDocument($doc); $facet = new Elastica_Facet_TermsStats('test'); $facet->setKeyField('name'); $facet->setValueField('paid'); $query = new Elastica_Query(); $query->addFacet($facet); $query->setQuery(new Elastica_Query_MatchAll()); $index->refresh(); $response = $type->search($query); $facets = $response->getFacets(); $this->assertEquals(2, count($facets['test']['terms'])); foreach ($facets['test']['terms'] as $facet) { if ($facet['term'] === 'tom') { $this->assertEquals(14, $facet['total']); } if ($facet['term'] === 'mike') { $this->assertEquals(29, $facet['total']); } } }