first frontend
[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, $page, $perPage)
72     {
73         $r = new Elasticsearch_Request(
74             $this->baseUrl . 'document/_search',
75             \HTTP_Request2::METHOD_GET
76         );
77         $doc = array(
78             '_source' => array(
79                 'url',
80                 'title',
81                 'author',
82                 'modate',
83             ),
84             'query' => array(
85                 'bool' => array(
86                     'must' => array(
87                         array(
88                             'query_string' => array(
89                                 'default_field' => '_all',
90                                 'query' => $query
91                             )
92                         ),
93                         array(
94                             'term' => array(
95                                 'status' => 'indexed'
96                             )
97                         ),
98                     )
99                 )
100             ),
101             'aggregations' => array(
102                 'tags' => array(
103                     'terms' => array(
104                         'field' => 'tags'
105                     )
106                 ),
107                 'language' => array(
108                     'terms' => array(
109                         'field' => 'language'
110                     )
111                 ),
112                 'domain' => array(
113                     'terms' => array(
114                         'field' => 'domain'
115                     )
116                 ),
117                 'type' => array(
118                     'terms' => array(
119                         'field' => 'type'
120                     )
121                 )
122             ),
123             'from' => $page * $perPage,
124             'size' => $perPage,
125             'sort' => array(
126                 //array('modate' => array('order' => 'desc'))
127             )
128         );
129         foreach ($filters as $type => $value) {
130             $doc['query']['bool']['must'][] = array(
131                 'term' => array(
132                     $type => $value
133                 )
134             );
135         }
136
137         //unset($doc['_source']);
138
139         //ini_set('xdebug.var_display_max_depth', 10);
140         //return json_decode(json_encode($doc));
141         $r->setBody(json_encode($doc));
142         $res = $r->send();
143         return json_decode($res->getBody());
144     }
145 }
146 ?>