BUG Add missing addStoredFields method

This commit is contained in:
Damian Mooyman 2015-02-16 18:48:47 +13:00
parent 79eb663638
commit 620d7861b6
2 changed files with 75 additions and 3 deletions

View File

@ -81,7 +81,7 @@ abstract class SolrIndex extends SearchIndex {
function getFieldDefinitions() {
$xml = array();
$stored = Director::isDev() ? "stored='true'" : "stored='false'";
$stored = $this->getStoredDefault();
$xml[] = "";
@ -95,7 +95,7 @@ abstract class SolrIndex extends SearchIndex {
// 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
@ -143,6 +143,28 @@ abstract class SolrIndex extends SearchIndex {
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
@ -166,7 +188,7 @@ abstract class SolrIndex extends SearchIndex {
'name' => $name,
'type' => $type,
'indexed' => 'true',
'stored' => Director::isDev() ? 'true' : 'false',
'stored' => $this->getStoredDefault(),
'multiValued' => $multiValued
),
isset($spec['extra_options']) ? $spec['extra_options'] : array()

View File

@ -132,6 +132,39 @@ class SolrIndexTest extends SapphireTest {
$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');
}
@ -165,3 +198,20 @@ class SolrIndexTest_FakeIndex extends SolrIndex {
$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');
}
}