From 2443e65d35a34d15fe6b8df6cdd7b538fd9ec579 Mon Sep 17 00:00:00 2001 From: Enrico Risa Date: Wed, 12 Feb 2020 09:54:06 +0100 Subject: [PATCH] Fixes https://github.com/orientechnologies/orientdb/issues/9134 --- .../lucene/collections/OLuceneResultSet.java | 8 ++-- .../OLuceneSearchOnClassFunctionTest.java | 48 +++++++++++-------- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/lucene/src/main/java/com/orientechnologies/lucene/collections/OLuceneResultSet.java b/lucene/src/main/java/com/orientechnologies/lucene/collections/OLuceneResultSet.java index 4f149d6b1bd..2e9a6e56116 100755 --- a/lucene/src/main/java/com/orientechnologies/lucene/collections/OLuceneResultSet.java +++ b/lucene/src/main/java/com/orientechnologies/lucene/collections/OLuceneResultSet.java @@ -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); diff --git a/lucene/src/test/java/com/orientechnologies/lucene/functions/OLuceneSearchOnClassFunctionTest.java b/lucene/src/test/java/com/orientechnologies/lucene/functions/OLuceneSearchOnClassFunctionTest.java index c7b97aa2d60..1437b1f0cd9 100644 --- a/lucene/src/test/java/com/orientechnologies/lucene/functions/OLuceneSearchOnClassFunctionTest.java +++ b/lucene/src/test/java/com/orientechnologies/lucene/functions/OLuceneSearchOnClassFunctionTest.java @@ -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); @@ -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); @@ -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(); @@ -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(); @@ -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(); } @@ -85,9 +79,7 @@ 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(); } @@ -95,15 +87,31 @@ public void shouldThrowExceptionIfMoreIndexesAreDefined() { @Test public void shouldHighlightTitle() throws Exception { - OResultSet resultSet = db.query( - "SELECT title, $title_hl from Song where SEARCH_CLASS('believe', {" - + "highlight: { fields: ['title'], 'start': '', 'end': '' } }) = true "); + OResultSet resultSet = db.query("SELECT title, $title_hl from Song where SEARCH_CLASS('believe', {" + + "highlight: { fields: ['title'], 'start': '', 'end': '' } }) = true "); - resultSet.stream() - .forEach(r -> assertThat(r.getProperty("$title_hl")).containsIgnoringCase("believe")); + resultSet.stream().forEach(r -> assertThat(r.getProperty("$title_hl")).containsIgnoringCase("believe")); 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': '', 'end': '' } }) = true "); + + resultSet.stream().forEach(r -> assertThat(r.getProperty("$description_hl")) + .containsIgnoringCase("shouldHighlightWithNullValues")); + resultSet.close(); + + } }