From b4e2849091e1c0e1e39c8544eb4208bdd812ff4c Mon Sep 17 00:00:00 2001 From: Aitor Algorta Date: Wed, 25 Sep 2024 11:06:12 +0200 Subject: [PATCH 1/2] wip --- .../communication/lucene/DocumentMapper.java | 99 +++++++++++++++---- .../communication/lucene/LuceneProvider.java | 6 +- 2 files changed, 83 insertions(+), 22 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..5856a1d862 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,100 @@ 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.getId() != null) { + log.error("1"); + document.add(new StringField("id", conversation.getId(), Field.Store.YES)); + } else { + log.error("conversation.getId() is null"); + } + + if (conversation.getChannelId() != null) { + log.error("2"); + document.add(new StringField("channel_id", conversation.getChannelId(), Field.Store.YES)); + } else { + log.error("conversation.getChannelId() is null"); + } + if (conversation.getDisplayName() != null) { + log.error("3"); 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) { + log.error("4"); + 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) { + log.error("5"); + 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) { + log.error("6"); + 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) { + log.error("7"); + document.add(new NumericDocValuesField("last_message_at", conversation.getLastMessageAt())); + } else { + log.error("conversation.getLastMessageAt() is null"); + } + + if (conversation.getTagIds() != null) { + log.error("8"); + 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) { + log.error("9"); + for (MetadataNode node : conversation.getMetadata()) { + log.error("10"); + final String key = String.format("metadata.%s", node.getKey()); + if (node.getValue() != null) { + log.error("11"); + document.add(new TextField(key, node.getValue(), Field.Store.NO)); + } else { + log.error("12"); + 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 From 736636b039a4f008f29cc3054dbc7b01148f5d87 Mon Sep 17 00:00:00 2001 From: Aitor Algorta Date: Wed, 25 Sep 2024 11:09:06 +0200 Subject: [PATCH 2/2] remove unncessary logs --- .../communication/lucene/DocumentMapper.java | 32 ++++++------------- 1 file changed, 10 insertions(+), 22 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 5856a1d862..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 @@ -26,58 +26,50 @@ public Document fromConversationIndex(ConversationIndex conversation) { final Document document = new Document(); if (conversation.getId() != null) { - log.error("1"); document.add(new StringField("id", conversation.getId(), Field.Store.YES)); } else { log.error("conversation.getId() is null"); } if (conversation.getChannelId() != null) { - log.error("2"); document.add(new StringField("channel_id", conversation.getChannelId(), Field.Store.YES)); } else { log.error("conversation.getChannelId() is null"); } - if (conversation.getDisplayName() != null) { - log.error("3"); + if (conversation.getDisplayName() != null) { document.add(new TextField("display_name", conversation.getDisplayName(), Field.Store.YES)); } else { log.error("conversation.getDisplayName() is null"); } - if (conversation.getSource() != null) { - log.error("4"); + if (conversation.getSource() != null) { document.add(new StringField("source", conversation.getSource(), Field.Store.YES)); } else { log.error("conversation.getSource() is null"); } - if (conversation.getCreatedAt() != null) { - log.error("5"); + 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) { - log.error("6"); + 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) { - log.error("7"); + if (conversation.getLastMessageAt() != null) { document.add(new NumericDocValuesField("last_message_at", conversation.getLastMessageAt())); } else { log.error("conversation.getLastMessageAt() is null"); } - if (conversation.getTagIds() != null) { - log.error("8"); + if (conversation.getTagIds() != null) { for (String tagId : conversation.getTagIds()) { document.add(new TextField("tag_ids", tagId, Field.Store.YES)); } @@ -85,16 +77,12 @@ public Document fromConversationIndex(ConversationIndex conversation) { log.error("conversation.getTagIds() is null"); } - if (conversation.getMetadata() != null) { - log.error("9"); - for (MetadataNode node : conversation.getMetadata()) { - log.error("10"); + if (conversation.getMetadata() != null) { + for (MetadataNode node : conversation.getMetadata()) { final String key = String.format("metadata.%s", node.getKey()); - if (node.getValue() != null) { - log.error("11"); + if (node.getValue() != null) { document.add(new TextField(key, node.getValue(), Field.Store.NO)); - } else { - log.error("12"); + } else { log.error("Metadata value for key \" + node.getKey() + \" is null"); } }