Skip to content

Commit

Permalink
Avoid NPE in more_like_this when field has zero tokens (#30365)
Browse files Browse the repository at this point in the history
Fixes and edge case when using `more_like_this` where TermVectorsWriter
could throw an NPE when a field produced zero tokens after analysis. This
changes the implementation to use an empty list of tokens in this case.

Closes #30148
  • Loading branch information
aditya-agrawal authored and Christoph Büscher committed May 8, 2018
1 parent 955667c commit 5939cd4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ searching ({pull}30338[#30338])
[float]
=== Bug Fixes

Fix NPE in 'more_like_this' when field has zero tokens ({pull}30365[#30365])

Do not ignore request analysis/similarity settings on index resize operations when the source index already contains such settings ({pull}30216[#30216])

Fix NPE when CumulativeSum agg encounters null value/empty bucket ({pull}29641[#29641])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ void setFields(Fields termVectorsByField, Set<String> selectedFields, EnumSet<Fl
Terms topLevelTerms = topLevelFields.terms(field);

// if no terms found, take the retrieved term vector fields for stats
if (fieldTermVector == null) {
fieldTermVector = EMPTY_TERMS;
}

if (topLevelTerms == null) {
topLevelTerms = EMPTY_TERMS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,36 @@ public void testSimpleMoreLikeThis() throws Exception {
assertHitCount(response, 1L);
}

//Issue #30148
public void testMoreLikeThisForZeroTokensInOneOfTheAnalyzedFields() throws Exception {
CreateIndexRequestBuilder createIndexRequestBuilder = prepareCreate("test")
.addMapping("type", jsonBuilder()
.startObject().startObject("type")
.startObject("properties")
.startObject("myField").field("type", "text").endObject()
.startObject("empty").field("type", "text").endObject()
.endObject()
.endObject().endObject());

assertAcked(createIndexRequestBuilder);

ensureGreen();

client().index(indexRequest("test").type("type").id("1").source(jsonBuilder().startObject()
.field("myField", "and_foo").field("empty", "").endObject())).actionGet();
client().index(indexRequest("test").type("type").id("2").source(jsonBuilder().startObject()
.field("myField", "and_foo").field("empty", "").endObject())).actionGet();

client().admin().indices().refresh(refreshRequest()).actionGet();

SearchResponse searchResponse = client().prepareSearch().setQuery(
moreLikeThisQuery(new String[]{"myField", "empty"}, null, new Item[]{new Item("test", "type", "1")})
.minTermFreq(1).minDocFreq(1)
).get();

assertHitCount(searchResponse, 1L);
}

public void testSimpleMoreLikeOnLongField() throws Exception {
logger.info("Creating index test");
assertAcked(prepareCreate("test")
Expand Down

0 comments on commit 5939cd4

Please sign in to comment.