Skip to content

Commit

Permalink
Fixes #9134
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed Feb 12, 2020
1 parent 85c002c commit 2443e65
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,11 @@ private OContextualRecordId toRecordId(Document doc, ScoreDoc score) {

for (String field : highlighted) {
String text = doc.get(field);
TokenStream tokenStream = TokenSources.getAnyTokenStream(indexReader, score.doc, field, doc, engine.indexAnalyzer());
TextFragment[] frag = highlighter.getBestTextFragments(tokenStream, text, true, maxNumFragments);
queryContext.addHighlightFragment(field, frag);
if(text !=null) {
TokenStream tokenStream = TokenSources.getAnyTokenStream(indexReader, score.doc, field, doc, engine.indexAnalyzer());
TextFragment[] frag = highlighter.getBestTextFragments(tokenStream, text, true, maxNumFragments);
queryContext.addHighlightFragment(field, frag);
}
}

engine.onRecordAddedToResultSet(queryContext, res, doc, score);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public void setUp() throws Exception {
@Test
public void shouldSearchOnClass() throws Exception {

OResultSet resultSet = db
.query("SELECT from Song where SEARCH_Class('BELIEVE') = true");
OResultSet resultSet = db.query("SELECT from Song where SEARCH_Class('BELIEVE') = true");

assertThat(resultSet).hasSize(2);

Expand All @@ -39,8 +38,7 @@ public void shouldSearchOnClass() throws Exception {
@Test
public void shouldSearchOnSingleFieldWithLeadingWildcard() throws Exception {

OResultSet resultSet = db
.query("SELECT from Song where SEARCH_CLASS( '*EVE*', {'allowLeadingWildcard': true}) = true");
OResultSet resultSet = db.query("SELECT from Song where SEARCH_CLASS( '*EVE*', {'allowLeadingWildcard': true}) = true");

assertThat(resultSet).hasSize(14);

Expand All @@ -50,8 +48,7 @@ public void shouldSearchOnSingleFieldWithLeadingWildcard() throws Exception {
@Test
public void shouldSearchInOr() throws Exception {

OResultSet resultSet = db
.query("SELECT from Song where SEARCH_CLASS('BELIEVE') = true OR SEARCH_CLASS('GOODNIGHT') = true ");
OResultSet resultSet = db.query("SELECT from Song where SEARCH_CLASS('BELIEVE') = true OR SEARCH_CLASS('GOODNIGHT') = true ");

assertThat(resultSet).hasSize(5);
resultSet.close();
Expand All @@ -61,9 +58,8 @@ public void shouldSearchInOr() throws Exception {
@Test
public void shouldSearchInAnd() throws Exception {

OResultSet resultSet = db
.query(
"SELECT from Song where SEARCH_CLASS('GOODNIGHT') = true AND SEARCH_CLASS( 'Irene', {'allowLeadingWildcard': true}) = true ");
OResultSet resultSet = db.query(
"SELECT from Song where SEARCH_CLASS('GOODNIGHT') = true AND SEARCH_CLASS( 'Irene', {'allowLeadingWildcard': true}) = true ");

assertThat(resultSet).hasSize(1);
resultSet.close();
Expand All @@ -73,9 +69,7 @@ public void shouldSearchInAnd() throws Exception {
@Test(expected = OCommandExecutionException.class)
public void shouldThrowExceptionWithWrongClass() throws Exception {

OResultSet resultSet = db
.query(
"SELECT from Author where SEARCH_CLASS('(description:happiness) (lyrics:sad) ') = true ");
OResultSet resultSet = db.query("SELECT from Author where SEARCH_CLASS('(description:happiness) (lyrics:sad) ') = true ");
resultSet.close();

}
Expand All @@ -85,25 +79,39 @@ public void shouldThrowExceptionIfMoreIndexesAreDefined() {

db.command("create index Song.author on Song (author) FULLTEXT ENGINE LUCENE ");

OResultSet resultSet = db
.query(
"SELECT from Song where SEARCH_CLASS('not important, will fail') = true ");
OResultSet resultSet = db.query("SELECT from Song where SEARCH_CLASS('not important, will fail') = true ");
resultSet.close();

}

@Test
public void shouldHighlightTitle() throws Exception {

OResultSet resultSet = db.query(
"SELECT title, $title_hl from Song where SEARCH_CLASS('believe', {"
+ "highlight: { fields: ['title'], 'start': '<span>', 'end': '</span>' } }) = true ");
OResultSet resultSet = db.query("SELECT title, $title_hl from Song where SEARCH_CLASS('believe', {"
+ "highlight: { fields: ['title'], 'start': '<span>', 'end': '</span>' } }) = true ");

resultSet.stream()
.forEach(r -> assertThat(r.<String>getProperty("$title_hl")).containsIgnoringCase("<span>believe</span>"));
resultSet.stream().forEach(r -> assertThat(r.<String>getProperty("$title_hl")).containsIgnoringCase("<span>believe</span>"));
resultSet.close();

}

@Test
public void shouldHighlightWithNullValues() throws Exception {

db.command("drop index Song.title");

db.command("create index Song.title_description on Song (title,description) FULLTEXT ENGINE LUCENE ");

db.command("insert into Song set description = 'shouldHighlightWithNullValues'");

OResultSet resultSet = db.query(
"SELECT title, $title_hl,description, $description_hl from Song where SEARCH_CLASS('shouldHighlightWithNullValues', {"
+ "highlight: { fields: ['title','description'], 'start': '<span>', 'end': '</span>' } }) = true ");

resultSet.stream().forEach(r -> assertThat(r.<String>getProperty("$description_hl"))
.containsIgnoringCase("<span>shouldHighlightWithNullValues</span>"));
resultSet.close();

}

}

0 comments on commit 2443e65

Please sign in to comment.