From 620d7861b60119f11b47c532bc36f6e5f9ea0bb5 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Mon, 16 Feb 2015 18:48:47 +1300 Subject: [PATCH] BUG Add missing addStoredFields method --- code/solr/SolrIndex.php | 28 ++++++++++++++++++++--- tests/SolrIndexTest.php | 50 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/code/solr/SolrIndex.php b/code/solr/SolrIndex.php index b6b7a95..43991fb 100644 --- a/code/solr/SolrIndex.php +++ b/code/solr/SolrIndex.php @@ -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[] = "" ; + $xml[] = "" ; // 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() diff --git a/tests/SolrIndexTest.php b/tests/SolrIndexTest.php index 7b05da2..b3aee60 100644 --- a/tests/SolrIndexTest.php +++ b/tests/SolrIndexTest.php @@ -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( + "assertContains( + "addAllFulltextFields(); + $index2->addStoredField('Field2'); + $schema2 = $index2->getFieldDefinitions(); + $this->assertContains( + "assertContains( + "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'); + } +}