addDocument() public method

Adds the given document to the search index.
public addDocument ( Document $doc ) : Response
$doc Document Document with data
return Response
 public function testSearch()
 {
     $client = $this->_getClient();
     $index = new Index($client, 'test');
     $index->create(array(), true);
     $type = new Type($index, 'helloworld');
     $doc = new Document(1, array('id' => 1, 'email' => '*****@*****.**', 'username' => 'hans', 'test' => array('2', '3', '5')));
     $type->addDocument($doc);
     $doc = new Document(2, array('id' => 2, 'email' => '*****@*****.**', 'username' => 'emil', 'test' => array('1', '3', '6')));
     $type->addDocument($doc);
     $doc = new Document(3, array('id' => 3, 'email' => '*****@*****.**', 'username' => 'ruth', 'test' => array('2', '3', '7')));
     $type->addDocument($doc);
     // Refresh index
     $index->refresh();
     $boolQuery = new Bool();
     $termQuery1 = new Term(array('test' => '2'));
     $boolQuery->addMust($termQuery1);
     $resultSet = $type->search($boolQuery);
     $this->assertEquals(2, $resultSet->count());
     $termQuery2 = new Term(array('test' => '5'));
     $boolQuery->addMust($termQuery2);
     $resultSet = $type->search($boolQuery);
     $this->assertEquals(1, $resultSet->count());
     $termQuery3 = new Term(array('username' => 'hans'));
     $boolQuery->addMust($termQuery3);
     $resultSet = $type->search($boolQuery);
     $this->assertEquals(1, $resultSet->count());
     $termQuery4 = new Term(array('username' => 'emil'));
     $boolQuery->addMust($termQuery4);
     $resultSet = $type->search($boolQuery);
     $this->assertEquals(0, $resultSet->count());
 }
 /**
  * @group functional
  */
 public function testSearch()
 {
     $client = $this->_getClient();
     $index = new Index($client, 'test');
     $index->create(array(), true);
     $index->getSettings()->setNumberOfReplicas(0);
     //$index->getSettings()->setNumberOfShards(1);
     $type = new Type($index, 'helloworldmlt');
     $mapping = new 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 Document(1000, array('email' => '*****@*****.**', 'content' => 'This is a sample post. Hello World Fuzzy Like This!'));
     $type->addDocument($doc);
     $doc = new Document(1001, array('email' => '*****@*****.**', 'content' => 'This is a fake nospam email address for gmail'));
     $type->addDocument($doc);
     // Refresh index
     $index->refresh();
     $mltQuery = new MoreLikeThis();
     $mltQuery->setLike('fake gmail sample');
     $mltQuery->setFields(array('email', 'content'));
     $mltQuery->setMaxQueryTerms(3);
     $mltQuery->setMinDocFrequency(1);
     $mltQuery->setMinTermFrequency(1);
     $query = new Query();
     $query->setQuery($mltQuery);
     $resultSet = $type->search($query);
     $resultSet->getResponse()->getData();
     $this->assertEquals(2, $resultSet->count());
 }
 public function testQuery()
 {
     $client = $this->_getClient();
     $index = new Index($client, 'test');
     $index->create(array(), true);
     $type = new Type($index, 'constant_score');
     $doc = new Document(1, array('id' => 1, 'email' => '*****@*****.**', 'username' => 'hans'));
     $type->addDocument($doc);
     $doc = new Document(2, array('id' => 2, 'email' => '*****@*****.**', 'username' => 'emil'));
     $type->addDocument($doc);
     $doc = new Document(3, array('id' => 3, 'email' => '*****@*****.**', 'username' => 'ruth'));
     $type->addDocument($doc);
     // Refresh index
     $index->refresh();
     $boost = 1.3;
     $query_match = new MatchAll();
     $query = new ConstantScore();
     $query->setQuery($query_match);
     $query->setBoost($boost);
     $expectedArray = array('constant_score' => array('query' => $query_match->toArray(), 'boost' => $boost));
     $this->assertEquals($expectedArray, $query->toArray());
     $resultSet = $type->search($query);
     $results = $resultSet->getResults();
     $this->assertEquals($resultSet->count(), 3);
     $this->assertEquals($results[1]->getScore(), 1);
 }
 /**
  * @param $status
  * @param null $dateString
  * @return $this
  */
 public function createStatusLog($status, $dateString = null)
 {
     $date = $dateString ? new \DateTime($dateString) : new \DateTime('05/05/2016');
     $statusLog = new StatusLog();
     $statusLog->setId(uniqid());
     $statusLog->setStatus($status);
     $statusLog->setDate($date);
     $this->type->addDocument(new \Elastica\Document("", $statusLog->toArray()));
     return $this;
 }
 protected function setUp()
 {
     parent::setUp();
     $this->index = $this->_createIndex('test_functionscore');
     $this->type = $this->index->getType('test');
     $this->type->setMapping(array('name' => array('type' => 'string', 'index' => 'not_analyzed'), 'location' => array('type' => 'geo_point'), 'price' => array('type' => 'float')));
     $this->type->addDocument(new Document(1, array('name' => "Mr. Frostie's", 'location' => array('lat' => 32.799605, 'lon' => -117.243027), 'price' => 4.5)));
     $this->type->addDocument(new Document(2, array('name' => "Miller's Field", 'location' => array('lat' => 32.795964, 'lon' => -117.255028), 'price' => 9.5)));
     $this->index->refresh();
 }
 protected function setUp()
 {
     parent::setUp();
     $this->index = $this->_createIndex('test_boostingquery');
     $this->type = $this->index->getType('test');
     $this->type->setMapping(array('name' => array('type' => 'string', 'index' => 'analyzed'), 'price' => array('type' => 'float')));
     $this->sampleData = array(array("name" => "Vital Lama", "price" => 5.2), array("name" => "Vital Match", "price" => 2.1), array("name" => "Mercury Vital", "price" => 7.5), array("name" => "Fist Mercury", "price" => 3.8), array("name" => "Lama Vital 2nd", "price" => 3.2));
     foreach ($this->sampleData as $key => $value) {
         $this->type->addDocument(new Document($key, $value));
     }
     $this->index->refresh();
 }
