MoreLikeThis.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace Elastica\Query;
  3. use Elastica\Document;
  4. /**
  5. * More Like This query.
  6. *
  7. * @author Raul Martinez, Jr <juneym@gmail.com>
  8. *
  9. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-mlt-query.html
  10. */
  11. class MoreLikeThis extends AbstractQuery
  12. {
  13. /**
  14. * Set fields to which to restrict the mlt query.
  15. *
  16. * @param array $fields Field names
  17. *
  18. * @return \Elastica\Query\MoreLikeThis Current object
  19. */
  20. public function setFields(array $fields)
  21. {
  22. return $this->setParam('fields', $fields);
  23. }
  24. /**
  25. * Set the "like" value.
  26. *
  27. * @param string|Document $like
  28. *
  29. * @return $this
  30. */
  31. public function setLike($like)
  32. {
  33. return $this->setParam('like', $like);
  34. }
  35. /**
  36. * Set boost.
  37. *
  38. * @param float $boost Boost value
  39. *
  40. * @return $this
  41. */
  42. public function setBoost($boost)
  43. {
  44. return $this->setParam('boost', (float) $boost);
  45. }
  46. /**
  47. * Set max_query_terms.
  48. *
  49. * @param int $maxQueryTerms Max query terms value
  50. *
  51. * @return $this
  52. */
  53. public function setMaxQueryTerms($maxQueryTerms)
  54. {
  55. return $this->setParam('max_query_terms', (int) $maxQueryTerms);
  56. }
  57. /**
  58. * Set min term frequency.
  59. *
  60. * @param int $minTermFreq
  61. *
  62. * @return $this
  63. */
  64. public function setMinTermFrequency($minTermFreq)
  65. {
  66. return $this->setParam('min_term_freq', (int) $minTermFreq);
  67. }
  68. /**
  69. * set min document frequency.
  70. *
  71. * @param int $minDocFreq
  72. *
  73. * @return $this
  74. */
  75. public function setMinDocFrequency($minDocFreq)
  76. {
  77. return $this->setParam('min_doc_freq', (int) $minDocFreq);
  78. }
  79. /**
  80. * set max document frequency.
  81. *
  82. * @param int $maxDocFreq
  83. *
  84. * @return $this
  85. */
  86. public function setMaxDocFrequency($maxDocFreq)
  87. {
  88. return $this->setParam('max_doc_freq', (int) $maxDocFreq);
  89. }
  90. /**
  91. * Set min word length.
  92. *
  93. * @param int $minWordLength
  94. *
  95. * @return $this
  96. */
  97. public function setMinWordLength($minWordLength)
  98. {
  99. return $this->setParam('min_word_length', (int) $minWordLength);
  100. }
  101. /**
  102. * Set max word length.
  103. *
  104. * @param int $maxWordLength
  105. *
  106. * @return $this
  107. */
  108. public function setMaxWordLength($maxWordLength)
  109. {
  110. return $this->setParam('max_word_length', (int) $maxWordLength);
  111. }
  112. /**
  113. * Set boost terms.
  114. *
  115. * @param bool $boostTerms
  116. *
  117. * @return $this
  118. */
  119. public function setBoostTerms($boostTerms)
  120. {
  121. return $this->setParam('boost_terms', (bool) $boostTerms);
  122. }
  123. /**
  124. * Set analyzer.
  125. *
  126. * @param string $analyzer
  127. *
  128. * @return $this
  129. */
  130. public function setAnalyzer($analyzer)
  131. {
  132. $analyzer = trim($analyzer);
  133. return $this->setParam('analyzer', $analyzer);
  134. }
  135. /**
  136. * Set stop words.
  137. *
  138. * @param array $stopWords
  139. *
  140. * @return $this
  141. */
  142. public function setStopWords(array $stopWords)
  143. {
  144. return $this->setParam('stop_words', $stopWords);
  145. }
  146. /**
  147. * Set minimum_should_match option.
  148. *
  149. * @param int|string $minimumShouldMatch
  150. *
  151. * @return $this
  152. */
  153. public function setMinimumShouldMatch($minimumShouldMatch)
  154. {
  155. return $this->setParam('minimum_should_match', $minimumShouldMatch);
  156. }
  157. public function toArray()
  158. {
  159. $array = parent::toArray();
  160. // If _id is provided, perform MLT on an existing document from the index
  161. // If _source is provided, perform MLT on a document provided as an input
  162. if (!empty($array['more_like_this']['like']['_id'])) {
  163. $doc = $array['more_like_this']['like'];
  164. $doc = array_intersect_key($doc, ['_index' => 1, '_type' => 1, '_id' => 1]);
  165. $array['more_like_this']['like'] = $doc;
  166. } elseif (!empty($array['more_like_this']['like']['_source'])) {
  167. $doc = $array['more_like_this']['like'];
  168. $doc['doc'] = $array['more_like_this']['like']['_source'];
  169. unset($doc['_id']);
  170. unset($doc['_source']);
  171. $array['more_like_this']['like'] = $doc;
  172. }
  173. return $array;
  174. }
  175. }