Skip to content
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

Fix automatic tracking of collapse with docvalue_fields #110103

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions docs/changelog/110103.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 110103
summary: Fix automatic tracking of collapse with `docvalue_fields`
area: Search
type: bug
issues: []
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Add #96510 to issue list

Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,26 @@ public void testCollapse() {
}
);
}

public void testCollapseWithDocValueFields() {
final String indexName = "test_collapse";
createIndex(indexName);
final String collapseField = "collapse_field";
final String otherField = "other_field";
assertAcked(indicesAdmin().preparePutMapping(indexName).setSource(collapseField, "type=keyword", otherField, "type=keyword"));
index(indexName, "id_1_0", Map.of(collapseField, "value1", otherField, "other_value1"));
index(indexName, "id_1_1", Map.of(collapseField, "value1", otherField, "other_value2"));
index(indexName, "id_2_0", Map.of(collapseField, "value2", otherField, "other_value3"));
refresh(indexName);

assertNoFailuresAndResponse(
prepareSearch(indexName).setQuery(new MatchAllQueryBuilder())
.addDocValueField(otherField)
.setCollapse(new CollapseBuilder(collapseField).setInnerHits(new InnerHitBuilder("ih").setSize(2))),
searchResponse -> {
assertEquals(collapseField, searchResponse.getHits().getCollapseField());
assertEquals(Set.of(new BytesRef("value1"), new BytesRef("value2")), Set.of(searchResponse.getHits().getCollapseValues()));
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public FetchDocValuesContext docValuesContext() {
searchContext.getSearchExecutionContext(),
Collections.singletonList(new FieldAndFormat(name, null))
);
} else if (searchContext.docValuesContext().fields().stream().map(ff -> ff.field).anyMatch(name::equals) == false) {
} else if (searchContext.docValuesContext().fields().stream().map(ff -> ff.field).noneMatch(name::equals)) {
dvContext.fields().add(new FieldAndFormat(name, null));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.query.SearchExecutionContext;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
Expand All @@ -24,7 +25,7 @@
*/
public class FetchDocValuesContext {

private final Collection<FieldAndFormat> fields;
private final List<FieldAndFormat> fields;

/**
* Create a new FetchDocValuesContext using the provided input list.
Expand All @@ -40,7 +41,7 @@ public FetchDocValuesContext(SearchExecutionContext searchExecutionContext, List
fieldToFormats.put(fieldName, new FieldAndFormat(fieldName, field.format, field.includeUnmapped));
}
}
this.fields = fieldToFormats.values();
this.fields = new ArrayList<>(fieldToFormats.values());
int maxAllowedDocvalueFields = searchExecutionContext.getIndexSettings().getMaxDocvalueFields();
if (fields.size() > maxAllowedDocvalueFields) {
throw new IllegalArgumentException(
Expand All @@ -58,7 +59,7 @@ public FetchDocValuesContext(SearchExecutionContext searchExecutionContext, List
/**
* Returns the required docvalue fields.
*/
public Collection<FieldAndFormat> fields() {
public List<FieldAndFormat> fields() {
return this.fields;
}
}