Add support for modification date queries: "before:", "after:" and "date:"
[phinde.git] / src / phinde / Elasticsearch.php
1 <?php
2 namespace phinde;
3
4 class Elasticsearch
5 {
6     protected $baseUrl;
7
8     public function __construct($baseUrl)
9     {
10         $this->baseUrl = $baseUrl;
11     }
12
13     /**
14      * @link https://www.elastic.co/guide/en/elasticsearch/guide/current/_finding_exact_values.html
15      */
16     public function isKnown($url)
17     {
18         $r = new Elasticsearch_Request(
19             $this->baseUrl . 'document/_search/exists',
20             \HTTP_Request2::METHOD_GET
21         );
22         $r->allow404 = true;
23         $r->setBody(
24             json_encode(
25                 array(
26                     'query' => array(
27                         'filtered' => array(
28                             'filter' => array(
29                                 'term' => array(
30                                     'url' => $url
31                                 )
32                             )
33                         )
34                     )
35                 )
36             )
37         );
38         $res = json_decode($r->send()->getBody());
39         return $res->exists;
40     }
41
42     public function get($url)
43     {
44         $r = new Elasticsearch_Request(
45             $this->baseUrl . 'document/' . rawurlencode($url),
46             \HTTP_Request2::METHOD_GET
47         );
48         $r->allow404 = true;
49         $res = $r->send();
50         if ($res->getStatus() != 200) {
51             return null;
52         }
53         $d = json_decode($res->getBody());
54         return $d->_source;
55     }
56
57     public function markQueued($url)
58     {
59         $r = new Elasticsearch_Request(
60             $this->baseUrl . 'document/' . rawurlencode($url),
61             \HTTP_Request2::METHOD_PUT
62         );
63         $doc = array(
64             'status' => 'queued',
65             'url' => $url
66         );
67         $r->setBody(json_encode($doc));
68         $r->send();
69     }
70
71     public function search($query, $filters, $site, $page, $perPage, $sort)
72     {
73         if (preg_match('#nick:([^ ]*)#', $query, $matches)) {
74             $authorName = $matches[1];
75             $query = str_replace(
76                 'nick:' . $authorName,
77                 'author.name:' . $authorName,
78                 $query
79             );
80         }
81
82         $qMust = array();//query parts for the MUST section
83
84         //modification date filters
85         if (preg_match('#after:([^ ]+)#', $query, $matches)) {
86             $dateAfter = $matches[1];
87             $query      = trim(str_replace($matches[0], '', $query));
88             $qMust[]    = array(
89                 'range' => array(
90                     'modate' => array(
91                         'gt' => $dateAfter . '||/d',
92                     )
93                 )
94             );
95         }
96         if (preg_match('#before:([^ ]+)#', $query, $matches)) {
97             $dateBefore = $matches[1];
98             $query      = trim(str_replace($matches[0], '', $query));
99             $qMust[]    = array(
100                 'range' => array(
101                     'modate' => array(
102                         'lt' => $dateBefore . '||/d',
103                     )
104                 )
105             );
106         }
107         if (preg_match('#date:([^ ]+)#', $query, $matches)) {
108             $dateExact = $matches[1];
109             $query      = trim(str_replace($matches[0], '', $query));
110             $qMust[]    = array(
111                 'range' => array(
112                     'modate' => array(
113                         'gte' => $dateExact . '||/d',
114                         'lte' => $dateExact . '||/d',
115                     )
116                 )
117             );
118         }
119
120         $qMust[] = array(
121             'query_string' => array(
122                 'default_field' => '_all',
123                 'default_operator' => 'AND',
124                 'query' => $query
125             )
126         );
127         $qMust[] = array(
128             'term' => array(
129                 'status' => 'indexed'
130             )
131         );
132
133         if ($sort == 'date') {
134             $sortCfg = array('modate' => array('order' => 'desc'));
135         } else {
136             $sortCfg = array();
137         }
138
139         $r = new Elasticsearch_Request(
140             $this->baseUrl . 'document/_search',
141             \HTTP_Request2::METHOD_GET
142         );
143         $doc = array(
144             '_source' => array(
145                 'url',
146                 'title',
147                 'author',
148                 'modate',
149             ),
150             'query' => array(
151                 'bool' => array(
152                     'must' => $qMust
153                 )
154             ),
155             'highlight' => array(
156                 'pre_tags' => array('<em class="hl">'),
157                 'order' => 'score',
158                 'encoder' => 'html',
159                 'fields' => array(
160                     'title' => array(
161                         'require_field_match' => false,
162                         'number_of_fragments' => 0,
163                     ),
164                     'url' => array(
165                         'require_field_match' => false,
166                         'number_of_fragments' => 0,
167                     ),
168                     'text' => array(
169                         'require_field_match' => false,
170                         'number_of_fragments' => 1,
171                     ),
172                 )
173             ),
174             'aggregations' => array(
175                 'tags' => array(
176                     'terms' => array(
177                         'field' => 'tags'
178                     )
179                 ),
180                 'language' => array(
181                     'terms' => array(
182                         'field' => 'language'
183                     )
184                 ),
185                 'domain' => array(
186                     'terms' => array(
187                         'field' => 'domain'
188                     )
189                 ),
190                 'type' => array(
191                     'terms' => array(
192                         'field' => 'type'
193                     )
194                 )
195             ),
196             'from' => $page * $perPage,
197             'size' => $perPage,
198             'sort' => $sortCfg,
199         );
200         foreach ($filters as $type => $value) {
201             $doc['query']['bool']['must'][] = array(
202                 'term' => array(
203                     $type => $value
204                 )
205             );
206         }
207         if ($site != '') {
208             $doc['query']['bool']['must'][] = array(
209                 'prefix' => array(
210                     'schemalessUrl' => array(
211                         'value' => $site
212                     )
213                 )
214             );
215         }
216
217         //unset($doc['_source']);
218
219         //ini_set('xdebug.var_display_max_depth', 10);
220         //echo json_encode($doc);die();
221         $r->setBody(json_encode($doc));
222         $res = $r->send();
223         return json_decode($res->getBody());
224     }
225 }
226 ?>