| 1 |  |  | <?php | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Triadev\Leopard\Business\Dsl; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use ONGR\ElasticsearchDSL\BuilderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use ONGR\ElasticsearchDSL\Query\Compound\FunctionScoreQuery; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 7 |  |  | class FunctionScore | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  | { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  |     /** @var FunctionScoreQuery */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |     private $query; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |      * FunctionScore constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |      * @param BuilderInterface $query | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |      * @param array $params | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 17 | 6 |  |     public function __construct(BuilderInterface $query, array $params = []) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 19 | 6 |  |         $this->query = new FunctionScoreQuery($query, $params); | 
            
                                                                        
                            
            
                                    
            
            
                | 20 | 6 |  |     } | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * Get query | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      * @return BuilderInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 27 | 6 |  |     public function getQuery() : BuilderInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 | 6 |  |         return $this->query; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     } | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * Field | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * @param string $field | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * @param float $factor | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * @param string $modifier | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * @param \Closure|null $search | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      * @return FunctionScore | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 1 |  |     public function field( | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         string $field, | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         float $factor, | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         string $modifier = 'none', | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         ?\Closure $search = null | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     ) : FunctionScore { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 1 |  |         $this->query->addFieldValueFactorFunction( | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 1 |  |             $field, | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 1 |  |             $factor, | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 1 |  |             $modifier, | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 1 |  |             $this->buildQueryFromSearchClosure($search) | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 1 |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     } | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      * Decay | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * @param string $type | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * @param string $field | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      * @param array $function | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      * @param array $options | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * @param \Closure|null $search | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * @param int|null $weight | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * @return FunctionScore | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |     public function decay( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         string $type, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         string $field, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         array $function, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         array $options = [], | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         ?\Closure $search = null, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         int $weight = null | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     ) : FunctionScore { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |         $this->query->addDecayFunction( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |             $type, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |             $field, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |             $function, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |             $options, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |             $this->buildQueryFromSearchClosure($search), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |             $weight | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |          | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 | 1 |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     } | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      * Weight | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |      * @param float $weight | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |      * @param \Closure|null $search | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |      * @return FunctionScore | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |     public function weight(float $weight, ?\Closure $search = null) : FunctionScore | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 1 |  |         $this->query->addWeightFunction($weight, $this->buildQueryFromSearchClosure($search)); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 | 1 |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |     } | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |      * Random | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |      * @param int|null $seed | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |      * @param \Closure|null $search | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |      * @return FunctionScore | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 108 | 1 |  |     public function random(?int $seed = null, ?\Closure $search = null) : FunctionScore | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 110 | 1 |  |         $this->query->addRandomFunction($seed, $this->buildQueryFromSearchClosure($search)); | 
            
                                                                        
                            
            
                                    
            
            
                | 111 | 1 |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |     } | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |      * Script | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |      * @param string $inline | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |      * @param array $params | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |      * @param array $options | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |      * @param \Closure|null $search | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |      * @return FunctionScore | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 1 |  |     public function script( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         string $inline, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         array $params = [], | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         array $options = [], | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         ?\Closure $search = null | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     ) : FunctionScore { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 1 |  |         $this->query->addScriptScoreFunction( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 1 |  |             $inline, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 1 |  |             $params, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 1 |  |             $options, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 1 |  |             $this->buildQueryFromSearchClosure($search) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |          | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 136 | 1 |  |         return $this; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 137 |  |  |     } | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                                                                
            
                                    
            
            
                | 138 |  |  |      | 
            
                                                                        
                                                                
            
                                    
            
            
                | 139 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |      * Simple | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |      * @param array $functions | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |      * @return FunctionScore | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 1 |  |     public function simple(array $functions) : FunctionScore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 1 |  |         $this->query->addSimpleFunction($functions); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 148 | 1 |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |     } | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |      * Build query from search closure | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |      * @param \Closure|null $search | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |      * @return BuilderInterface|null | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 5 |  |     private function buildQueryFromSearchClosure(?\Closure $search) : ?BuilderInterface | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 5 |  |         $query = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 5 |  |         if ($search) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 3 |  |             $searchBuilder = app()->make(Search::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 3 |  |             $search($searchBuilder); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 3 |  |             $query = $searchBuilder->getQuery(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |          | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 168 | 5 |  |         return $query; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 169 |  |  |     } | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 170 |  |  | } | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 171 |  |  |  |