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); $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 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 testParentMapping() { $index = $this->_createIndex(); $parenttype = new Elastica_Type($index, 'parenttype'); $parentmapping = new Elastica_Type_Mapping($parenttype, array('name' => array('type' => 'string', 'store' => 'yes'))); $parenttype->setMapping($parentmapping); $childtype = new Elastica_Type($index, 'childtype'); $childmapping = new Elastica_Type_Mapping($childtype, array('name' => array('type' => 'string', 'store' => 'yes'))); $childmapping->setParam('_parent', array('type' => 'parenttype')); $childtype->setMapping($childmapping); }
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, '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 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()); }
public function testAddDocumentVersion() { $client = new Elastica_Client(); $index = $client->getIndex('test'); $index->create(array(), true); $type = new Elastica_Type($index, 'test'); $doc1 = new Elastica_Document(1); $doc1->add('title', 'Hello world'); $return = $type->addDocument($doc1); $data = $return->getData(); $this->assertEquals(1, $data['_version']); $return = $type->addDocument($doc1); $data = $return->getData(); $this->assertEquals(2, $data['_version']); }
public static function createType($handle) { self::init(); $local_type = self::getTypeByHandle($handle); $mapping = json_decode($local_type->mapping_json, TRUE); $type = new Elastica_Type(self::getIndex(), $handle); $type_mapping = new Elastica_Type_Mapping($type); foreach ($mapping[$handle] as $key => $value) { $type_mapping->setParam($key, $value); } $type->setMapping($type_mapping); self::getIndex()->refresh(); }
public static function deleteDocument($id, Elastica_Type $type) { return $type->deleteById($id); }
public function testMoreLikeThisApi() { $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); $type = new Elastica_Type($index, 'mlt_test'); $type->addDocument(new Elastica_Document(1, array('visible' => true, 'name' => 'bruce wayne batman'))); $type->addDocument(new Elastica_Document(2, array('visible' => true, 'name' => 'bruce wayne'))); $type->addDocument(new Elastica_Document(3, array('visible' => false, 'name' => 'bruce wayne'))); $type->addDocument(new Elastica_Document(4, array('visible' => true, 'name' => 'batman'))); $type->addDocument(new Elastica_Document(5, array('visible' => false, 'name' => 'batman'))); $type->addDocument(new Elastica_Document(6, array('visible' => true, 'name' => 'superman'))); $type->addDocument(new Elastica_Document(7, array('visible' => true, 'name' => 'spiderman'))); $index->refresh(); $document = $type->getDocument(1); // Return all similar $resultSet = $type->moreLikeThis($document, array('min_term_freq' => '1', 'min_doc_freq' => '1')); $this->assertEquals(4, $resultSet->count()); // Return just the visible similar $query = new Elastica_Query(); $filterTerm = new Elastica_Filter_Term(); $filterTerm->setTerm('visible', true); $query->setFilter($filterTerm); $resultSet = $type->moreLikeThis($document, array('min_term_freq' => '1', 'min_doc_freq' => '1'), $query); $this->assertEquals(2, $resultSet->count()); }
/** * Index a associative array. * * @param \Elastica_Type $type * @param int $id ID of document * @param array $data Array with data of document */ protected function index(\Elastica_Type $type, $id, $data, $parent = false) { // create document $document = new \Elastica_Document($id, $data); if ($parent) { $document->setParent($parent); } // save to index $type->addDocument($document); }
public function testDeleteByQuery() { $index = $this->_createIndex(); $type = new Elastica_Type($index, 'test'); $type->addDocument(new Elastica_Document(1, array('name' => 'ruflin nicolas'))); $type->addDocument(new Elastica_Document(2, array('name' => 'ruflin'))); $index->refresh(); $response = $index->search('ruflin*'); $this->assertEquals(2, $response->count()); $response = $index->search('nicolas'); $this->assertEquals(1, $response->count()); // Delete first document $response = $type->deleteByQuery('nicolas'); $this->assertTrue($response->isOk()); $index->refresh(); // Makes sure, document is deleted $response = $index->search('ruflin*'); $this->assertEquals(1, $response->count()); $response = $index->search('nicolas'); $this->assertEquals(0, $response->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()); }