From 1a1c726c790a8a4049b51a828948488888ff064e Mon Sep 17 00:00:00 2001 From: Aitor Algorta Date: Wed, 25 Sep 2024 12:11:46 +0200 Subject: [PATCH] [#4180] Lucene crashes with some specific messages (#4181) * wip * remove unncessary logs --- .../communication/lucene/DocumentMapper.java | 89 ++++++++++++++----- .../communication/lucene/LuceneProvider.java | 6 +- 2 files changed, 72 insertions(+), 23 deletions(-) diff --git a/backend/components/communication/src/main/java/co/airy/core/communication/lucene/DocumentMapper.java b/backend/components/communication/src/main/java/co/airy/core/communication/lucene/DocumentMapper.java index fb14b60a6e..cb0ccc3a40 100644 --- a/backend/components/communication/src/main/java/co/airy/core/communication/lucene/DocumentMapper.java +++ b/backend/components/communication/src/main/java/co/airy/core/communication/lucene/DocumentMapper.java @@ -11,41 +11,88 @@ import org.apache.lucene.document.StringField; import org.apache.lucene.document.TextField; import org.apache.lucene.index.IndexableField; +import co.airy.log.AiryLoggerFactory; +import org.slf4j.Logger; import java.util.List; import static java.util.stream.Collectors.toList; public class DocumentMapper { + + private static final Logger log = AiryLoggerFactory.getLogger(DocumentMapper.class); + public Document fromConversationIndex(ConversationIndex conversation) { - final Document document = new Document(); - document.add(new StringField("id", conversation.getId(), Field.Store.YES)); - document.add(new StringField("channel_id", conversation.getChannelId(), Field.Store.YES)); + final Document document = new Document(); - if (conversation.getDisplayName() != null) { + if (conversation.getId() != null) { + document.add(new StringField("id", conversation.getId(), Field.Store.YES)); + } else { + log.error("conversation.getId() is null"); + } + + if (conversation.getChannelId() != null) { + document.add(new StringField("channel_id", conversation.getChannelId(), Field.Store.YES)); + } else { + log.error("conversation.getChannelId() is null"); + } + + if (conversation.getDisplayName() != null) { document.add(new TextField("display_name", conversation.getDisplayName(), Field.Store.YES)); + } else { + log.error("conversation.getDisplayName() is null"); } - document.add(new StringField("source", conversation.getSource(), Field.Store.YES)); - - document.add(new LongPoint("created_at", conversation.getCreatedAt())); - document.add(new StoredField("created_at", conversation.getCreatedAt())); - document.add(new IntPoint("unread_count", conversation.getUnreadMessageCount())); - document.add(new StoredField("unread_count", conversation.getUnreadMessageCount())); - - // sort enabled field - document.add(new NumericDocValuesField("last_message_at", conversation.getLastMessageAt())); - for (String tagId : conversation.getTagIds()) { - document.add(new TextField("tag_ids", tagId, Field.Store.YES)); + + if (conversation.getSource() != null) { + document.add(new StringField("source", conversation.getSource(), Field.Store.YES)); + } else { + log.error("conversation.getSource() is null"); } - - for (MetadataNode node : conversation.getMetadata()) { - final String key = String.format("metadata.%s", node.getKey()); - // Index but don't store metadata - document.add(new TextField(key, node.getValue(), Field.Store.NO)); + + if (conversation.getCreatedAt() != null) { + document.add(new LongPoint("created_at", conversation.getCreatedAt())); + document.add(new StoredField("created_at", conversation.getCreatedAt())); + } else { + log.error("conversation.getCreatedAt() is null"); } - + + if (conversation.getUnreadMessageCount() != null) { + document.add(new IntPoint("unread_count", conversation.getUnreadMessageCount())); + document.add(new StoredField("unread_count", conversation.getUnreadMessageCount())); + } else { + log.error("conversation.getUnreadMessageCount() is null"); + } + + if (conversation.getLastMessageAt() != null) { + document.add(new NumericDocValuesField("last_message_at", conversation.getLastMessageAt())); + } else { + log.error("conversation.getLastMessageAt() is null"); + } + + if (conversation.getTagIds() != null) { + for (String tagId : conversation.getTagIds()) { + document.add(new TextField("tag_ids", tagId, Field.Store.YES)); + } + } else { + log.error("conversation.getTagIds() is null"); + } + + if (conversation.getMetadata() != null) { + for (MetadataNode node : conversation.getMetadata()) { + final String key = String.format("metadata.%s", node.getKey()); + if (node.getValue() != null) { + document.add(new TextField(key, node.getValue(), Field.Store.NO)); + } else { + log.error("Metadata value for key \" + node.getKey() + \" is null"); + } + } + } else { + log.error("conversation.getMetadata() is null"); + } + return document; } + public ConversationIndex fromDocument(Document document) { final Long createdAt = document.getField("created_at").numericValue().longValue(); diff --git a/backend/components/communication/src/main/java/co/airy/core/communication/lucene/LuceneProvider.java b/backend/components/communication/src/main/java/co/airy/core/communication/lucene/LuceneProvider.java index 82629e43fa..d128f8c7e4 100644 --- a/backend/components/communication/src/main/java/co/airy/core/communication/lucene/LuceneProvider.java +++ b/backend/components/communication/src/main/java/co/airy/core/communication/lucene/LuceneProvider.java @@ -45,8 +45,10 @@ public LuceneProvider() throws IOException { @Override public void put(ConversationIndex conversation) throws IOException { - final Document document = this.documentMapper.fromConversationIndex(conversation); - writer.updateDocument(new Term("id", conversation.getId()), document); + if (conversation != null) { + final Document document = this.documentMapper.fromConversationIndex(conversation); + writer.updateDocument(new Term("id", conversation.getId()), document); + } } @Override