/**
  * @param MetaInformationInterface $meta
  *
  * @return Document
  */
 public function createDocument(MetaInformationInterface $meta)
 {
     $document = new Document();
     $document->setKey(MetaInformationInterface::DOCUMENT_KEY_FIELD_NAME, $meta->getDocumentKey());
     $document->setBoost($meta->getBoost());
     return $document;
 }
 /**
  * @param Query $query
  * @param callable $transform
  * @param null $readEndpoint
  * @param null $writeEndpoint
  * @param bool $commit
  */
 public function updateBufferedPlugin(Query $query, callable $transform = null, $readEndpoint = null, $writeEndpoint = null, $commit = true)
 {
     /**
      * @var PrefetchIterator $prefetch
      * @var BufferedAdd $update
      * @var SelectDocument $document
      */
     $prefetch = $this->getSolarium()->getPlugin('prefetchiterator');
     $prefetch->setPrefetch($this->getBuffer());
     $prefetch->setQuery($query);
     $prefetch->setEndpoint($readEndpoint);
     $update = $this->solarium->getPlugin('bufferedadd');
     $update->setBufferSize($this->getBuffer());
     $update->setEndpoint($writeEndpoint);
     foreach ($prefetch as $document) {
         if (is_callable($transform)) {
             $document = $transform($document);
         } else {
             $document = new UpdateDocument($document->getFields());
         }
         $update->addDocument($document);
     }
     $update->flush();
     if ($commit === true) {
         $update->commit();
     }
 }
 /**
  * @param MetaInformationInterface $meta
  *
  * @return Document
  */
 public function createDocument(MetaInformationInterface $meta)
 {
     $document = new Document();
     $document->addField('id', $meta->getEntityId());
     $document->addField('document_name_s', $meta->getDocumentName());
     $document->setBoost($meta->getBoost());
     return $document;
 }
 /**
  * @group query1
  */
 public function testGetQuery_SearchInAllFields()
 {
     $document = new Document();
     $document->addField('document_name_s', 'validtestentity');
     $query = new FindByDocumentNameQuery();
     $query->setDocument($document);
     $queryString = $query->getQuery();
     $this->assertEquals('document_name_s:validtestentity', $queryString, 'filter query');
 }
 public function testGetQuery_SearchInAllFields()
 {
     $document = new Document();
     $document->setKey('id', 'validtestentity_1');
     $expectedQuery = 'id:validtestentity_1';
     $query = new FindByIdentifierQuery();
     $query->setDocumentKey('validtestentity_1');
     $query->setDocument($document);
     $queryString = $query->getQuery();
     $this->assertEquals($expectedQuery, $queryString);
 }
 public function testGetQuery_DocumentNameMissing()
 {
     $document = new Document();
     $document->addField('id', '1');
     $query = new FindByIdentifierQuery();
     $query->setDocument($document);
     try {
         $query->getQuery();
         $this->fail('an exception should be thrown');
     } catch (\RuntimeException $e) {
         $this->assertEquals('documentName should not be null', $e->getMessage());
     }
 }
Example #7
0
 public function testNoEscapeAddField()
 {
     $this->doc->setFilterControlCharacters(false);
     $this->doc->setField('foo', $value1 = 'bar' . chr(15));
     $this->doc->addField('foo', $value2 = 'bar' . chr(15) . chr(8));
     $this->assertEquals(array($value1, $value2), $this->doc->foo);
 }
 /**
  * @param array $fields
  * @return Document
  */
 public static function createDocumentFromArray($fields = [])
 {
     if (empty($fields)) {
         return null;
     }
     $document = new Document();
     foreach ($fields as $field => $value) {
         if ($field == "id") {
             $document->addField($field, $value);
         } elseif (!is_array($value)) {
             $document->addField($field . "_s", strtolower($value));
         } else {
             $document->addField($field, $value);
         }
     }
     return $document;
 }
 public function testSetAndGetVersion()
 {
     $this->assertEquals(null, $this->doc->getVersion());
     $this->doc->setVersion(Document::VERSION_MUST_NOT_EXIST);
     $this->assertEquals(Document::VERSION_MUST_NOT_EXIST, $this->doc->getVersion());
     $this->doc->setVersion(234);
     $this->assertEquals(234, $this->doc->getVersion());
 }
