diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/Solr/SearchHandler.php b/module/VuFindSearch/src/VuFindSearch/Backend/Solr/SearchHandler.php index 7f50e6b90620f2914fae951b148b43ec84902521..d4c0977228caedb8ccf3d435dc0506c3a6c4736d 100644 --- a/module/VuFindSearch/src/VuFindSearch/Backend/Solr/SearchHandler.php +++ b/module/VuFindSearch/src/VuFindSearch/Backend/Solr/SearchHandler.php @@ -88,6 +88,8 @@ class SearchHandler if (empty($this->specs['DismaxHandler'])) { $this->specs['DismaxHandler'] = $defaultDismaxHandler; } + // Set default mm handler if necessary: + $this->setDefaultMustMatch(); } /// Public API @@ -248,6 +250,44 @@ class SearchHandler /// Internal API + /** + * Support method for constructor: if no mm is provided, set a reasonable + * default based on the selected Dismax handler. + * + * @return void + */ + protected function setDefaultMustMatch() + { + // Initialize parameter array if absent: + if (!isset($this->specs['DismaxParams'])) { + $this->specs['DismaxParams'] = []; + } + // Add mm if applicable: + if ($this->hasDismax()) { + // Our default mm depends on whether we're using dismax or edismax; + // for dismax, we want 100% matches, because we always want to + // simulate "AND" behavior by default (any "OR" searches will get + // rerouted to Lucene queries). For edismax, boolean operators are + // accounted for, and with an mm of 100%, OR searches will always + // fail. We can use 0% here, because the default q.op of AND will + // make AND searches work correctly even without a high mm value. + $default = $this->hasExtendedDismax() ? '0%' : '100%'; + + // Now if the configuration has no explicit mm value, let's push in + // our default: + $foundSetting = false; + foreach ($this->specs['DismaxParams'] as $current) { + if ($current[0] == 'mm') { + $foundSetting = true; + break; + } + } + if (!$foundSetting) { + $this->specs['DismaxParams'][] = ['mm', $default]; + } + } + } + /** * Return a Dismax subquery for specified search string. * diff --git a/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Solr/QueryBuilderTest.php b/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Solr/QueryBuilderTest.php index a58c0ef47524556e4320cf047679b63c1eaca6c7..8e12024ca03d1448090e8c5be62f57d1bca17a52 100644 --- a/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Solr/QueryBuilderTest.php +++ b/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Solr/QueryBuilderTest.php @@ -302,7 +302,7 @@ class QueryBuilderTest extends \VuFindTest\Unit\TestCase $response = $qb->build($q); $processedQ = $response->get('q'); - $this->assertEquals('((_query_:"{!dismax qf=\"field_a\" }value1") OR (_query_:"{!dismax qf=\"field_b\" }value2"))', $processedQ[0]); + $this->assertEquals('((_query_:"{!dismax qf=\"field_a\" mm=\\\'100%\\\'}value1") OR (_query_:"{!dismax qf=\"field_b\" mm=\\\'100%\\\'}value2"))', $processedQ[0]); } /** @@ -333,7 +333,7 @@ class QueryBuilderTest extends \VuFindTest\Unit\TestCase $response = $qb->build($q); $processedQ = $response->get('q'); - $this->assertEquals('((field_a:(value*)^100 OR field_c:(value*)^200) OR (_query_:"{!dismax qf=\"field_b\" }value2"))', $processedQ[0]); + $this->assertEquals('((field_a:(value*)^100 OR field_c:(value*)^200) OR (_query_:"{!dismax qf=\"field_b\" mm=\\\'100%\\\'}value2"))', $processedQ[0]); } /** diff --git a/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Solr/SearchHandlerTest.php b/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Solr/SearchHandlerTest.php index 4a96f87e049d7351107fd646b24e8615e61fed89..25d4de15591f79494d842cb5b7e2424488ebde09 100644 --- a/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Solr/SearchHandlerTest.php +++ b/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Solr/SearchHandlerTest.php @@ -51,7 +51,7 @@ class SearchHandlerTest extends PHPUnit_Framework_TestCase { $spec = ['DismaxParams' => [['foo', 'bar']], 'DismaxFields' => ['field1', 'field2']]; $hndl = new SearchHandler($spec); - $this->assertEquals('(_query_:"{!dismax qf=\"field1 field2\" foo=\\\'bar\\\'}foobar")', $hndl->createSimpleQueryString('foobar')); + $this->assertEquals('(_query_:"{!dismax qf=\"field1 field2\" foo=\\\'bar\\\' mm=\\\'100%\\\'}foobar")', $hndl->createSimpleQueryString('foobar')); } /** @@ -72,7 +72,7 @@ class SearchHandlerTest extends PHPUnit_Framework_TestCase */ public function testToArray() { - $spec = ['DismaxParams' => [['foo', 'bar']], 'DismaxFields' => ['field1', 'field2']]; + $spec = ['DismaxParams' => [['foo', 'bar'], ['mm', '100%']], 'DismaxFields' => ['field1', 'field2']]; $hndl = new SearchHandler($spec); $defaults = ['CustomMunge' => [], 'DismaxHandler' => 'dismax', 'QueryFields' => [], 'FilterQuery' => []]; $this->assertEquals($spec + $defaults, $hndl->toArray()); @@ -87,7 +87,7 @@ class SearchHandlerTest extends PHPUnit_Framework_TestCase { $spec = ['DismaxParams' => [['foo', 'bar']], 'DismaxFields' => ['field1', 'field2']]; $hndl = new SearchHandler($spec, 'edismax'); - $this->assertEquals('(_query_:"{!edismax qf=\"field1 field2\" foo=\\\'bar\\\'}foobar")', $hndl->createSimpleQueryString('foobar')); + $this->assertEquals('(_query_:"{!edismax qf=\"field1 field2\" foo=\\\'bar\\\' mm=\\\'0%\\\'}foobar")', $hndl->createSimpleQueryString('foobar')); } /**