Created
May 20, 2015 11:10
-
-
Save htuscher/3e05c8deadefa87bff3b to your computer and use it in GitHub Desktop.
Solr boost on field via entered query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*************************************************************** | |
* Copyright notice | |
* | |
* (c) 2015 Hans Höchtl <[email protected]> | |
* All rights reserved | |
* | |
* This script is part of the TYPO3 project. The TYPO3 project is | |
* free software; you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation; either version 2 of the License, or | |
* (at your option) any later version. | |
* | |
* The GNU General Public License can be found at | |
* http://www.gnu.org/copyleft/gpl.html. | |
* A copy is found in the textfile GPL.txt and important notices to the license | |
* from the author is found in LICENSE.txt distributed with these scripts. | |
* | |
* | |
* This script is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* This copyright notice MUST APPEAR in all copies of the script! | |
***************************************************************/ | |
namespace Onedrop\Solution\QueryModifier; | |
class BoostQueryModifier implements \Tx_Solr_QueryModifier { | |
/** | |
* @var \Tx_Solr_Search | |
*/ | |
protected $search; | |
/** | |
* @param \Tx_Solr_Query $query | |
* @return \Tx_Solr_Query The modified query | |
*/ | |
public function modifyQuery(\Tx_Solr_Query $query) { | |
$queryString = $query->getQueryString(); | |
$boostQueries = $query->getQueryParameter('bq'); | |
$queryStringParts = explode(' ', $queryString); | |
foreach ($queryStringParts as $queryStringPart) { | |
$boostQueries[] = '(category_textM:' . $this->escape($queryStringPart) . ')^10'; | |
} | |
$query->setBoostQuery($boostQueries); | |
return $query; | |
} | |
/** | |
* Quote and escape search strings | |
* | |
* @param string $string String to escape | |
* @return string The escaped/quoted string | |
*/ | |
public function escape($string) { | |
if (!is_numeric($string)) { | |
if (preg_match('/\W/', $string) == 1) { | |
// multiple words | |
$stringLength = strlen($string); | |
if ($string{0} == '"' && $string{$stringLength - 1} == '"') { | |
// phrase | |
$string = trim($string, '"'); | |
$string = $this->escapePhrase($string); | |
} else { | |
$string = $this->escapeSpecialCharacters($string); | |
} | |
} else { | |
$string = $this->escapeSpecialCharacters($string); | |
} | |
} | |
return $string; | |
} | |
/** | |
* Escapes characters with special meanings in Lucene query syntax. | |
* | |
* @param string $value Unescaped - "dirty" - string | |
* @return string Escaped - "clean" - string | |
*/ | |
protected function escapeSpecialCharacters($value) { | |
// list taken from http://lucene.apache.org/core/4_4_0/queryparser/org/apache/lucene/queryparser/classic/package-summary.html#package_description | |
// which mentions: + - && || ! ( ) { } [ ] ^ " ~ * ? : \ / | |
// of which we escape: ( ) { } [ ] ^ " ~ : \ / | |
// and explicitly don't escape: + - && || ! * ? | |
$pattern = '/(\\(|\\)|\\{|\\}|\\[|\\]|\\^|"|~|\:|\\\\|\\/)/'; | |
$replace = '\\\$1'; | |
return preg_replace($pattern, $replace, $value); | |
} | |
/** | |
* Escapes a value meant to be contained in a phrase with characters with | |
* special meanings in Lucene query syntax. | |
* | |
* @param string $value Unescaped - "dirty" - string | |
* @return string Escaped - "clean" - string | |
*/ | |
protected function escapePhrase($value) { | |
$pattern = '/("|\\\)/'; | |
$replace = '\\\$1'; | |
return '"' . preg_replace($pattern, $replace, $value) . '"'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['modifySearchQuery']['categoryNameBoosting'] = 'Onedrop\\Solution\\QueryModifier\\BoostQueryModifier'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment