Skip to content

Instantly share code, notes, and snippets.

@htuscher
Created May 20, 2015 11:10
Show Gist options
  • Save htuscher/3e05c8deadefa87bff3b to your computer and use it in GitHub Desktop.
Save htuscher/3e05c8deadefa87bff3b to your computer and use it in GitHub Desktop.
Solr boost on field via entered query
<?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) . '"';
}
}
$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