PageRenderTime 28ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Solarium/QueryType/MoreLikeThis/Query.php

http://github.com/basdenooijer/solarium
PHP | 438 lines | 155 code | 36 blank | 247 comment | 4 complexity | 06e2968976744c2f5bd5cba731904334 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Copyright 2011 Bas de Nooijer.
  4. * Copyright 2011 Gasol Wu. PIXNET Digital Media Corporation.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this listof conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  21. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * The views and conclusions contained in the software and documentation are
  30. * those of the authors and should not be interpreted as representing official
  31. * policies, either expressed or implied, of the copyright holder.
  32. *
  33. * @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
  34. * @copyright Copyright 2011 Gasol Wu <gasol.wu@gmail.com>
  35. * @license http://github.com/basdenooijer/solarium/raw/master/COPYING
  36. * @link http://www.solarium-project.org/
  37. */
  38. /**
  39. * @namespace
  40. */
  41. namespace Solarium\QueryType\MoreLikeThis;
  42. use Solarium\QueryType\Select\Query\Query as SelectQuery;
  43. use Solarium\Core\Client\Client;
  44. use Solarium\QueryType\MoreLikeThis\ResponseParser as ResponseParser;
  45. use Solarium\QueryType\MoreLikeThis\RequestBuilder as RequestBuilder;
  46. /**
  47. * MoreLikeThis Query
  48. *
  49. * Can be used to select documents and/or facets from Solr. This querytype has
  50. * lots of options and there are many Solarium subclasses for it.
  51. * See the Solr documentation and the relevant Solarium classes for more info.
  52. */
  53. class Query extends SelectQuery
  54. {
  55. /**
  56. * Default options
  57. *
  58. * @var array
  59. */
  60. protected $options = array(
  61. 'handler' => 'mlt',
  62. 'resultclass' => 'Solarium\QueryType\MoreLikeThis\Result',
  63. 'documentclass' => 'Solarium\QueryType\Select\Result\Document',
  64. 'query' => '*:*',
  65. 'start' => 0,
  66. 'rows' => 10,
  67. 'fields' => '*,score',
  68. 'interestingTerms' => 'none',
  69. 'matchinclude' => false,
  70. 'matchoffset' => 0,
  71. 'stream' => false,
  72. 'omitheader' => true,
  73. );
  74. /**
  75. * Get type for this query
  76. *
  77. * @return string
  78. */
  79. public function getType()
  80. {
  81. return Client::QUERY_MORELIKETHIS;
  82. }
  83. /**
  84. * Get a requestbuilder for this query
  85. *
  86. * @return RequestBuilder
  87. */
  88. public function getRequestBuilder()
  89. {
  90. return new RequestBuilder;
  91. }
  92. /**
  93. * Get a response parser for this query
  94. *
  95. * @return ResponseParser
  96. */
  97. public function getResponseParser()
  98. {
  99. return new ResponseParser;
  100. }
  101. /**
  102. * Set query stream option
  103. *
  104. * Set to true to post query content instead of using the URL param
  105. *
  106. * @link http://wiki.apache.org/solr/ContentStream ContentStream
  107. *
  108. * @param boolean $stream
  109. * @return self Provides fluent interface
  110. */
  111. public function setQueryStream($stream)
  112. {
  113. return $this->setOption('stream', $stream);
  114. }
  115. /**
  116. * Get stream option
  117. *
  118. * @return boolean
  119. */
  120. public function getQueryStream()
  121. {
  122. return $this->getOption('stream');
  123. }
  124. /**
  125. * Set the interestingTerms parameter. Must be one of: none, list, details.
  126. *
  127. * @see http://wiki.apache.org/solr/MoreLikeThisHandler#Params
  128. * @param string $term
  129. * @return self Provides fluent interface
  130. */
  131. public function setInterestingTerms($term)
  132. {
  133. return $this->setOption('interestingTerms', $term);
  134. }
  135. /**
  136. * Get the interestingTerm parameter.
  137. *
  138. * @return string
  139. */
  140. public function getInterestingTerms()
  141. {
  142. return $this->getOption('interestingTerms');
  143. }
  144. /**
  145. * Set the match.include parameter, which is either 'true' or 'false'.
  146. *
  147. * @see http://wiki.apache.org/solr/MoreLikeThisHandler#Params
  148. *
  149. * @param boolean $include
  150. * @return self Provides fluent interface
  151. */
  152. public function setMatchInclude($include)
  153. {
  154. return $this->setOption('matchinclude', $include);
  155. }
  156. /**
  157. * Get the match.include parameter.
  158. *
  159. * @return string
  160. */
  161. public function getMatchInclude()
  162. {
  163. return $this->getOption('matchinclude');
  164. }
  165. /**
  166. * Set the mlt.match.offset parameter, which determines the which result from the query should be used for MLT
  167. * For paging of MLT use setStart / setRows
  168. *
  169. * @see http://wiki.apache.org/solr/MoreLikeThisHandler#Params
  170. *
  171. * @param int $offset
  172. * @return self Provides fluent interface
  173. */
  174. public function setMatchOffset($offset)
  175. {
  176. return $this->setOption('matchoffset', $offset);
  177. }
  178. /**
  179. * Get the mlt.match.offset parameter.
  180. *
  181. * @return int
  182. */
  183. public function getMatchOffset()
  184. {
  185. return $this->getOption('matchoffset');
  186. }
  187. /**
  188. * Set MLT fields option
  189. *
  190. * The fields to use for similarity. NOTE: if possible, these should have a
  191. * stored TermVector
  192. *
  193. * Separate multiple fields with commas if you use string input.
  194. *
  195. * @param string|array $fields
  196. * @return self Provides fluent interface
  197. */
  198. public function setMltFields($fields)
  199. {
  200. if (is_string($fields)) {
  201. $fields = explode(',', $fields);
  202. $fields = array_map('trim', $fields);
  203. }
  204. return $this->setOption('mltfields', $fields);
  205. }
  206. /**
  207. * Get MLT fields option
  208. *
  209. * @return array
  210. */
  211. public function getMltFields()
  212. {
  213. $value = $this->getOption('mltfields');
  214. if ($value === null) {
  215. $value = array();
  216. }
  217. return $value;
  218. }
  219. /**
  220. * Set minimumtermfrequency option
  221. *
  222. * Minimum Term Frequency - the frequency below which terms will be ignored
  223. * in the source doc.
  224. *
  225. * @param int $minimum
  226. * @return self Provides fluent interface
  227. */
  228. public function setMinimumTermFrequency($minimum)
  229. {
  230. return $this->setOption('minimumtermfrequency', $minimum);
  231. }
  232. /**
  233. * Get minimumtermfrequency option
  234. *
  235. * @return integer|null
  236. */
  237. public function getMinimumTermFrequency()
  238. {
  239. return $this->getOption('minimumtermfrequency');
  240. }
  241. /**
  242. * Set minimumdocumentfrequency option
  243. *
  244. * Minimum Document Frequency - the frequency at which words will be
  245. * ignored which do not occur in at least this many docs.
  246. *
  247. * @param int $minimum
  248. * @return self Provides fluent interface
  249. */
  250. public function setMinimumDocumentFrequency($minimum)
  251. {
  252. return $this->setOption('minimumdocumentfrequency', $minimum);
  253. }
  254. /**
  255. * Get minimumdocumentfrequency option
  256. *
  257. * @return integer|null
  258. */
  259. public function getMinimumDocumentFrequency()
  260. {
  261. return $this->getOption('minimumdocumentfrequency');
  262. }
  263. /**
  264. * Set minimumwordlength option
  265. *
  266. * Minimum word length below which words will be ignored.
  267. *
  268. * @param int $minimum
  269. * @return self Provides fluent interface
  270. */
  271. public function setMinimumWordLength($minimum)
  272. {
  273. return $this->setOption('minimumwordlength', $minimum);
  274. }
  275. /**
  276. * Get minimumwordlength option
  277. *
  278. * @return integer|null
  279. */
  280. public function getMinimumWordLength()
  281. {
  282. return $this->getOption('minimumwordlength');
  283. }
  284. /**
  285. * Set maximumwordlength option
  286. *
  287. * Maximum word length above which words will be ignored.
  288. *
  289. * @param int $maximum
  290. * @return self Provides fluent interface
  291. */
  292. public function setMaximumWordLength($maximum)
  293. {
  294. return $this->setOption('maximumwordlength', $maximum);
  295. }
  296. /**
  297. * Get maximumwordlength option
  298. *
  299. * @return integer|null
  300. */
  301. public function getMaximumWordLength()
  302. {
  303. return $this->getOption('maximumwordlength');
  304. }
  305. /**
  306. * Set maximumqueryterms option
  307. *
  308. * Maximum number of query terms that will be included in any generated
  309. * query.
  310. *
  311. * @param int $maximum
  312. * @return self Provides fluent interface
  313. */
  314. public function setMaximumQueryTerms($maximum)
  315. {
  316. return $this->setOption('maximumqueryterms', $maximum);
  317. }
  318. /**
  319. * Get maximumqueryterms option
  320. *
  321. * @return integer|null
  322. */
  323. public function getMaximumQueryTerms()
  324. {
  325. return $this->getOption('maximumqueryterms');
  326. }
  327. /**
  328. * Set maximumnumberoftokens option
  329. *
  330. * Maximum number of tokens to parse in each example doc field that is not
  331. * stored with TermVector support.
  332. *
  333. * @param int $maximum
  334. * @return self Provides fluent interface
  335. */
  336. public function setMaximumNumberOfTokens($maximum)
  337. {
  338. return $this->setOption('maximumnumberoftokens', $maximum);
  339. }
  340. /**
  341. * Get maximumnumberoftokens option
  342. *
  343. * @return integer|null
  344. */
  345. public function getMaximumNumberOfTokens()
  346. {
  347. return $this->getOption('maximumnumberoftokens');
  348. }
  349. /**
  350. * Set boost option
  351. *
  352. * If true the query will be boosted by the interesting term relevance.
  353. *
  354. * @param boolean $boost
  355. * @return self Provides fluent interface
  356. */
  357. public function setBoost($boost)
  358. {
  359. return $this->setOption('boost', $boost);
  360. }
  361. /**
  362. * Get boost option
  363. *
  364. * @return boolean|null
  365. */
  366. public function getBoost()
  367. {
  368. return $this->getOption('boost');
  369. }
  370. /**
  371. * Set queryfields option
  372. *
  373. * Query fields and their boosts using the same format as that used in
  374. * DisMaxQParserPlugin. These fields must also be specified in fields.
  375. *
  376. * Separate multiple fields with commas if you use string input.
  377. *
  378. * @param string|array $queryFields
  379. * @return self Provides fluent interface
  380. */
  381. public function setQueryFields($queryFields)
  382. {
  383. if (is_string($queryFields)) {
  384. $queryFields = explode(',', $queryFields);
  385. $queryFields = array_map('trim', $queryFields);
  386. }
  387. return $this->setOption('queryfields', $queryFields);
  388. }
  389. /**
  390. * Get queryfields option
  391. *
  392. * @return array
  393. */
  394. public function getQueryFields()
  395. {
  396. $value = $this->getOption('queryfields');
  397. if ($value === null) {
  398. $value = array();
  399. }
  400. return $value;
  401. }
  402. }