96a769bc18319386eeec8ceef6003980f80220df
[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         if ($sort == 'date') {
83             $sortCfg = array('modate' => array('order' => 'desc'));
84         } else {
85             $sortCfg = array();
86         }
87
88         $r = new Elasticsearch_Request(
89             $this->baseUrl . 'document/_search',
90             \HTTP_Request2::METHOD_GET
91         );
92         $doc = array(
93             '_source' => array(
94                 'url',
95                 'title',
96                 'author',
97                 'modate',
98             ),
99             'query' => array(
100                 'bool' => array(
101                     'must' => array(
102                         array(
103                             'query_string' => array(
104                                 'default_field' => '_all',
105                                 'default_operator' => 'AND',
106                                 'query' => $query
107                             )
108                         ),
109                         array(
110                             'term' => array(
111                                 'status' => 'indexed'
112                             )
113                         ),
114                     )
115                 )
116             ),
117             'highlight' => array(
118                 'pre_tags' => array('<em class="hl">'),
119                 'order' => 'score',
120                 'encoder' => 'html',
121                 'fields' => array(
122                     'title' => array(
123                         'require_field_match' => false,
124                         'number_of_fragments' => 0,
125                     ),
126                     'url' => array(
127                         'require_field_match' => false,
128                         'number_of_fragments' => 0,
129                     ),
130                     'text' => array(
131                         'require_field_match' => false,
132                         'number_of_fragments' => 1,
133                     ),
134                 )
135             ),
136             'aggregations' => array(
137                 'tags' => array(
138                     'terms' => array(
139                         'field' => 'tags'
140                     )
141                 ),
142                 'language' => array(
143                     'terms' => array(
144                         'field' => 'language'
145                     )
146                 ),
147                 'domain' => array(
148                     'terms' => array(
149                         'field' => 'domain'
150                     )
151                 ),
152                 'type' => array(
153                     'terms' => array(
154                         'field' => 'type'
155                     )
156                 )
157             ),
158             'from' => $page * $perPage,
159             'size' => $perPage,
160             'sort' => $sortCfg,
161         );
162         foreach ($filters as $type => $value) {
163             $doc['query']['bool']['must'][] = array(
164                 'term' => array(
165                     $type => $value
166                 )
167             );
168         }
169         if ($site != '') {
170             $doc['query']['bool']['must'][] = array(
171                 'prefix' => array(
172                     'schemalessUrl' => array(
173                         'value' => $site
174                     )
175                 )
176             );
177         }
178
179         //unset($doc['_source']);
180
181         //ini_set('xdebug.var_display_max_depth', 10);
182         //echo json_encode($doc);die();
183         $r->setBody(json_encode($doc));
184         $res = $r->send();
185         return json_decode($res->getBody());
186     }
187 }
188 ?>