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