|
46 | 46 | */ |
47 | 47 | class SearchHandler extends \VuFindSearch\Backend\Solr\SearchHandler |
48 | 48 | { |
49 | | - public function mungeValues($search, $tokenize = true) |
| 49 | + /** |
| 50 | + * Known configuration keys. |
| 51 | + * |
| 52 | + * @var array |
| 53 | + */ |
| 54 | + protected static $configKeys = [ |
| 55 | + 'CustomMunge', 'DismaxFields', 'DismaxHandler', 'QueryFields', |
| 56 | + 'DismaxParams', 'FilterQuery', 'DismaxMunge' |
| 57 | + ]; |
| 58 | + |
| 59 | + /** |
| 60 | + * Constructor. |
| 61 | + * |
| 62 | + * @param array $spec Search handler specification |
| 63 | + * @param string $defaultDismaxHandler Default dismax handler (if no |
| 64 | + * DismaxHandler set in specs). |
| 65 | + * |
| 66 | + * @return void |
| 67 | + */ |
| 68 | + public function __construct(array $spec, $defaultDismaxHandler = 'dismax') |
| 69 | + { |
| 70 | + foreach (self::$configKeys as $key) { |
| 71 | + $this->specs[$key] = $spec[$key] ?? []; |
| 72 | + } |
| 73 | + // Set dismax handler to default if not specified: |
| 74 | + if (empty($this->specs['DismaxHandler'])) { |
| 75 | + $this->specs['DismaxHandler'] = $defaultDismaxHandler; |
| 76 | + } |
| 77 | + // Set default mm handler if necessary: |
| 78 | + $this->setDefaultMustMatch(); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Apply standard pre-processing to the query string. |
| 83 | + * |
| 84 | + * @param string $search Search string |
| 85 | + * |
| 86 | + * @return string |
| 87 | + */ |
| 88 | + public function preprocessQueryString($search) |
| 89 | + { |
| 90 | + // Apply Dismax munging, if required: |
| 91 | + if ($this->hasDismax()) { |
| 92 | + return $this->dismaxMunge($search); |
| 93 | + } |
| 94 | + return $search; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Return the munge values for specified search string. |
| 99 | + * |
| 100 | + * If optional argument $tokenize is true tokenize the search string. |
| 101 | + * |
| 102 | + * @param string $search Search string |
| 103 | + * @param bool $tokenize Tokenize the search string? |
| 104 | + * |
| 105 | + * @return string |
| 106 | + */ |
| 107 | + protected function mungeValues($search, $tokenize = true) |
| 108 | + { |
| 109 | + if ($tokenize) { |
| 110 | + $tokens = $this->tokenize($search); |
| 111 | + $mungeValues = [ |
| 112 | + 'onephrase' => sprintf( |
| 113 | + '"%s"', str_replace('"', '', implode(' ', $tokens)) |
| 114 | + ), |
| 115 | + 'and' => implode(' AND ', $tokens), |
| 116 | + 'or' => implode(' OR ', $tokens), |
| 117 | + 'identity' => $search, |
| 118 | + ]; |
| 119 | + } else { |
| 120 | + $mungeValues = [ |
| 121 | + 'and' => $search, |
| 122 | + 'or' => $search, |
| 123 | + ]; |
| 124 | + // If we're skipping tokenization, we just want to pass $lookfor through |
| 125 | + // unmodified (it's probably an advanced search that won't benefit from |
| 126 | + // tokenization). We'll just set all possible values to the same thing, |
| 127 | + // except that we'll try to do the "one phrase" in quotes if possible. |
| 128 | + // IMPORTANT: If we detect a boolean NOT, we MUST omit the quotes. We |
| 129 | + // also omit quotes if the phrase is already quoted or if there is no |
| 130 | + // whitespace (in which case phrase searching is pointless and might |
| 131 | + // interfere with wildcard behavior): |
| 132 | + if (strstr($search, '"') || strstr($search, ' NOT ') |
| 133 | + || !preg_match('/\s/', $search) |
| 134 | + ) { |
| 135 | + $mungeValues['onephrase'] = $search; |
| 136 | + } else { |
| 137 | + $mungeValues['onephrase'] = sprintf('"%s"', $search); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + $mungeValues['identity'] = $search; |
| 142 | + |
| 143 | + foreach ($this->specs['CustomMunge'] as $mungeName => $mungeOps) { |
| 144 | + $mungeValues[$mungeName] = $search; |
| 145 | + foreach ($mungeOps as $operation) { |
| 146 | + $mungeValues[$mungeName] |
| 147 | + = $this->customMunge($mungeValues[$mungeName], $operation); |
| 148 | + } |
| 149 | + } |
| 150 | + return $mungeValues; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Apply custom search string munging to a Dismax query. |
| 155 | + * |
| 156 | + * @param string $search searchstring |
| 157 | + * |
| 158 | + * @return string |
| 159 | + */ |
| 160 | + protected function dismaxMunge($search) |
| 161 | + { |
| 162 | + foreach ($this->specs['DismaxMunge'] as $operation) { |
| 163 | + $search = $this->customMunge($search, $operation); |
| 164 | + } |
| 165 | + return $search; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Apply a munge operation to a search string. |
| 170 | + * |
| 171 | + * @param string $string string to munge |
| 172 | + * @param array $operation munge operation |
| 173 | + * |
| 174 | + * @return string |
| 175 | + */ |
| 176 | + protected function customMunge($string, $operation) |
| 177 | + { |
| 178 | + switch ($operation[0]) { |
| 179 | + case 'append': |
| 180 | + $string .= $operation[1]; |
| 181 | + break; |
| 182 | + case 'lowercase': |
| 183 | + $string = strtolower($string); |
| 184 | + break; |
| 185 | + case 'preg_replace': |
| 186 | + $string = preg_replace( |
| 187 | + $operation[1], $operation[2], $string |
| 188 | + ); |
| 189 | + break; |
| 190 | + case 'ucfirst': |
| 191 | + $string = ucfirst($string); |
| 192 | + break; |
| 193 | + case 'uppercase': |
| 194 | + $string = strtoupper($string); |
| 195 | + break; |
| 196 | + default: |
| 197 | + throw new \InvalidArgumentException( |
| 198 | + sprintf('Unknown munge operation: %s', $operation[0]) |
| 199 | + ); |
| 200 | + } |
| 201 | + return $string; |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * Return query string for specified search string. |
| 206 | + * |
| 207 | + * If optional argument $advanced is true the search string contains |
| 208 | + * advanced lucene query syntax. |
| 209 | + * |
| 210 | + * @param string $search Search string |
| 211 | + * @param bool $advanced Is the search an advanced search string? |
| 212 | + * |
| 213 | + * @return string |
| 214 | + */ |
| 215 | + protected function createQueryString($search, $advanced = false) |
50 | 216 | { |
51 | | - return parent::mungeValues($search, $tokenize); |
| 217 | + // If this is a basic query and we have Dismax settings (or if we have |
| 218 | + // Extended Dismax available), let's build a Dismax subquery to avoid |
| 219 | + // some of the ugly side effects of our Lucene query generation logic. |
| 220 | + if (($this->hasExtendedDismax() || !$advanced) && $this->hasDismax()) { |
| 221 | + $query = $this->dismaxSubquery( |
| 222 | + $this->dismaxMunge($search) |
| 223 | + ); |
| 224 | + } else { |
| 225 | + $mungeRules = $this->mungeRules(); |
| 226 | + // Do not munge w/o rules |
| 227 | + if ($mungeRules) { |
| 228 | + $mungeValues = $this->mungeValues($search, !$advanced); |
| 229 | + $query = $this->munge($mungeRules, $mungeValues); |
| 230 | + } else { |
| 231 | + $query = $search; |
| 232 | + } |
| 233 | + } |
| 234 | + if ($this->hasFilterQuery()) { |
| 235 | + $query = sprintf('(%s) AND (%s)', $query, $this->getFilterQuery()); |
| 236 | + } |
| 237 | + return "($query)"; |
52 | 238 | } |
53 | 239 | } |
0 commit comments