8 public function __construct($baseUrl)
10 $this->baseUrl = $baseUrl;
13 public static function getDocId($url)
15 return hash('sha256', $url);
18 public function isKnown($url)
20 $r = new Elasticsearch_Request(
21 $this->baseUrl . 'document/' . static::getDocId($url),
22 \HTTP_Request2::METHOD_HEAD
26 return $res->getStatus() == 200;
29 public function get($url)
31 $r = new Elasticsearch_Request(
32 $this->baseUrl . 'document/' . static::getDocId($url),
33 \HTTP_Request2::METHOD_GET
37 if ($res->getStatus() != 200) {
40 $d = json_decode($res->getBody());
44 public function markQueued($url)
46 $r = new Elasticsearch_Request(
47 $this->baseUrl . 'document/' . static::getDocId($url),
48 \HTTP_Request2::METHOD_PUT
50 $doc = (object) array(
52 'status' => (object) array(
57 $r->setBody(json_encode($doc));
61 public function getIndexStatus()
63 $r = new Elasticsearch_Request(
64 $this->baseUrl . '_stats/docs,store',
65 \HTTP_Request2::METHOD_GET
68 $data = json_decode($res->getBody());
70 'documents' => $data->_all->total->docs->count,
71 'size' => $data->_all->total->store->size_in_bytes,
75 public function search($query, $filters, $site, $page, $perPage, $sort)
77 if (preg_match_all('#nick:([^ ]*)#', $query, $matches)) {
78 foreach ($matches[1] as $authorName) {
80 'nick:' . $authorName,
81 'author.name:' . $authorName,
87 $qMust = array();//query parts for the MUST section
89 //modification date filters
90 if (preg_match('#after:([^ ]+)#', $query, $matches)) {
91 $dateAfter = $matches[1];
92 $query = trim(str_replace($matches[0], '', $query));
95 'status.modate' => array(
96 'gt' => $dateAfter . '||/d',
101 if (preg_match('#before:([^ ]+)#', $query, $matches)) {
102 $dateBefore = $matches[1];
103 $query = trim(str_replace($matches[0], '', $query));
106 'status.modate' => array(
107 'lt' => $dateBefore . '||/d',
112 if (preg_match('#date:([^ ]+)#', $query, $matches)) {
113 $dateExact = $matches[1];
114 $query = trim(str_replace($matches[0], '', $query));
117 'status.modate' => array(
118 'gte' => $dateExact . '||/d',
119 'lte' => $dateExact . '||/d',
125 if (strpos($query, '/') !== false && strpos($query, '"') === false) {
126 //add quotes when there is a slash and no quotes
127 // https://stackoverflow.com/questions/31963643/escaping-forward-slashes-in-elasticsearch
128 $query = '"' . $query . '"';
131 'query_string' => array(
132 'default_field' => '_all',
133 'default_operator' => 'AND',
139 'status.findable' => true
143 if ($sort == '' && $GLOBALS['phinde']['defaultSort'] == 'date') {
146 if ($sort == 'date') {
147 $sortCfg = array('status.modate' => array('order' => 'desc'));
152 $contentMatchSize = 100;
153 if ($GLOBALS['phinde']['showFullContent']) {
154 $contentMatchSize = 999999;
157 $r = new Elasticsearch_Request(
158 $this->baseUrl . 'document/_search',
159 \HTTP_Request2::METHOD_GET
173 'highlight' => array(
174 'pre_tags' => array('<em class="hl">'),
179 'require_field_match' => false,
180 'number_of_fragments' => 0,
183 'require_field_match' => false,
184 'number_of_fragments' => 0,
187 'require_field_match' => false,
188 'number_of_fragments' => 1,
189 'fragment_size' => $contentMatchSize,
190 'no_match_size' => $contentMatchSize,
194 'aggregations' => array(
202 'field' => 'language'
216 'from' => $page * $perPage,
220 foreach ($filters as $type => $value) {
221 $doc['query']['bool']['must'][] = array(
228 $doc['query']['bool']['must'][] = array(
230 'schemalessUrl' => array(
237 //unset($doc['_source']);
239 //ini_set('xdebug.var_display_max_depth', 10);
240 //echo json_encode($doc);die();
241 $r->setBody(json_encode($doc));
243 return json_decode($res->getBody());