From 874bd32300bf6735685b9dbabeee826e863d61a2 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Wed, 5 Sep 2012 18:16:40 +0200 Subject: [PATCH] ENHANCEMENT Solr->addCopyFields() --- code/solr/SolrIndex.php | 20 ++++++++++++++++++++ tests/SolrIndexTest.php | 14 ++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/code/solr/SolrIndex.php b/code/solr/SolrIndex.php index 0dffeee..87a7bc1 100644 --- a/code/solr/SolrIndex.php +++ b/code/solr/SolrIndex.php @@ -26,6 +26,8 @@ abstract class SolrIndex extends SearchIndex { protected $analyzerFields = array(); + protected $copyFields = array(); + function generateSchema() { return $this->renderWith(Director::baseFolder() . '/fulltextsearch/conf/templates/schema.ss'); } @@ -148,6 +150,18 @@ abstract class SolrIndex extends SearchIndex { return $xml; } + /** + * @param String $source Composite field name (_) + * @param String $dest + */ + function addCopyField($source, $dest, $extraOptions = array()) { + if(!isset($this->copyFields[$source])) $this->copyFields[$source] = array(); + $this->copyFields[$source][] = array_merge( + array('source' => $source, 'dest' => $dest), + $extraOptions + ); + } + function getCopyFieldDefinitions() { $xml = array(); @@ -155,6 +169,12 @@ abstract class SolrIndex extends SearchIndex { $xml[] = ""; } + foreach ($this->copyFields as $source => $fields) { + foreach($fields as $fieldAttrs) { + $xml[] = $this->toXmlTag('copyField', $fieldAttrs); + } + } + return implode("\n\t", $xml); } diff --git a/tests/SolrIndexTest.php b/tests/SolrIndexTest.php index 411b17b..3abddb1 100644 --- a/tests/SolrIndexTest.php +++ b/tests/SolrIndexTest.php @@ -82,8 +82,18 @@ class SolrIndexTest extends SapphireTest { $this->assertEquals('solr.HTMLStripCharFilterFactory', $analyzers[0]->charFilter[0]['class']); } - protected function getServiceMock() { - $serviceMock = Phockito::mock('SolrService'); + function testAddCopyField() { + $index = new SolrIndexTest_FakeIndex(); + $index->addCopyField('sourceField', 'destField'); + $defs = simplexml_load_string('' . $index->getCopyFieldDefinitions() . ''); + $lastDef = array_pop($defs); + + $this->assertEquals('sourceField', $lastDef['source']); + $this->assertEquals('destField', $lastDef['dest']); + } + + protected function getServiceSpy() { + $serviceSpy = Phockito::spy('SolrService'); $fakeResponse = new Apache_Solr_Response(new Apache_Solr_HttpTransport_Response(null, null, null)); Phockito::when($serviceMock)