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