Skip to content

Avoid NPE in more_like_this when field has zero tokens #30365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 8, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,38 @@ 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();


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could you remove the second empty line (or maybe even both?), I think a few empty lines to separate blocks don't hurt but too many are also just bloating the code.

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