aboutsummaryrefslogtreecommitdiff
path: root/src/phinde/Elasticsearch.php
blob: 310b63bd1538009083e5287cfd0a23dd0f99c5ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
namespace phinde;

class Elasticsearch
{
    protected $baseUrl;

    public function __construct($baseUrl)
    {
        $this->baseUrl = $baseUrl;
    }

    /**
     * @link https://www.elastic.co/guide/en/elasticsearch/guide/current/_finding_exact_values.html
     */
    public function isKnown($url)
    {
        $r = new Elasticsearch_Request(
            $this->baseUrl . 'document/_search/exists',
            \HTTP_Request2::METHOD_GET
        );
        $r->allow404 = true;
        $r->setBody(
            json_encode(
                array(
                    'query' => array(
                        'filtered' => array(
                            'filter' => array(
                                'term' => array(
                                    'url' => $url
                                )
                            )
                        )
                    )
                )
            )
        );
        $res = json_decode($r->send()->getBody());
        return $res->exists;
    }

    public function get($url)
    {
        $r = new Elasticsearch_Request(
            $this->baseUrl . 'document/' . rawurlencode($url),
            \HTTP_Request2::METHOD_GET
        );
        $r->allow404 = true;
        $res = $r->send();
        if ($res->getStatus() != 200) {
            return null;
        }
        $d = json_decode($res->getBody());
        return $d->_source;
    }

    public function markQueued($url)
    {
        $r = new Elasticsearch_Request(
            $this->baseUrl . 'document/' . rawurlencode($url),
            \HTTP_Request2::METHOD_PUT
        );
        $doc = array(
            'status' => 'queued',
            'url' => $url
        );
        $r->setBody(json_encode($doc));
        $r->send();
    }

    public function search($query, $filters, $site, $page, $perPage, $sort)
    {
        if ($sort == 'date') {
            $sortCfg = array('modate' => array('order' => 'desc'));
        } else {
            $sortCfg = array();
        }

        $r = new Elasticsearch_Request(
            $this->baseUrl . 'document/_search',
            \HTTP_Request2::METHOD_GET
        );
        $doc = array(
            '_source' => array(
                'url',
                'title',
                'author',
                'modate',
            ),
            'query' => array(
                'bool' => array(
                    'must' => array(
                        array(
                            'query_string' => array(
                                'default_field' => '_all',
                                'default_operator' => 'AND',
                                'query' => $query
                            )
                        ),
                        array(
                            'term' => array(
                                'status' => 'indexed'
                            )
                        ),
                    )
                )
            ),
            'highlight' => array(
                'pre_tags' => array('<em class="hl">'),
                'order' => 'score',
                'encoder' => 'html',
                'fields' => array(
                    'title' => array(
                        'require_field_match' => false,
                        'number_of_fragments' => 0,
                    ),
                    'url' => array(
                        'require_field_match' => false,
                        'number_of_fragments' => 0,
                    ),
                    'text' => array(
                        'require_field_match' => false,
                        'number_of_fragments' => 1,
                    ),
                )
            ),
            'aggregations' => array(
                'tags' => array(
                    'terms' => array(
                        'field' => 'tags'
                    )
                ),
                'language' => array(
                    'terms' => array(
                        'field' => 'language'
                    )
                ),
                'domain' => array(
                    'terms' => array(
                        'field' => 'domain'
                    )
                ),
                'type' => array(
                    'terms' => array(
                        'field' => 'type'
                    )
                )
            ),
            'from' => $page * $perPage,
            'size' => $perPage,
            'sort' => $sortCfg,
        );
        foreach ($filters as $type => $value) {
            $doc['query']['bool']['must'][] = array(
                'term' => array(
                    $type => $value
                )
            );
        }
        if ($site != '') {
            $doc['query']['bool']['must'][] = array(
                'prefix' => array(
                    'schemalessUrl' => array(
                        'value' => $site
                    )
                )
            );
        }

        //unset($doc['_source']);

        //ini_set('xdebug.var_display_max_depth', 10);
        //echo json_encode($doc);die();
        $r->setBody(json_encode($doc));
        $res = $r->send();
        return json_decode($res->getBody());
    }
}
?>