Skip to content

Commit

Permalink
JAMES-4100 Improve Search Snippet display
Browse files Browse the repository at this point in the history
  • Loading branch information
hung phan committed Jan 20, 2025
1 parent da112ef commit 5089a3c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -532,4 +532,43 @@ default void shouldHighLightBodyWhenHTMLBodyMatched() throws Exception {
softly.assertThat(searchSnippets.getFirst().highlightedBody().get()).contains("<mark>barcamp</mark>");
});
}

@Test
default void highlightSearchShouldShortenGreaterThanCharacters() throws Exception {
MailboxSession session = session(USERNAME1);

// Given m1,m2 with m1 has body containing the searched word (contentA)
ComposedMessageId m1 = appendMessage(MessageManager.AppendCommand.from(
Message.Builder.of()
.setTo("[email protected]")
.setSubject("Hallo, Thx Matthieu for your help")
.setBody("Start \n>>>>>>>>>> append contentA to > inbox \n>>>>>> End",
StandardCharsets.UTF_8)),
session).getId();

ComposedMessageId m2 = appendMessage(MessageManager.AppendCommand.from(
Message.Builder.of()
.setTo("[email protected]")
.setSubject("Hallo, Thx Alex for your help")
.setBody("append contentB to inbox", StandardCharsets.UTF_8)),
session).getId();

verifyMessageWasIndexed(2);

// When searching for the word (contentA) in the body
MultimailboxesSearchQuery multiMailboxSearch = MultimailboxesSearchQuery.from(SearchQuery.of(
SearchQuery.bodyContains("contentA")))
.inMailboxes(List.of(m1.getMailboxId(), m2.getMailboxId()))
.build();

// Then highlightSearch should return the SearchSnippet with the highlightedBody containing the word (contentA)
List<SearchSnippet> searchSnippets = Flux.from(testee().highlightSearch(List.of(m1.getMessageId(), m2.getMessageId()), multiMailboxSearch, session))
.collectList()
.block();
assertThat(searchSnippets).hasSize(1);
assertSoftly(softly -> {
softly.assertThat(searchSnippets.getFirst().messageId()).isEqualTo(m1.getMessageId());
softly.assertThat(searchSnippets.getFirst().highlightedBody().get()).isEqualTo("Start \n append <mark>contentA</mark> to > inbox \n End");
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ public Document createMessageDocument(MailboxMessage message, MailboxSession ses
doc.add(new TextField(BCC_FIELD, uppercase(EMailers.from(headerCollection.getBccAddressSet()).serialize()), Field.Store.YES));

// index body
Optional<String> bodyText = mimePartExtracted.locateFirstTextBody();
Optional<String> bodyHtml = mimePartExtracted.locateFirstHtmlBody();
Optional<String> bodyText = mimePartExtracted.locateFirstTextBody().map(SearchUtil::removeGreaterThanCharacters);
Optional<String> bodyHtml = mimePartExtracted.locateFirstHtmlBody().map(SearchUtil::removeGreaterThanCharacters);

bodyText.or(() -> bodyHtml)
.ifPresent(bodyContent -> doc.add(new TextField(BODY_FIELD, bodyContent, Field.Store.YES)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ private Mono<IndexableMessage> instantiateIndexedMessage() throws IOException, M
.asMimePart(textExtractor)
.map(parsingResult -> {

Optional<String> bodyText = parsingResult.locateFirstTextBody();
Optional<String> bodyHtml = parsingResult.locateFirstHtmlBody();
Optional<String> bodyText = parsingResult.locateFirstTextBody().map(SearchUtil::removeGreaterThanCharacters);
Optional<String> bodyHtml = parsingResult.locateFirstHtmlBody().map(SearchUtil::removeGreaterThanCharacters);

boolean hasAttachment = MessageAttachmentMetadata.hasNonInlinedAttachment(message.getAttachments());
List<MimePart> attachments = setFlattenedAttachments(parsingResult, indexAttachments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,5 +473,22 @@ public boolean test(MessageId input) {
};
}

public static String removeGreaterThanCharacters(String text) {
StringBuilder result = new StringBuilder();
boolean isNewLine = false;

for (int i = 0; i < text.length(); i++) {
char current = text.charAt(i);

if (current == '\n') {
isNewLine = true;
result.append(current);
} else if (!isNewLine || current != '>') {
result.append(current);
isNewLine = false;
}
}

return result.toString();
}
}

0 comments on commit 5089a3c

Please sign in to comment.