diff --git a/code/search/SearchIndex.php b/code/search/SearchIndex.php
index 81f5993..fd3904b 100644
--- a/code/search/SearchIndex.php
+++ b/code/search/SearchIndex.php
@@ -47,7 +47,7 @@ abstract class SearchIndex extends ViewableData {
* Examines the classes this index is built on to try and find defined fields in the class hierarchy for those classes.
* Looks for db and viewable-data fields, although can't nessecarily find type for viewable-data fields.
*/
- function fieldData($field, $forceType = null) {
+ function fieldData($field, $forceType = null, $extraOptions = array()) {
$fullfield = str_replace(".", "_", $field);
$sources = $this->getClasses();
@@ -150,7 +150,8 @@ abstract class SearchIndex extends ViewableData {
'class' => $dataclass,
'lookup_chain' => $fieldoptions['lookup_chain'],
'type' => $forceType ? $forceType : $type,
- 'multi_valued' => isset($fieldoptions['multi_valued']) ? true : false
+ 'multi_valued' => isset($fieldoptions['multi_valued']) ? true : false,
+ 'extra_options' => $extraOptions
);
}
}
@@ -200,9 +201,10 @@ abstract class SearchIndex extends ViewableData {
* Add a field that should be fulltext searchable
* @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 String $extraOptions - Dependent on search implementation
*/
- public function addFulltextField($field, $forceType = null) {
- $this->fulltextFields = array_merge($this->fulltextFields, $this->fieldData($field, $forceType));
+ public function addFulltextField($field, $forceType = null, $extraOptions = array()) {
+ $this->fulltextFields = array_merge($this->fulltextFields, $this->fieldData($field, $forceType, $extraOptions));
}
public function getFulltextFields() { return $this->fulltextFields; }
@@ -211,9 +213,10 @@ abstract class SearchIndex extends ViewableData {
* Add a field that should be filterable
* @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 String $extraOptions - Dependent on search implementation
*/
- public function addFilterField($field, $forceType = null) {
- $this->filterFields = array_merge($this->filterFields, $this->fieldData($field, $forceType));
+ public function addFilterField($field, $forceType = null, $extraOptions = array()) {
+ $this->filterFields = array_merge($this->filterFields, $this->fieldData($field, $forceType, $extraOptions));
}
public function getFilterFields() { return $this->filterFields; }
@@ -222,9 +225,10 @@ abstract class SearchIndex extends ViewableData {
* Add a field that should be sortable
* @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 String $extraOptions - Dependent on search implementation
*/
- public function addSortField($field, $forceType = null) {
- $this->sortFields = array_merge($this->sortFields, $this->fieldData($field, $forceType));
+ public function addSortField($field, $forceType = null, $extraOptions = array()) {
+ $this->sortFields = array_merge($this->sortFields, $this->fieldData($field, $forceType, $extraOptions));
}
public function getSortFields() { return $this->sortFields; }
diff --git a/code/solr/SolrIndex.php b/code/solr/SolrIndex.php
index d56c83c..68d4f0f 100644
--- a/code/solr/SolrIndex.php
+++ b/code/solr/SolrIndex.php
@@ -57,33 +57,69 @@ abstract class SolrIndex extends SearchIndex {
// Add the user-specified fields
foreach ($this->fulltextFields as $name => $field) {
- $type = isset(self::$fulltextTypeMap[$field['type']]) ? self::$fulltextTypeMap[$field['type']] : self::$fulltextTypeMap['*'];
- $xml[] = "";
+ $xml[] = $this->getFieldDefinition($name, $field, self::$fulltextTypeMap);
}
foreach ($this->filterFields as $name => $field) {
if ($field['fullfield'] == 'ID' || $field['fullfield'] == 'ClassName') continue;
-
- $multiValued = (isset($field['multi_valued']) && $field['multi_valued']) ? "multiValued='true'" : '';
-
- $type = isset(self::$filterTypeMap[$field['type']]) ? self::$filterTypeMap[$field['type']] : self::$filterTypeMap['*'];
- $xml[] = "";
+ $xml[] = $this->getFieldDefinition($name, $field);
}
foreach ($this->sortFields as $name => $field) {
if ($field['fullfield'] == 'ID' || $field['fullfield'] == 'ClassName') continue;
-
- $multiValued = (isset($field['multi_valued']) && $field['multi_valued']) ? "multiValued='true'" : '';
-
- $typeMap = array_merge(self::$filterTypeMap, self::$sortTypeMap);
- $type = isset($typeMap[$field['type']]) ? $typeMap[$field['type']] : $typeMap['*'];
-
- $xml[] = "";
+ $xml[] = $this->getFieldDefinition($name, $field);
}
return implode("\n\t\t", $xml);
}
+ /**
+ * @param String $name
+ * @param Array $spec
+ * @param Array $typeMap
+ * @return String XML
+ */
+ protected function getFieldDefinition($name, $spec, $typeMap = null) {
+ if(!$typeMap) $typeMap = self::$filterTypeMap;
+ $multiValued = (isset($spec['multi_valued']) && $spec['multi_valued']) ? "true" : '';
+ $type = isset($typeMap[$spec['type']]) ? $typeMap[$spec['type']] : $typeMap['*'];
+
+ $fieldParams = array_merge(
+ array(
+ 'name' => $name,
+ 'type' => $type,
+ 'indexed' => 'true',
+ 'stored' => Director::isDev() ? 'true' : 'false',
+ 'multiValued' => $multiValued
+ ),
+ isset($spec['extra_options']) ? $spec['extra_options'] : array()
+ );
+
+ return $this->toXmlTag(
+ "field",
+ $fieldParams
+ );
+ }
+
+ /**
+ * Convert definition to XML tag
+ *
+ * @param String $tag
+ * @param String $attrs Map of attributes
+ * @param String $content Inner content
+ * @return String XML tag
+ */
+ protected function toXmlTag($tag, $attrs, $content = null) {
+ $xml = "<$tag ";
+ if($attrs) {
+ $attrStrs = array();
+ foreach($attrs as $attrName => $attrVal) $attrStrs[] = "$attrName='$attrVal'";
+ $xml .= $attrStrs ? implode(' ', $attrStrs) : '';
+ }
+ $xml .= $content ? ">$content$tag>" : '/>';
+ return $xml;
+ }
+
function getCopyFieldDefinitions() {
$xml = array();
diff --git a/tests/SolrIndexTest.php b/tests/SolrIndexTest.php
index 345dd7b..afb8f2d 100644
--- a/tests/SolrIndexTest.php
+++ b/tests/SolrIndexTest.php
@@ -49,6 +49,22 @@ class SolrIndexTest extends SapphireTest {
$this->assertEquals('2010-12-30T00:00:00Z', $value['value'], 'Writes non-NULL dates');
}
+ function testAddFieldExtraOptions() {
+ $origMode = Director::get_environment_type();
+ Director::set_environment_type('live'); // dev mode would for stored=true for everything
+ $index = new SolrIndexTest_FakeIndex();
+
+ $defs = simplexml_load_string('' . $index->getFieldDefinitions() . '');
+ $defField1 = $defs->xpath('field[@name="SearchUpdaterTest_Container_Field1"]');
+ $this->assertEquals((string)$defField1[0]['stored'], 'false');
+
+ $index->addFilterField('Field1', null, array('stored' => 'true'));
+ $defs = simplexml_load_string('' . $index->getFieldDefinitions() . '');
+ $defField1 = $defs->xpath('field[@name="SearchUpdaterTest_Container_Field1"]');
+ $this->assertEquals((string)$defField1[0]['stored'], 'true');
+
+ Director::set_environment_type($origMode);
+ }
protected function getServiceMock() {
$serviceMock = Phockito::mock('SolrService');
$fakeResponse = new Apache_Solr_Response(new Apache_Solr_HttpTransport_Response(null, null, null));