Skip to content

Commit

Permalink
[#4180] Lucene crashes with some specific messages (#4181)
Browse files Browse the repository at this point in the history
* wip

* remove unncessary logs
  • Loading branch information
AitorAlgorta authored Sep 25, 2024
1 parent ff62f6e commit 1a1c726
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1a1c726

Please sign in to comment.