PageRenderTime 21ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/titania/includes/library/ezcomponents/Search/abstraction/implementations/zend_lucene.php

https://github.com/marc1706/customisation-db
PHP | 445 lines | 168 code | 36 blank | 241 comment | 13 complexity | 1fe6135060fcf9504fb6ffed069abf3d MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. * File containing the ezcSearchQueryZendLucene class.
  4. *
  5. * @package Search
  6. * @version //autogen//
  7. * @copyright Copyright (C) 2005-2010 eZ Systems AS. All rights reserved.
  8. * @license http://ez.no/licenses/new_bsd New BSD License
  9. * @ignore
  10. */
  11. /**
  12. * ezcSearchQueryZendLucene implements the find query for searching documents.
  13. *
  14. * @package Search
  15. * @version //autogen//
  16. * @ignore
  17. */
  18. class ezcSearchQueryZendLucene implements ezcSearchFindQuery
  19. {
  20. /**
  21. * Holds the columns to return in the search result
  22. *
  23. * @var array
  24. */
  25. public $resultFields;
  26. /**
  27. * Holds the columns to highlight in the search result
  28. *
  29. * @var array(string)
  30. */
  31. public $highlightFields;
  32. /**
  33. * Holds all the search clauses that will be used to create the search query.
  34. *
  35. * @var array(string)
  36. */
  37. public $whereClauses;
  38. /**
  39. * Holds all the order by clauses that will be used to create the search query.
  40. *
  41. * @var array(string)
  42. */
  43. public $orderByClauses;
  44. /**
  45. * Holds the maximum number of results for the query.
  46. *
  47. * @var int
  48. */
  49. public $limit = null;
  50. /**
  51. * Holds the number of the first element to return in the results.
  52. *
  53. * This is used in combination with the $limit option.
  54. *
  55. * @var int
  56. */
  57. public $offset;
  58. /**
  59. * Holds all the facets
  60. *
  61. * @var array(string)
  62. */
  63. public $facets;
  64. /**
  65. * Holds the search handler for which this query is built.
  66. *
  67. * @var ezcSearchHandler
  68. */
  69. private $handler;
  70. /**
  71. * Contains the document definition for which this query is built.
  72. *
  73. * @param ezcSearchDocumentDefinition $definition
  74. */
  75. private $definition;
  76. /**
  77. * Constructs a new ezcSearchQueryZendLucene object for the handler $handler
  78. *
  79. * The handler implements mapping field names and values based on the
  80. * document $definition.
  81. *
  82. * @param ezcSearchHandler $handler
  83. * @param ezcSearchDocumentDefinition $definition
  84. */
  85. public function __construct( ezcSearchHandler $handler, ezcSearchDocumentDefinition $definition )
  86. {
  87. $this->handler = $handler;
  88. $this->definition = $definition;
  89. $this->reset();
  90. }
  91. /**
  92. * Resets all the internal query values to their defaults.
  93. */
  94. public function reset()
  95. {
  96. $this->resultFields = array();
  97. $this->highlightFields = array();
  98. $this->whereClauses = array();
  99. $this->limit = null;
  100. $this->offset = 0;
  101. $this->facets = array();
  102. }
  103. /**
  104. * Returns the definition that belongs to this query
  105. *
  106. * @return ezcSearchDocumentDefinition
  107. */
  108. function getDefinition()
  109. {
  110. return $this->definition;
  111. }
  112. /**
  113. * Returns the query as a string for debugging purposes
  114. *
  115. * @return string
  116. * @ignore
  117. */
  118. public function getQuery()
  119. {
  120. return $this->handler->getQuery( $this );
  121. }
  122. /**
  123. * Checks whether the field $field exists in the definition.
  124. *
  125. * @param string $field
  126. * @throws ezcSearchFieldNotDefinedException if the field is not defined.
  127. */
  128. private function checkIfFieldExists( $field )
  129. {
  130. if ( !isset( $this->definition->fields[$field] ) )
  131. {
  132. throw new ezcSearchFieldNotDefinedException( $this->definition->documentType, $field );
  133. }
  134. }
  135. /**
  136. * Adds the fields to return in the results.
  137. *
  138. * This method accepts either an array of fieldnames, but can also accept
  139. * multiple parameters as field names. The following is therefore
  140. * equivalent:
  141. * <code>
  142. * $q->select( array( 'one', 'two', 'three' ) );
  143. * $q->select( 'one', 'two', 'three' ) );
  144. * </code>
  145. *
  146. * If fields already have been added with this function, they will not be
  147. * overwritten when this function is called subsequently.
  148. *
  149. * @param mixed $...
  150. * @return ezcSearchQueryZendLucene
  151. */
  152. public function select()
  153. {
  154. $args = func_get_args();
  155. $cols = ezcSearchQueryTools::arrayFlatten( $args );
  156. $this->resultFields = array_merge( $this->resultFields, $cols );
  157. return $this;
  158. }
  159. /**
  160. * Adds the fields to highlight in the results.
  161. *
  162. * This method accepts either an array of fieldnames, but can also accept
  163. * multiple parameters as field names. The following is therefore
  164. * equivalent:
  165. * <code>
  166. * $q->highlight( array( 'one', 'two', 'three' ) );
  167. * $q->highlight( 'one', 'two', 'three' ) );
  168. * </code>
  169. *
  170. * If fields already have been added with this function, they will not be
  171. * overwritten when this function is called subsequently.
  172. *
  173. * @param mixed $...
  174. * @return ezcSearchQueryZendLucene
  175. */
  176. public function highlight()
  177. {
  178. $args = func_get_args();
  179. $cols = ezcSearchQueryTools::arrayFlatten( $args );
  180. $this->highlightFields = array_merge( $this->highlightFields, $cols );
  181. return $this;
  182. }
  183. /**
  184. * Adds a select/filter statement to the query
  185. *
  186. * @param string $clause
  187. * @return ezcSearchQueryZendLucene
  188. */
  189. public function where( $clause )
  190. {
  191. $this->whereClauses[] = $clause;
  192. return $this;
  193. }
  194. /**
  195. * Registers from which offset to start returning results, and how many results to return.
  196. *
  197. * $limit controls the maximum number of rows that will be returned.
  198. * $offset controls which row that will be the first in the result
  199. * set from the total amount of matching rows.
  200. *
  201. * @param int $limit
  202. * @param int $offset
  203. * @return ezcSearchQueryZendLucene
  204. */
  205. public function limit( $limit, $offset = 0 )
  206. {
  207. $this->limit = $limit;
  208. $this->offset = $offset;
  209. return $this;
  210. }
  211. /**
  212. * Tells the query on which field to sort on, and in which order
  213. *
  214. * You can call orderBy multiple times. Each call will add a
  215. * column to order by.
  216. *
  217. * @param string $field
  218. * @param int $type
  219. * @return ezcSearchQueryZendLucene
  220. */
  221. public function orderBy( $field, $type = ezcSearchQueryTools::ASC )
  222. {
  223. $field = $this->handler->mapFieldType( $field, $this->definition->fields[$field]->type );
  224. $this->orderByClauses[$field] = $type;
  225. return $this;
  226. }
  227. /**
  228. * Adds one facet to the query.
  229. *
  230. * Facets should only be used for STRING fields, and not TEXT fields.
  231. *
  232. * @param string $facet
  233. * @return ezcSearchQueryZendLucene
  234. */
  235. public function facet( $facet )
  236. {
  237. $field = $this->handler->mapFieldType( $facet, $this->definition->fields[$facet]->type );
  238. $this->facets[] = $field;
  239. return $this;
  240. }
  241. /**
  242. * Returns a string containing a field/value specifier, and an optional boost value.
  243. *
  244. * The method uses the document definition field type to map the fieldname
  245. * to a solr fieldname, and the $fieldType argument to escape the $value
  246. * correctly. If a definition is set, the $fieldType will be overridden
  247. * with the type from the definition.
  248. *
  249. * @param string $field
  250. * @param mixed $value
  251. *
  252. * @return string
  253. */
  254. public function eq( $field, $value )
  255. {
  256. $field = trim( $field );
  257. $this->checkIfFieldExists( $field );
  258. $fieldType = $this->definition->fields[$field]->type;
  259. $value = $this->handler->mapFieldValueForSearch( $fieldType, $value );
  260. $fieldName = $this->handler->mapFieldType( $field, $this->definition->fields[$field]->type );
  261. $ret = "$fieldName:$value";
  262. if ( $this->definition->fields[$field]->boost != 1 )
  263. {
  264. $ret .= "^{$this->definition->fields[$field]->boost}";
  265. }
  266. return $ret;
  267. }
  268. /**
  269. * Returns a string containing a field/value specifier, and an optional boost value.
  270. *
  271. * The method uses the document definition field type to map the fieldname
  272. * to a solr fieldname, and the $fieldType argument to escape the values
  273. * correctly.
  274. *
  275. * @param string $field
  276. * @param mixed $value1
  277. * @param mixed $value2
  278. *
  279. * @return string
  280. */
  281. public function between( $field, $value1, $value2 )
  282. {
  283. $field = trim( $field );
  284. $this->checkIfFieldExists( $field );
  285. $fieldType = $this->definition->fields[$field]->type;
  286. $value1 = $this->handler->mapFieldValueForSearch( $fieldType, $value1 );
  287. $value2 = $this->handler->mapFieldValueForSearch( $fieldType, $value2 );
  288. $fieldName = $this->handler->mapFieldType( $field, $this->definition->fields[$field]->type );
  289. $ret = "$fieldName:[$value1 TO $value2]";
  290. if ( $this->definition->fields[$field]->boost != 1 )
  291. {
  292. $ret .= "^{$this->definition->fields[$field]->boost}";
  293. }
  294. return $ret;
  295. }
  296. /**
  297. * Creates an OR clause
  298. *
  299. * This method accepts either an array of fieldnames, but can also accept
  300. * multiple parameters as field names.
  301. *
  302. * @param mixed $...
  303. * @return string
  304. */
  305. public function lOr()
  306. {
  307. $args = func_get_args();
  308. if ( count( $args ) < 1 )
  309. {
  310. throw new ezcSearchQueryVariableParameterException( 'lOr', count( $args ), 1 );
  311. }
  312. $elements = ezcSearchQueryTools::arrayFlatten( $args );
  313. if ( count( $elements ) == 1 )
  314. {
  315. return $elements[0];
  316. }
  317. else
  318. {
  319. return '( ' . join( ' OR ', $elements ) . ' )';
  320. }
  321. }
  322. /**
  323. * Creates an AND clause
  324. *
  325. * This method accepts either an array of fieldnames, but can also accept
  326. * multiple parameters as field names.
  327. *
  328. * @param mixed $...
  329. * @return string
  330. */
  331. public function lAnd()
  332. {
  333. $args = func_get_args();
  334. if ( count( $args ) < 1 )
  335. {
  336. throw new ezcSearchQueryVariableParameterException( 'lAnd', count( $args ), 1 );
  337. }
  338. $elements = ezcSearchQueryTools::arrayFlatten( $args );
  339. if ( count( $elements ) == 1 )
  340. {
  341. return $elements[0];
  342. }
  343. else
  344. {
  345. return '( ' . join( ' AND ', $elements ) . ' )';
  346. }
  347. }
  348. /**
  349. * Creates a NOT clause
  350. *
  351. * This method accepts a clause and negates it.
  352. *
  353. * @param string $clause
  354. * @return string
  355. */
  356. public function not( $clause )
  357. {
  358. return "!$clause";
  359. }
  360. /**
  361. * Creates an 'important' clause
  362. *
  363. * This method accepts a clause and marks it as important.
  364. *
  365. * @param string $clause
  366. * @return string
  367. */
  368. public function important( $clause )
  369. {
  370. return "+$clause";
  371. }
  372. /**
  373. * Modifies a clause to give it higher weight while searching.
  374. *
  375. * This method accepts a clause and adds a boost factor.
  376. *
  377. * @param string $clause
  378. * @param float $boostFactor
  379. * @return string
  380. */
  381. public function boost( $clause, $boostFactor )
  382. {
  383. // make sure we only apply boost once
  384. if ( preg_match( '@(.*)\^[0-9]+(\.[0-9]+)?$@', $clause, $matches ) )
  385. {
  386. $clause = $matches[1];
  387. }
  388. return "$clause^$boostFactor";
  389. }
  390. /**
  391. * Modifies a clause make it fuzzy.
  392. *
  393. * This method accepts a clause and registers it as a fuzzy search, an
  394. * optional fuzz factor is also supported.
  395. *
  396. * @param string $clause
  397. * @param float $fuzzFactor
  398. * @return string
  399. */
  400. public function fuzz( $clause, $fuzzFactor = null )
  401. {
  402. if ( $fuzzFactor )
  403. {
  404. return "$clause~$fuzzFactor";
  405. }
  406. return "$clause~";
  407. }
  408. }
  409. ?>