/**
  * @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;
 }
 public function testAddFieldWithModifier()
 {
     $this->doc->clear();
     $this->doc->setKey('id', 1);
     $this->doc->addField('myfield', 'myvalue', null, Document::MODIFIER_ADD);
     $this->doc->addField('myfield', 'myvalue2', null, Document::MODIFIER_ADD);
     $this->assertEquals(array('id' => 1, 'myfield' => array('myvalue', 'myvalue2')), $this->doc->getFields());
     $this->assertEquals(Document::MODIFIER_ADD, $this->doc->getFieldModifier('myfield'));
 }
Example #3
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;
 }
 /**
  * @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');
 }
Example #6
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');
 }
 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 #8
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));
 }
 public function testBuildAddXmlWithFieldModifiersAndMultivalueFields()
 {
     $doc = new Document();
     $doc->setKey('id', 1);
     $doc->addField('category', 123, null, Document::MODIFIER_ADD);
     $doc->addField('category', 234, null, Document::MODIFIER_ADD);
     $doc->addField('name', 'test', 2.3, Document::MODIFIER_SET);
     $doc->setField('stock', 2, null, Document::MODIFIER_INC);
     $command = new AddCommand();
     $command->addDocument($doc);
     $this->assertEquals('<add><doc><field name="id">1</field><field name="category" update="add">123</field><field name="category" update="add">234</field><field name="name" boost="2.3" update="set">test</field><field name="stock" update="inc">2</field></doc></add>', $this->builder->buildAddXml($command));
 }
Example #10
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);
 }