Query.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace Elastica\Rescore;
  3. use Elastica\Query as BaseQuery;
  4. /**
  5. * Query Rescore.
  6. *
  7. * @author Jason Hu <mjhu91@gmail.com>
  8. *
  9. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-rescore.html
  10. */
  11. class Query extends AbstractRescore
  12. {
  13. /**
  14. * Constructor.
  15. *
  16. * @param string|\Elastica\Query\AbstractQuery $query
  17. */
  18. public function __construct($query = null)
  19. {
  20. $this->setParam('query', []);
  21. $this->setRescoreQuery($query);
  22. }
  23. /**
  24. * Override default implementation so params are in the format
  25. * expected by elasticsearch.
  26. *
  27. * @return array Rescore array
  28. */
  29. public function toArray()
  30. {
  31. $data = $this->getParams();
  32. if (!empty($this->_rawParams)) {
  33. $data = array_merge($data, $this->_rawParams);
  34. }
  35. $array = $this->_convertArrayable($data);
  36. if (isset($array['query']['rescore_query']['query'])) {
  37. $array['query']['rescore_query'] = $array['query']['rescore_query']['query'];
  38. }
  39. return $array;
  40. }
  41. /**
  42. * Sets rescoreQuery object.
  43. *
  44. * @param string|\Elastica\Query|\Elastica\Query\AbstractQuery $rescoreQuery
  45. *
  46. * @return $this
  47. */
  48. public function setRescoreQuery($rescoreQuery)
  49. {
  50. $rescoreQuery = BaseQuery::create($rescoreQuery);
  51. $query = $this->getParam('query');
  52. $query['rescore_query'] = $rescoreQuery;
  53. return $this->setParam('query', $query);
  54. }
  55. /**
  56. * Sets query_weight.
  57. *
  58. * @param float $weight
  59. *
  60. * @return $this
  61. */
  62. public function setQueryWeight($weight)
  63. {
  64. $query = $this->getParam('query');
  65. $query['query_weight'] = $weight;
  66. return $this->setParam('query', $query);
  67. }
  68. /**
  69. * Sets rescore_query_weight.
  70. *
  71. * @param float $weight
  72. *
  73. * @return $this
  74. */
  75. public function setRescoreQueryWeight($weight)
  76. {
  77. $query = $this->getParam('query');
  78. $query['rescore_query_weight'] = $weight;
  79. return $this->setParam('query', $query);
  80. }
  81. }