3 Commits

Author SHA1 Message Date
  Chimo 66bd973fb2 Index both notice 'verb' and 'type' 1 year ago
  Chimo 25cd3e1b13 Handle "no results" 1 year ago
  Chimo 88d4bfb779 Some error handling 1 year ago
1 changed files with 37 additions and 13 deletions
  1. 37
    13
      lib/ElasticSearch.php

+ 37
- 13
lib/ElasticSearch.php View File

@@ -43,6 +43,9 @@ class ElasticSearch extends SearchEngine
43 43
                     'text' => [
44 44
                         'type' => 'string'
45 45
                     ],
46
+                    'verb' => [
47
+                        'type' => 'string'
48
+                    ],
46 49
                     'type' => [
47 50
                         'type' => 'string'
48 51
                     ],
@@ -87,9 +90,14 @@ class ElasticSearch extends SearchEngine
87 90
                 ]
88 91
             ];
89 92
 
90
-            $response = $this->client->indices()->create($params);
91
-
92
-            // TODO: Parse response, handle errors
93
+            try {
94
+                $this->client->indices()->create($params);
95
+            } catch(Exception $e) {
96
+                common_log(
97
+                    LOG_ERROR,
98
+                    "Unable to create index existing $this->index_name: $e->getMessage()"
99
+                );
100
+            }
93 101
         }
94 102
     }
95 103
 
@@ -154,6 +162,17 @@ class ElasticSearch extends SearchEngine
154 162
         $author = Profile::getKV('id', $notice->profile_id);
155 163
         $webfinger = $author->getAcctUri(false);
156 164
 
165
+        try {
166
+            $object_type = $notice->getObjectType();
167
+        } catch(NoObjectTypeException $e) {
168
+            common_log(
169
+                LOG_INFO,
170
+                "Notice $notice->getID() doesn't have an object_type"
171
+            );
172
+
173
+            $object_type = null;
174
+        }
175
+
157 176
         $params = [
158 177
             'index' => $this->index_name,
159 178
             'type' => $this->index_type,
@@ -161,7 +180,8 @@ class ElasticSearch extends SearchEngine
161 180
             'body' => [
162 181
                 'author' => $webfinger,
163 182
                 'text' => $notice->content,
164
-                'type' => $notice->getVerb(true),
183
+                'verb' => $notice->getVerb(true),
184
+                'type' => $object_type,
165 185
                 'created' => $notice->created
166 186
             ]
167 187
         ];
@@ -221,19 +241,23 @@ class ElasticSearch extends SearchEngine
221 241
         $hits = $response['hits']['hits'];
222 242
 
223 243
         if (count($hits) === 0) {
224
-            return false;
225
-        }
244
+            // No results
245
+            //
246
+            // Force empty result set because if we don't we end up
247
+            // displaying the most recent notices (no WHERE clause)
248
+            $this->target->whereAdd("1 = 2");
249
+        } else {
250
+            $ids = array();
251
+
252
+            foreach($hits as $hit) {
253
+                $ids[] = $hit['_id'];
254
+            }
226 255
 
227
-        $ids = array();
256
+            $id_set = join(', ', $ids);
228 257
 
229
-        foreach($hits as $hit) {
230
-            $ids[] = $hit['_id'];
258
+            $this->target->whereAdd("id in ($id_set)");
231 259
         }
232 260
 
233
-        $id_set = join(', ', $ids);
234
-
235
-        $this->target->whereAdd("id in ($id_set)");
236
-
237 261
         return true;
238 262
     }
239 263
 

Loading…
Cancel
Save