Example #7
0
 /**
  * @param Hotel $hotel
  *
  * @return $this
  */
 public function addHotel($hotel)
 {
     $data = array('id' => $hotel->getId());
     $document = $this->hotelType->createDocument($hotel->getId());
     $document->setData($data);
     $this->hotelType->addDocument($document);
     $this->updateHotel($hotel, $document);
     return $this;
 }
 /**
  * @test
  */
 public function createNewDocumentOnSave()
 {
     $id = 1;
     $data = 'foobar';
     $this->prophesizeGetDocumentThrowsException($id);
     $this->index->refresh()->shouldBeCalled();
     $doc = $this->getDocument($id, $data);
     $this->type->createDocument($this->getCacheId($id), [Cache::VALUE_FIELD => serialize($data)])->shouldBeCalled()->willReturn($doc);
     $this->type->addDocument($doc)->shouldBeCalled();
     self::assertTrue($this->cache->save($id, $data));
 }
 /**
  * @group functional
  */
 public function testSearch()
 {
     $index = $this->_createIndex();
     $index->getSettings()->setNumberOfReplicas(0);
     $type = new Type($index, 'helloworld');
     $doc = new Document(1, array('email' => '*****@*****.**', 'username' => 'test 7/6 123', 'test' => array('2', '3', '5')));
     $type->addDocument($doc);
     // Refresh index
     $index->refresh();
     $queryString = new QueryString(Util::escapeTerm('test 7/6'));
     $resultSet = $type->search($queryString);
     $this->assertEquals(1, $resultSet->count());
 }
 public function testSearch()
 {
     $client = $this->_getClient();
     $index = new Index($client, 'test');
     $index->create(array(), true);
     $index->getSettings()->setNumberOfReplicas(0);
     //$index->getSettings()->setNumberOfShards(1);
     $type = new Type($index, 'helloworld');
     $doc = new Document(1, array('email' => '*****@*****.**', 'username' => 'hanswurst', 'test' => array('2', '3', '5')));
     $type->addDocument($doc);
     // Refresh index
     $index->refresh();
     $queryString = new QueryString('test*');
     $resultSet = $type->search($queryString);
     $this->assertEquals(1, $resultSet->count());
 }
Example #11
0
 public function testQuery()
 {
     $client = $this->_getClient();
     $index = new Index($client, 'test');
     $index->create(array(), true);
     $type = new Type($index, 'multi_match');
     $doc = new Document(1, array('id' => 1, 'name' => 'Rodolfo', 'last_name' => 'Moraes'));
     $type->addDocument($doc);
     // Refresh index
     $index->refresh();
     $multiMatch = new MultiMatch();
     $query = new 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());
 }
Example #12
0
 public function testSearch()
 {
     $client = $this->_getClient();
     $index = new Index($client, 'test');
     $index->create(array(), true);
     $index->getSettings()->setNumberOfReplicas(0);
     //$index->getSettings()->setNumberOfShards(1);
     $type = new Type($index, 'helloworldfuzzy');
     $mapping = new 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 Document(1000, array('email' => '*****@*****.**', 'content' => 'This is a sample post. Hello World Fuzzy Like This!'));
     $type->addDocument($doc);
     // Refresh index
     $index->refresh();
     $fltQuery = new 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());
 }
