Skip to content

Commit

Permalink
BUG Add missing addStoredFields method
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian Mooyman committed Feb 16, 2015
1 parent 79eb663 commit 620d786
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
28 changes: 25 additions & 3 deletions code/solr/SolrIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function addAnalyzer($field, $type, $params) {

function getFieldDefinitions() {
$xml = array();
$stored = Director::isDev() ? "stored='true'" : "stored='false'";
$stored = $this->getStoredDefault();

$xml[] = "";

Expand All @@ -95,7 +95,7 @@ function getFieldDefinitions() {

// Add the fulltext collation field

$xml[] = "<field name='_text' type='htmltext' indexed='true' $stored multiValued='true' />" ;
$xml[] = "<field name='_text' type='htmltext' indexed='true' stored='$stored' multiValued='true' />" ;

// Add the user-specified fields

Expand Down Expand Up @@ -143,6 +143,28 @@ protected function getSuggestionQueryString($collation = '') {
return str_replace(' ', '+', $this->getNiceSuggestion($collation));
}

/**
* Add a field that should be stored
*
* @param string $field The field to add
* @param string $forceType The type to force this field as (required in some cases, when not
* detectable from metadata)
* @param array $extraOptions Dependent on search implementation
*/
public function addStoredField($field, $forceType = null, $extraOptions = array()) {
$options = array_merge($extraOptions, array('stored' => 'true'));
$this->addFulltextField($field, $forceType, $options);
}

/**
* Gets the default 'stored' value for fields in this index
*
* @return string A default value for the 'stored' field option, either 'true' or 'false'
*/
protected function getStoredDefault() {
return Director::isDev() ? 'true' : 'false';
}

/**
* @param String $name
* @param Array $spec
Expand All @@ -166,7 +188,7 @@ protected function getFieldDefinition($name, $spec, $typeMap = null) {
'name' => $name,
'type' => $type,
'indexed' => 'true',
'stored' => Director::isDev() ? 'true' : 'false',
'stored' => $this->getStoredDefault(),
'multiValued' => $multiValued
),
isset($spec['extra_options']) ? $spec['extra_options'] : array()
Expand Down
50 changes: 50 additions & 0 deletions tests/SolrIndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,39 @@ function testAddCopyField() {
$this->assertEquals('destField', $copyField[0]['dest']);
}

/**
* Tests the setting of the 'stored' flag
*/
public function testStoredFields() {
// Test two fields
$index = new SolrIndexTest_FakeIndex2();
$index->addStoredField('Field1');
$index->addFulltextField('Field2');
$schema = $index->getFieldDefinitions();
$this->assertContains(
"<field name='SearchUpdaterTest_Container_Field1' type='text' indexed='true' stored='true'",
$schema
);
$this->assertContains(
"<field name='SearchUpdaterTest_Container_Field2' type='text' indexed='true' stored='false'",
$schema
);

// Test with addAllFulltextFields
$index2 = new SolrIndexTest_FakeIndex2();
$index2->addAllFulltextFields();
$index2->addStoredField('Field2');
$schema2 = $index2->getFieldDefinitions();
$this->assertContains(
"<field name='SearchUpdaterTest_Container_Field1' type='text' indexed='true' stored='false'",
$schema2
);
$this->assertContains(
"<field name='SearchUpdaterTest_Container_Field2' type='text' indexed='true' stored='true'",
$schema2
);
}

protected function getServiceMock() {
return Phockito::mock('Solr3Service');
}
Expand Down Expand Up @@ -165,3 +198,20 @@ function init() {
$this->addFilterField('ManyManyObjects.Field1');
}
}


class SolrIndexTest_FakeIndex2 extends SolrIndex {

protected function getStoredDefault() {
// Override isDev defaulting to stored
return 'false';
}

function init() {
$this->addClass('SearchUpdaterTest_Container');
$this->addFilterField('MyDate', 'Date');
$this->addFilterField('HasOneObject.Field1');
$this->addFilterField('HasManyObjects.Field1');
$this->addFilterField('ManyManyObjects.Field1');
}
}

0 comments on commit 620d786

Please sign in to comment.