Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
AitorAlgorta committed Sep 25, 2024
1 parent ff62f6e commit b4e2849
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
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 b4e2849

Please sign in to comment.