Example #13
0
 public function testAddDocumentVersion()
 {
     $client = $this->_getClient();
     $index = $client->getIndex('test');
     $index->create(array(), true);
     $type = new Type($index, 'test');
     $doc1 = new Document(1);
     $doc1->set('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']);
 }
 /**
  * @param array $document
  * @param string $type
  * @throws \Exception
  */
 protected function addDocumentToElastic(array $document, $type = 'issue')
 {
     switch ($type) {
         case 'issue':
             if (isset($document['custom_fields'])) {
                 foreach ($document['custom_fields'] as $customField) {
                     switch ($customField['id']) {
                         case 4:
                             $document['typo3_version'] = isset($customField['value']) ? $customField['value'] : '-';
                             break;
                         case 5:
                             $document['php_version'] = isset($customField['value']) ? $customField['value'] : '-';
                             break;
                         case 8:
                             $document['complexity'] = isset($customField['value']) ? $customField['value'] : '-';
                             break;
                         case 15:
                             $document['isregression'] = isset($customField['value']) ? true : false;
                             break;
                         case 18:
                             $document['focus']['name'] = isset($customField['value']) ? $customField['value'] : '-';
                             break;
                         default:
                     }
                 }
             }
             $document['updated_on'] = $this->fixDateFormat($document['updated_on']);
             $document['created_on'] = $this->fixDateFormat($document['created_on']);
             $type = new Elastica\Type($this->elasticIndex, 'issue');
             try {
                 $testIfDocExists = $type->getDocument($document['id']);
             } catch (\Exception $e) {
                 $message = new \WMDB\Forger\Utilities\Slack\Message();
                 $message->sendMessage($document);
             }
             break;
         case 'review':
             $type = new Elastica\Type($this->elasticIndex, 'review');
             break;
         case 'user':
             $type = new Elastica\Type($this->elasticIndex, 'user');
             break;
         default:
     }
     #\TYPO3\Flow\var_dump($type);
     $doc = new Elastica\Document($document['id'], $document);
     #\TYPO3\Flow\var_dump($doc);
     $type->addDocument($doc);
     #sleep(1);
 }
Example #15
0
 /**
  * @group functional
  */
 public function testOldObject()
 {
     if (version_compare(phpversion(), 7, '>=')) {
         self::markTestSkipped('These objects are not supported in PHP 7');
     }
     $index = $this->_createIndex();
     $type = new Type($index, 'test');
     $docNumber = 3;
     for ($i = 0; $i < $docNumber; ++$i) {
         $doc = new Document($i, array('email' => '*****@*****.**'));
         $type->addDocument($doc);
     }
     $index->refresh();
     $this->hideDeprecated();
     $boolQuery = new \Elastica\Query\Bool();
     $this->showDeprecated();
     $resultSet = $type->search($boolQuery);
     $this->assertEquals($resultSet->count(), $docNumber);
 }
Example #16
0
 /**
  * @group functional
  */
 public function testExists()
 {
     $index = $this->_createIndex();
     $this->assertTrue($index->exists());
     $type = new Type($index, 'user');
     $this->assertFalse($type->exists());
     $type->addDocument(new Document(1, array('name' => 'test name')));
     $index->optimize();
     // sleep a moment to be sure that all nodes in cluster has new type
     sleep(5);
     //Test if type exists
     $this->assertTrue($type->exists());
     $index->delete();
     $this->assertFalse($index->exists());
 }
Example #17
0
 public function testMoreLikeThisApi()
 {
     $client = new 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 Type($index, 'mlt_test');
     $type->addDocument(new Document(1, array('visible' => true, 'name' => 'bruce wayne batman')));
     $type->addDocument(new Document(2, array('visible' => true, 'name' => 'bruce wayne')));
     $type->addDocument(new Document(3, array('visible' => false, 'name' => 'bruce wayne')));
     $type->addDocument(new Document(4, array('visible' => true, 'name' => 'batman')));
     $type->addDocument(new Document(5, array('visible' => false, 'name' => 'batman')));
     $type->addDocument(new Document(6, array('visible' => true, 'name' => 'superman')));
     $type->addDocument(new 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 Query();
     $filterTerm = new 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());
 }
