aboutsummaryrefslogtreecommitdiff
path: root/src/phinde/Elasticsearch.php
blob: 4bc4637440e9a103f39a5e086bde7789612e1b07 (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
<?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, $page, $perPage)
    {
        $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',
                                'query' => $query
                            )
                        ),
                        array(
                            'term' => array(
                                'status' => 'indexed'
                            )
                        ),
                    )
                )
            ),
            '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' => array(
                //array('modate' => array('order' => 'desc'))
            )
        );
        foreach ($filters as $type => $value) {
            $doc['query']['bool']['must'][] = array(
                'term' => array(
                    $type => $value
                )
            );
        }

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

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