public function testDeleteDocument() { // Creates a new index 'xodoa' and a type 'user' inside this index $client = new Elastica_Client(); $index = new Elastica_Index($client, 'xodoa'); $index->create(array(), true); $type = new Elastica_Type($index, 'user'); // Adds hans, john and rolf to the index $docs = array(new Elastica_Document(1, array('username' => 'hans', 'test' => array('2', '3', '5'))), new Elastica_Document(2, array('username' => 'john', 'test' => array('1', '3', '6'))), new Elastica_Document(3, array('username' => 'rolf', 'test' => array('2', '3', '7')))); $type->addDocuments($docs); $index->refresh(); // sanity check for rolf $resultSet = $type->search('rolf'); $this->assertEquals(1, $resultSet->count()); $data = $resultSet->current()->getData(); $this->assertEquals('rolf', $data['username']); // delete rolf $type->deleteById(3); $index->refresh(); // rolf should no longer be there $resultSet = $type->search('rolf'); $this->assertEquals(0, $resultSet->count()); // it should not be possible to delete the entire type with this method try { $type->deleteById(' '); } catch (Exception $e) { /* ignore */ } try { $type->deleteById(null); } catch (Exception $e) { /* ignore */ } try { $type->deleteById(array()); } catch (Exception $e) { /* ignore */ } try { $type->deleteById('*'); } catch (Exception $e) { /* ignore */ } try { $type->deleteById('*:*'); } catch (Exception $e) { /* ignore */ } try { $type->deleteById('!'); } catch (Exception $e) { /* ignore */ } $index->refresh(); // rolf should no longer be there $resultSet = $type->search('john'); $this->assertEquals(1, $resultSet->count()); }
public function testSearch() { $client = new Elastica_Client(); $index = new Elastica_Index($client, 'test'); $index->create(array(), true); $index->getSettings()->setNumberOfReplicas(0); //$index->getSettings()->setNumberOfShards(1); $type = new Elastica_Type($index, 'helloworldfuzzy'); $mapping = new Elastica_Type_Mapping($type , array( 'email' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'), 'content' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'), )); $mapping->setSource(array('enabled' => false)); $type->setMapping($mapping); $doc = new Elastica_Document(1000, array('email' => '*****@*****.**', 'content' => 'This is a sample post. Hello World Fuzzy Like This!')); $type->addDocument($doc); // Refresh index $index->refresh(); $fltQuery = new Elastica_Query_FuzzyLikeThis(); $fltQuery->setLikeText("sample gmail"); $fltQuery->addFields(array("email","content")); $fltQuery->setMinSimilarity(0.3); $fltQuery->setMaxQueryTerms(3); $resultSet = $type->search($fltQuery); $this->assertEquals(1, $resultSet->count()); }
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 testSearch() { $client = new Elastica_Client(); $index = new Elastica_Index($client, 'test'); $index->create(array(), true); $type = new Elastica_Type($index, 'helloworld'); $doc = new Elastica_Document(1, array('id' => 1, 'email' => '*****@*****.**', 'username' => 'hans', 'test' => array('2', '3', '5'))); $type->addDocument($doc); $doc = new Elastica_Document(2, array('id' => 2, 'email' => '*****@*****.**', 'username' => 'emil', 'test' => array('1', '3', '6'))); $type->addDocument($doc); $doc = new Elastica_Document(3, array('id' => 3, 'email' => '*****@*****.**', 'username' => 'ruth', 'test' => array('2', '3', '7'))); $type->addDocument($doc); // Refresh index $index->refresh(); $boolQuery = new Elastica_Query_Bool(); $termQuery1 = new Elastica_Query_Term(array('test' => '2')); $boolQuery->addMust($termQuery1); $resultSet = $type->search($boolQuery); $this->assertEquals(2, $resultSet->count()); $termQuery2 = new Elastica_Query_Term(array('test' => '5')); $boolQuery->addMust($termQuery2); $resultSet = $type->search($boolQuery); $this->assertEquals(1, $resultSet->count()); $termQuery3 = new Elastica_Query_Term(array('username' => 'hans')); $boolQuery->addMust($termQuery3); $resultSet = $type->search($boolQuery); $this->assertEquals(1, $resultSet->count()); $termQuery4 = new Elastica_Query_Term(array('username' => 'emil')); $boolQuery->addMust($termQuery4); $resultSet = $type->search($boolQuery); $this->assertEquals(0, $resultSet->count()); }
public function testSearch() { $client = new Elastica_Client(); $index = new Elastica_Index($client, 'test'); $index->create(array(), true); $index->getSettings()->setNumberOfReplicas(0); //$index->getSettings()->setNumberOfShards(1); $type = new Elastica_Type($index, 'helloworldmlt'); $mapping = new Elastica_Type_Mapping($type, array('email' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'), 'content' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'))); $mapping->setSource(array('enabled' => false)); $type->setMapping($mapping); $doc = new Elastica_Document(1000, array('email' => '*****@*****.**', 'content' => 'This is a sample post. Hello World Fuzzy Like This!')); $type->addDocument($doc); $doc = new Elastica_Document(1001, array('email' => '*****@*****.**', 'content' => 'This is a fake nospam email address for gmail')); $type->addDocument($doc); // Refresh index $index->refresh(); $mltQuery = new Elastica_Query_MoreLikeThis(); $mltQuery->setLikeText('fake gmail sample'); $mltQuery->setFields(array('email', 'content')); $mltQuery->setMaxQueryTerms(1); $mltQuery->setMinDocFrequency(1); $mltQuery->setMinTermFrequency(1); $query = new Elastica_Query(); $query->setFields(array('email', 'content')); $query->setQuery($mltQuery); $resultSet = $type->search($query); $resultSet->getResponse()->getData(); $this->assertEquals(2, $resultSet->count()); }
public function testSearch() { $client = new Elastica_Client(); $index = new Elastica_Index($client, 'test'); $index->create(array(), true); $index->getSettings()->setNumberOfReplicas(0); //$index->getSettings()->setNumberOfShards(1); $type = new Elastica_Type($index, 'helloworld'); $doc = new Elastica_Document(1, array('email' => '*****@*****.**', 'username' => 'hanswurst', 'test' => array('2', '3', '5'))); $type->addDocument($doc); // Refresh index $index->refresh(); $queryString = new Elastica_Query_QueryString('test*'); $resultSet = $type->search($queryString); $this->assertEquals(1, $resultSet->count()); }
public function testMatchDoc() { $index = $this->_createIndex(); $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 testQuery() { $client = new Elastica_Client(); $index = new Elastica_Index($client, 'test'); $index->create(array(), true); $type = new Elastica_Type($index, 'multi_match'); $doc = new Elastica_Document(1, array('id' => 1, 'name' => 'Rodolfo', 'last_name' => 'Moraes')); $type->addDocument($doc); // Refresh index $index->refresh(); $multiMatch = new Elastica_Query_MultiMatch(); $query = new Elastica_Query(); $multiMatch->setQuery('Rodolfo'); $multiMatch->setFields(array('name', 'last_name')); $query->setQuery($multiMatch); $resultSet = $index->search($query); $this->assertEquals(1, $resultSet->count()); $multiMatch->setQuery('Moraes'); $multiMatch->setFields(array('name', 'last_name')); $query->setQuery($multiMatch); $resultSet = $index->search($query); $this->assertEquals(1, $resultSet->count()); }