Example #18
0
 /**
  * @group functional
  */
 public function testOldObject()
 {
     if (version_compare(phpversion(), 7, '>=')) {
         self::markTestSkipped('These objects are not supported in PHP 7');
     }
     $index = $this->_createIndex();
     $type = new Type($index, 'test');
     $docNumber = 3;
     for ($i = 0; $i < $docNumber; ++$i) {
         $doc = new Document($i, array('email' => '*****@*****.**'));
         $type->addDocument($doc);
     }
     $index->refresh();
     $err = array();
     set_error_handler(function () use(&$err) {
         $err[] = func_get_args();
     });
     $boolQuery = new \Elastica\Query\Bool();
     restore_error_handler();
     $this->assertCount(1, $err);
     $this->assertEquals(E_USER_DEPRECATED, $err[0][0]);
     $resultSet = $type->search($boolQuery);
     $this->assertEquals($resultSet->count(), $docNumber);
 }
Example #19
0
 public function testExists()
 {
     $index = $this->_createIndex();
     $this->assertTrue($index->exists());
     $type = new Type($index, 'user');
     $this->assertFalse($type->exists());
     $type->addDocument(new Document(1, array('name' => 'test name')));
     $index->optimize();
     //Test if type exists
     $this->assertTrue($type->exists());
     $index->delete();
     $this->assertFalse($index->exists());
 }
 public function testSearchSetAnalyzer()
 {
     $client = $this->_getClient();
     $index = new Index($client, 'test');
     $index->create(array('analysis' => array('analyzer' => array('searchAnalyzer' => array('type' => 'custom', 'tokenizer' => 'standard', 'filter' => array('myStopWords'))), 'filter' => array('myStopWords' => array('type' => 'stop', 'stopwords' => array('The'))))), true);
     $index->getSettings()->setNumberOfReplicas(0);
     //$index->getSettings()->setNumberOfShards(1);
     $type = new Type($index, 'helloworldfuzzy');
     $mapping = new 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 Document(1000, array('email' => '*****@*****.**', 'content' => 'The Fuzzy Test!'));
     $type->addDocument($doc);
     $doc = new Document(1001, array('email' => '*****@*****.**', 'content' => 'Elastica Fuzzy Test'));
     $type->addDocument($doc);
     // Refresh index
     $index->refresh();
     $fltQuery = new FuzzyLikeThis();
     $fltQuery->addFields(array("email", "content"));
     $fltQuery->setLikeText("The");
     $fltQuery->setMinSimilarity(0.1);
     $fltQuery->setMaxQueryTerms(3);
     // Test before analyzer applied, should return 1 result
     $resultSet = $type->search($fltQuery);
     $this->assertEquals(1, $resultSet->count());
     $fltQuery->setParam('analyzer', 'searchAnalyzer');
     $resultSet = $type->search($fltQuery);
     $this->assertEquals(0, $resultSet->count());
 }
Example #21
0
 /**
  * @group functional
  */
 public function testNoSource()
 {
     $index = $this->_createIndex();
     $type = new Type($index, 'user');
     // Adds 1 document to the index
     $doc1 = new Document(1, array('username' => 'ruflin', 'test' => array('2', '3', '5')));
     $type->addDocument($doc1);
     // To update index
     $index->refresh();
     $query = Query::create('ruflin');
     $resultSet = $type->search($query);
     // Disable source
     $query->setSource(false);
     $resultSetNoSource = $type->search($query);
     $this->assertEquals(1, $resultSet->count());
     $this->assertEquals(1, $resultSetNoSource->count());
     // Tests if no source is in response except id
     $result = $resultSetNoSource->current();
     $this->assertEquals(1, $result->getId());
     $this->assertEmpty($result->getData());
     // Tests if source is in response except id
     $result = $resultSet->current();
     $this->assertEquals(1, $result->getId());
     $this->assertNotEmpty($result->getData());
 }
Example #22
0
 /**
  * Construit les données afin que celles-ci soient indexées (avec les fields corrects)
  * Méthode non testée et non utilisée pour le moment (Préférable de la tester avant..)
  *
  * @param CMbObject     $datum the datum you want to construct
  * @param Elastica\Type $type  the type where you want to index the data
  *
  * @return array
  */
 function indexingDatum($datum, $type)
 {
     $datum_to_index = $this->constructDatum($datum);
     $document = $type->createDocument($datum['object_id'], $datum_to_index);
     switch ($datum['type']) {
         case 'create':
             $type->addDocument($document);
             break;
         case 'store':
             $type->updateDocument($document);
             break;
         case 'delete':
             $type->deleteDocument($document);
             break;
         case 'merge':
             //nothing to do
             /*supprimer un des deux et faire un update de l'autre.*/
             break;
         default:
             return false;
     }
     $type->getIndex()->refresh();
     $this->deleteDatumTemporaryTable($datum['search_indexing_id']);
     return true;
 }