Example #10
0
 public function testFindAll()
 {
     $document = new Document();
     $document->addField('id', 2);
     $document->addField('document_name_s', 'post');
     $metaFactory = $this->getMock('FS\\SolrBundle\\Doctrine\\Mapper\\MetaInformationFactory', array(), array(), '', false);
     $metaFactory->expects($this->once())->method('loadInformation')->will($this->returnValue(MetaTestInformationFactory::getMetaInformation()));
     $mapper = $this->getMock('FS\\SolrBundle\\Doctrine\\Mapper\\EntityMapper', array(), array(), '', false);
     $mapper->expects($this->once())->method('toDocument')->will($this->returnValue($document));
     $solr = $this->getMock('FS\\SolrBundle\\Solr', array(), array(), '', false);
     $solr->expects($this->exactly(2))->method('getMapper')->will($this->returnValue($mapper));
     $solr->expects($this->once())->method('getCommandFactory')->will($this->returnValue(CommandFactoryStub::getFactoryWithAllMappingCommand()));
     $solr->expects($this->once())->method('getMetaFactory')->will($this->returnValue($metaFactory));
     $entity = new ValidTestEntity();
     $solr->expects($this->once())->method('query')->will($this->returnValue(array($entity)));
     $repo = new Repository($solr, $entity);
     $actual = $repo->findAll();
     $this->assertTrue(is_array($actual));
     $this->assertNull($document->id, 'id was removed');
 }
Example #11
0
 protected function assertFieldCount($expectedCount, Document $document, $message = '')
 {
     $this->assertEquals($expectedCount + self::FIELDS_ALWAYS_MAPPED, $document->count(), $message);
 }
Example #12
0
 public function testBuildAddXmlWithFieldModifierAndNullValue()
 {
     $doc = new Document();
     $doc->setKey('employeeId', '05991');
     $doc->addField('skills', null, null, Document::MODIFIER_SET);
     $command = new AddCommand();
     $command->addDocument($doc);
     $this->assertEquals('<add>' . '<doc>' . '<field name="employeeId">05991</field>' . '<field name="skills" update="set" null="true"></field>' . '</doc>' . '</add>', $this->builder->buildAddXml($command));
 }
 /**
  * @test
  */
 public function adapterPrepareUpdateReturnsSolariumDocument()
 {
     $object = new SolariumUpdateDocument();
     $object->setField('id', 5);
     $object->setField('type', $this->type);
     /* @var $preparedObject SolariumUpdateDocument */
     $preparedObject = $this->adapter->prepareUpdate($object);
     $this->assertInstanceOf(SolariumUpdateDocument::class, $preparedObject);
     $this->assertEquals(5, $preparedObject->offsetGet('id'));
     $this->assertEquals($this->type, $preparedObject->offsetGet('type'));
 }
 public function testBuildAddXmlWithVersionedDocument()
 {
     $doc = new Document(array('id' => 1));
     $doc->setVersion(Document::VERSION_MUST_NOT_EXIST);
     $command = new AddCommand();
     $command->addDocument($doc);
     $this->assertEquals('<add><doc><field name="id">1</field><field name="_version_">-1</field></doc></add>', $this->builder->buildAddXml($command));
 }
Example #15
0
 public function testQuery_OneDocumentFound()
 {
     $arrayObj = new SolrDocumentStub(array('title_s' => 'title'));
     $document = new Document();
     $document->addField('document_name_s', 'name');
     $query = new FindByDocumentNameQuery();
     $query->setDocumentName('name');
     $query->setDocument($document);
     $query->setEntity(new ValidTestEntity());
     $query->setIndex('index0');
     $this->assertQueryWasExecuted(array($arrayObj), 'index0');
     $solr = new Solr($this->solrClientFake, $this->commandFactory, $this->eventDispatcher, $this->metaFactory, $this->mapper);
     $entities = $solr->query($query);
     $this->assertEquals(1, count($entities));
 }
 /**
  * Helper method for indexing.
  *
  * Adds $value with field name $key to the document $doc. The format of $value
  * is the same as specified in
  * \Drupal\search_api\Backend\BackendSpecificInterface::indexItems().
  */
 protected function addIndexField(Document $doc, $key, $key_single, $values, $type)
 {
     // Don't index empty values (i.e., when field is missing).
     if (!isset($values)) {
         return;
     }
     // All fields.
     foreach ($values as $value) {
         switch ($type) {
             case 'boolean':
                 $value = $value ? 'true' : 'false';
                 break;
             case 'date':
                 $value = is_numeric($value) ? (int) $value : strtotime($value);
                 if ($value === FALSE) {
                     return;
                 }
                 $value = format_date($value, 'custom', self::SOLR_DATE_FORMAT, 'UTC');
                 break;
             case 'integer':
                 $value = (int) $value;
                 break;
             case 'decimal':
                 $value = (double) $value;
                 break;
         }
         // For tokenized text, add each word separately.
         if ($type == 'tokenized_text' && is_array($value)) {
             foreach ($value as $tokenizd_value) {
                 // @todo Score is tracked by key, not for each value, how to handle
                 //   this?
                 $doc->addField($key, $tokenizd_value['value'], $tokenizd_value['score']);
             }
         } else {
             $doc->addField($key, $value);
         }
     }
     $field_value = $doc->{$key};
     $first_value = is_array($field_value) ? reset($field_value) : $field_value;
     if ($type == 'tokenized_text' && is_array($first_value) && isset($first_value['value'])) {
         $first_value = $first_value['value'];
     }
     $doc->setField($key_single, $first_value);
 }