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

Add _size and _doc_count to fields output #70575

Merged
merged 8 commits into from
Mar 31, 2021
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
22 changes: 7 additions & 15 deletions docs/plugins/mapper-size.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ PUT my-index-000001
--------------------------

The value of the `_size` field is accessible in queries, aggregations, scripts,
and when sorting:
and when sorting. It can be retrieved using the {ref}/search-fields.html#search-fields-param[fields API]:
Copy link
Contributor

Choose a reason for hiding this comment

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

I just noticed this: we've been trying not to call it the 'fields API' in official docs. It has caused confusion in the past because some users thought it was a separate API endpoint. We could say fields parameter or fields option instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oops, not sure this was updated before merging?

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed, I missed that one, only changed the mention below in the example. Will push a small online-fix.

Copy link
Member Author

Choose a reason for hiding this comment

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

Corrected this.


[source,console]
--------------------------
Expand Down Expand Up @@ -65,29 +65,21 @@ GET my-index-000001/_search
}
}
],
"fields": ["_size"], <4>
"script_fields": {
"size": {
"script": "doc['_size']" <4>
"script": "doc['_size']" <5>
}
},
"docvalue_fields": [
{
"field": "_size" <5>
}
]
}
}
--------------------------
// TEST[continued]

<1> Querying on the `_size` field
<2> Aggregating on the `_size` field
<3> Sorting on the `_size` field
<4> Uses a
<4> Use the `fields` parameter to return the `_size` in the search response.
<5> Uses a
{ref}/search-fields.html#script-fields[script field]
to return the `_size` field in the search response.
<5> Uses a
{ref}/search-fields.html#docvalue-fields[doc value
field] to return the `_size` field in the search response. Doc value fields are
useful if
{ref}/modules-scripting-security.html#allowed-script-types-setting[inline
scripts are disabled].

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
Expand Down Expand Up @@ -93,11 +94,24 @@ private void assertSizeMappingEnabled(String index, boolean enabled) throws IOEx

public void testBasic() throws Exception {
assertAcked(prepareCreate("test").setMapping("_size", "enabled=true"));
final String source = "{\"f\":10}";
final String source = "{\"f\":\"" + randomAlphaOfLengthBetween(1, 100)+ "\"}";
indexRandom(true,
client().prepareIndex("test").setId("1").setSource(source, XContentType.JSON));
GetResponse getResponse = client().prepareGet("test", "1").setStoredFields("_size").get();
assertNotNull(getResponse.getField("_size"));
assertEquals(source.length(), (int) getResponse.getField("_size").getValue());
}

public void testGetWithFields() throws Exception {
assertAcked(prepareCreate("test").setMapping("_size", "enabled=true"));
final String source = "{\"f\":\"" + randomAlphaOfLengthBetween(1, 100)+ "\"}";
indexRandom(true,
client().prepareIndex("test").setId("1").setSource(source, XContentType.JSON));
SearchResponse searchResponse = client().prepareSearch("test").addFetchField("_size").get();
assertEquals(source.length(), ((Long) searchResponse.getHits().getHits()[0].getFields().get("_size").getValue()).intValue());

// this should not work when requesting fields via wildcard expression
searchResponse = client().prepareSearch("test").addFetchField("*").get();
assertNull(searchResponse.getHits().getHits()[0].getFields().get("_size"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
package org.elasticsearch.index.mapper.size;

import org.elasticsearch.common.Explicit;
import org.elasticsearch.index.mapper.DocValueFetcher;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.MetadataFieldMapper;
import org.elasticsearch.index.mapper.NumberFieldMapper.NumberFieldType;
import org.elasticsearch.index.mapper.NumberFieldMapper.NumberType;
import org.elasticsearch.index.mapper.ParseContext;
import org.elasticsearch.index.mapper.ValueFetcher;
import org.elasticsearch.index.query.SearchExecutionContext;

import java.io.IOException;
import java.util.List;
Expand Down Expand Up @@ -42,12 +45,26 @@ protected List<Parameter<?>> getParameters() {

@Override
public SizeFieldMapper build() {
return new SizeFieldMapper(enabled.getValue(), new NumberFieldType(NAME, NumberType.INTEGER));
return new SizeFieldMapper(enabled.getValue(), new SizeFieldType());
}
}

private static class SizeFieldType extends NumberFieldType {
SizeFieldType() {
super(NAME, NumberType.INTEGER);
}

@Override
public ValueFetcher valueFetcher(SearchExecutionContext context, String format) {
if (hasDocValues() == false) {
return lookup -> List.of();
}
return new DocValueFetcher(docValueFormat(format, null), context.getForField(this));
}
}

public static final TypeParser PARSER = new ConfigurableTypeParser(
c -> new SizeFieldMapper(new Explicit<>(false, false), new NumberFieldType(NAME, NumberType.INTEGER)),
c -> new SizeFieldMapper(new Explicit<>(false, false), new SizeFieldType()),
c -> new Builder()
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,19 @@ private static FieldFetcher create(SearchExecutionContext context,

for (FieldAndFormat fieldAndFormat : fieldAndFormats) {
String fieldPattern = fieldAndFormat.field;
boolean isWildcardPattern = Regex.isSimpleMatchPattern(fieldPattern);
if (fieldAndFormat.includeUnmapped != null && fieldAndFormat.includeUnmapped) {
unmappedFetchPattern.add(fieldAndFormat.field);
}

Collection<String> concreteFields = context.simpleMatchToIndexNames(fieldPattern);
for (String field : concreteFields) {
MappedFieldType ft = context.getFieldType(field);
if (ft == null || context.isMetadataField(field)) {
if (ft == null) {
continue;
}
// we want to skip metadata fields if we have a wildcard pattern
if (context.isMetadataField(field) && isWildcardPattern) {
continue;
}
if (field.startsWith(nestedScopePath) == false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,22 @@ public void testMetadataFields() throws IOException {
MapperService mapperService = createMapperService();
XContentBuilder source = XContentFactory.jsonBuilder().startObject()
.field("field", "value")
.field("_doc_count", 100)
.endObject();

Map<String, DocumentField> fields = fetchFields(mapperService, source, "_routing");
assertTrue(fields.isEmpty());
Map<String, DocumentField> fields = fetchFields(mapperService, source, "_doc_count");
assertNotNull(fields.get("_doc_count"));
assertEquals(100, ((Integer) fields.get("_doc_count").getValue()).intValue());

// The _type field was deprecated in 7.x and is not supported in 8.0. So the behavior
// should be the same as if the field didn't exist.
fields = fetchFields(mapperService, source, "_type");
assertTrue(fields.isEmpty());

// several other metadata fields throw exceptions via their value fetchers when trying to get them
for (String fieldname : List.of("_id", "_index", "_seq_no", "_routing", "_ignored")) {
expectThrows(UnsupportedOperationException.class, () -> fetchFields(mapperService, source, fieldname));
}
}

public void testFetchAllFields() throws IOException {
Expand Down