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 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 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); $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()); }
/** * Match a document to percolator queries * * @param Elastica_Document $doc * @param string|Elastica_Query|Elastica_Query_Abstract $query Not implemented yet * @return Elastica_Response */ public function matchDoc(Elastica_Document $doc, $query = null) { $path = $this->_index->getName() . '/type/_percolate'; $data = array('doc' => $doc->getData()); $response = $this->getIndex()->getClient()->request($path, Elastica_Request::GET, $data); $data = $response->getData(); return $data['matches']; }
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, '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()); }
/** * Index a SQL query result. * * @param string $type Object type for the index * @param string $sql SQL query * @param function|array $callback */ protected function sql($type, $sql, $mapping = false, $callback = false) { $statement = db_query($sql); // get type object from index $type = $this->index->getType($type); if ($mapping) { $type->setMapping($mapping); } // fetch as associative array $result = $statement->fetchAll(\PDO::FETCH_ASSOC); $i = 0; $total = count($result); foreach ($result as $data) { $data = self::convert($data); // array or function callback if (is_array($callback)) { foreach ($callback as $key => $sql) { $data[$key] = array(); // fetch a single column for additional data $result = db_query($sql, array('id' => $data['id'])); $result->setFetchMode(\PDO::FETCH_ASSOC); foreach ($result as $object) { $object = self::convert($object); $data[$key][] = $object[$key]; } } } else { if ($callback) { $data = $callback($data); } } $this->index($type, $data['id'], $data, isset($data['parent']) ? $data['parent'] : false); echo " Indexing {$type->getName()}... " . ceil(++$i / $total * 100) . "%\r"; } echo "\n"; }
/** * @return \Elastica_Type */ public function getType() { return $this->index->getType($this->type); }
public function testSetReadOnly() { $client = new Elastica_Client(); $index = new Elastica_Index($client, 'elastica_test'); $index->getSettings()->setReadOnly(false); $index = $this->_createIndex(); // Add document to normal index $doc = new Elastica_Document(null, array('hello' => 'world')); $type = $index->getType('test'); $type->addDocument($doc); $this->assertFalse((bool) $index->getSettings()->get('blocks.read_only')); // Try to add doc to read only index $index->getSettings()->setReadOnly(true); $this->assertTrue((bool) $index->getSettings()->get('blocks.read_only')); try { $type->addDocument($doc); $this->fail('Should throw exception because of read only'); } catch(Elastica_Exception_Response $e) { $message = $e->getMessage(); $this->assertContains('ClusterBlockException', $message); $this->assertContains('index read-only', $message); } // Remove read only, add document $response = $index->getSettings()->setReadOnly(false); $this->assertTrue($response->isOk()); $type->addDocument($doc); $index->refresh(); $this->assertEquals(2, $type->count()); }
public function testAddWordxFile() { $indexMapping = array('file' => array('type' => 'attachment'), 'text' => array('type' => 'string', 'store' => 'no')); $indexParams = array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)); $client = new Elastica_Client(); $index = new Elastica_Index($client, 'content'); $type = new Elastica_Type($index, 'content'); $index->create($indexParams, true); $type->setMapping($indexMapping); $doc1 = new Elastica_Document(1); $doc1->addFile('file', BASE_PATH . '/data/test.docx'); $doc1->add('text', 'basel world'); $type->addDocument($doc1); $doc2 = new Elastica_Document(2); $doc2->add('text', 'running in basel'); $type->addDocument($doc2); $index->optimize(); $resultSet = $type->search('xodoa'); $this->assertEquals(1, $resultSet->count()); $resultSet = $type->search('basel'); $this->assertEquals(2, $resultSet->count()); $resultSet = $type->search('ruflin'); $this->assertEquals(0, $resultSet->count()); }