Passed
Pull Request — master (#13)
by Christopher
05:25 queued 01:06
created

Search::overwriteType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Triadev\Leopard\Business\Dsl;
3
4
use Illuminate\Database\Eloquent\Model;
5
use ONGR\ElasticsearchDSL\Sort\FieldSort;
6
use Triadev\Leopard\Busines\Dsl\Query\Specialized;
7
use Triadev\Leopard\Business\Dsl\Query\Compound;
8
use Triadev\Leopard\Business\Dsl\Query\Fulltext;
9
use Triadev\Leopard\Business\Dsl\Query\Geo;
10
use Triadev\Leopard\Business\Dsl\Query\InnerHit;
11
use Triadev\Leopard\Business\Dsl\Query\Joining;
12
use Triadev\Leopard\Business\Dsl\Query\TermLevel;
13
14
class Search extends AbstractQuery
15
{
16
    /**
17
     * Search constructor.
18
     * @param \ONGR\ElasticsearchDSL\Search|null $search
19
     * @param Model|null $model
20
     */
21 30
    public function __construct(
22
        ?\ONGR\ElasticsearchDSL\Search $search = null,
23
        ?Model $model = null
24
    ) {
25 30
        parent::__construct($search, $model);
26 30
    }
27
    
28
    /**
29
     * Aggregation
30
     *
31
     * @param \Closure $aggregation
32
     * @return Search
33
     */
34 1
    public function aggregation(\Closure $aggregation) : Search
35
    {
36 1
        $aggregation(new Aggregation($this->search));
37 1
        return $this;
38
    }
39
    
40
    /**
41
     * Term level
42
     *
43
     * @return TermLevel
44
     */
45 22
    public function termLevel() : TermLevel
46
    {
47 22
        return new TermLevel($this->search, $this->model);
48
    }
49
    
50
    /**
51
     * Fulltext
52
     *
53
     * @return Fulltext
54
     */
55 2
    public function fulltext() : Fulltext
56
    {
57 2
        return new Fulltext($this->search, $this->model);
58
    }
59
    
60
    /**
61
     * Geo
62
     *
63
     * @return Geo
64
     */
65 1
    public function geo() : Geo
66
    {
67 1
        return new Geo($this->search, $this->model);
68
    }
69
    
70
    /**
71
     * Compound
72
     *
73
     * @return Compound
74
     */
75 7
    public function compound() : Compound
76
    {
77 7
        return new Compound($this->search, $this->model);
78
    }
79
    
80
    /**
81
     * Joining
82
     *
83
     * @return Joining
84
     */
85 1
    public function joining() : Joining
86
    {
87 1
        return new Joining($this->search, $this->model);
88
    }
89
    
90
    /**
91
     * Specialized
92
     *
93
     * @return Specialized
94
     */
95 1
    public function specialized() : Specialized
96
    {
97 1
        return new Specialized($this->search, $this->model);
98
    }
99
    
100
    /**
101
     * Inner hit
102
     *
103
     * @return InnerHit
104
     */
105 1
    public function innerHit() : InnerHit
106
    {
107 1
        return new InnerHit($this->search, $this->model);
108
    }
109
    
110
    /**
111
     * Paginate
112
     *
113
     * @param int $page
114
     * @param int $limit
115
     * @return Search
116
     */
117 1
    public function paginate(int $page, int $limit = 25) : Search
118
    {
119 1
        $this->search->setFrom($limit * ($page - 1))->setSize($limit);
120 1
        return $this;
121
    }
122
    
123
    /**
124
     * Min score
125
     *
126
     * @param int $minScore
127
     * @return Search
128
     */
129 1
    public function minScore(int $minScore) : Search
130
    {
131 1
        $this->search->setMinScore($minScore);
132 1
        return $this;
133
    }
134
    
135
    /**
136
     * Sort
137
     *
138
     * @param string $field
139
     * @param string $order
140
     * @param array $params
141
     * @return Search
142
     */
143 1
    public function sort(string $field, string $order = FieldSort::DESC, array $params = []) : Search
144
    {
145 1
        $this->search->addSort(new FieldSort(
146 1
            $field,
147 1
            $order,
148 1
            $params
149
        ));
150
        
151 1
        return $this;
152
    }
153
}
154