|
14 | 14 |
|
15 | 15 | use std::{fmt, fs, path::Path, sync::Arc};
|
16 | 16 |
|
17 |
| -use ruma::{OwnedEventId, OwnedRoomId, RoomId, events::AnySyncMessageLikeEvent}; |
| 17 | +use ruma::{EventId, OwnedEventId, OwnedRoomId, RoomId, events::AnySyncMessageLikeEvent}; |
18 | 18 | use tantivy::{
|
19 | 19 | Index, IndexReader, TantivyDocument,
|
20 | 20 | collector::TopDocs,
|
21 | 21 | directory::{MmapDirectory, error::OpenDirectoryError},
|
22 | 22 | query::QueryParser,
|
23 | 23 | schema::Value,
|
24 | 24 | };
|
25 |
| -use tracing::error; |
| 25 | +use tracing::{error, warn}; |
26 | 26 |
|
27 | 27 | use crate::{
|
28 | 28 | OpStamp, TANTIVY_INDEX_MEMORY_BUDGET,
|
@@ -129,8 +129,14 @@ impl RoomIndex {
|
129 | 129 | /// This which will add/remove/edit an event in the index based on the
|
130 | 130 | /// event type.
|
131 | 131 | pub fn handle_event(&mut self, event: AnySyncMessageLikeEvent) -> Result<(), IndexError> {
|
| 132 | + let event_id = event.event_id().to_owned(); |
| 133 | + |
132 | 134 | match self.schema.handle_event(event)? {
|
133 |
| - RoomIndexOperation::Add(document) => self.writer.add_document(document)?, |
| 135 | + RoomIndexOperation::Add(document) => { |
| 136 | + if !self.contains(&event_id) { |
| 137 | + self.writer.add_document(document)?; |
| 138 | + } |
| 139 | + } |
134 | 140 | };
|
135 | 141 | Ok(())
|
136 | 142 | }
|
@@ -184,6 +190,18 @@ impl RoomIndex {
|
184 | 190 |
|
185 | 191 | Ok(ret)
|
186 | 192 | }
|
| 193 | + |
| 194 | + fn contains(&self, event_id: &EventId) -> bool { |
| 195 | + let search_result = self.search(format!("event_id:\"{}\"", event_id).as_str(), 5); |
| 196 | + match search_result { |
| 197 | + // If there are > 1 entry (which there shouldn't be), still return true |
| 198 | + Ok(results) => results.len() != 0, |
| 199 | + Err(err) => { |
| 200 | + warn!("Failed to check if event has been indexed, assuming it has: {err:?}"); |
| 201 | + true |
| 202 | + } |
| 203 | + } |
| 204 | + } |
187 | 205 | }
|
188 | 206 |
|
189 | 207 | #[cfg(test)]
|
|
0 commit comments