diff --git a/code/solr/SolrIndex.php b/code/solr/SolrIndex.php
index b6b7a951..43991fbf 100644
--- a/code/solr/SolrIndex.php
+++ b/code/solr/SolrIndex.php
@@ -81,7 +81,7 @@ function addAnalyzer($field, $type, $params) {
function getFieldDefinitions() {
$xml = array();
- $stored = Director::isDev() ? "stored='true'" : "stored='false'";
+ $stored = $this->getStoredDefault();
$xml[] = "";
@@ -95,7 +95,7 @@ function getFieldDefinitions() {
// Add the fulltext collation field
- $xml[] = "" ;
+ $xml[] = "" ;
// Add the user-specified fields
@@ -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
@@ -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()
diff --git a/tests/SolrIndexTest.php b/tests/SolrIndexTest.php
index 7b05da2a..b3aee60e 100644
--- a/tests/SolrIndexTest.php
+++ b/tests/SolrIndexTest.php
@@ -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(
+ "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');
+ }
+}