[maps] fix Can not style by date time with vector tiles (#124530) (#124741)

* [maps] fix Can not style by date time with vector tiles

* eslint

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
(cherry picked from commit cd7618eeca)

# Conflicts:
#	x-pack/plugins/maps/server/mvt/get_tile.ts
This commit is contained in:
Nathan Reese 2022-02-04 12:56:12 -07:00 committed by GitHub
parent 113ff7d59f
commit 96690d98c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 5 deletions

View file

@ -5,9 +5,9 @@
* 2.0.
*/
import _ from 'lodash';
import { Logger } from 'src/core/server';
import type { DataRequestHandlerContext } from 'src/plugins/data/server';
import { Field, mergeFields } from './merge_fields';
function isAbortError(error: Error) {
return error.message === 'Request aborted' || error.message === 'Aborted';
@ -36,14 +36,18 @@ export async function getEsTile({
}): Promise<Buffer | null> {
try {
const path = `/${encodeURIComponent(index)}/_mvt/${geometryFieldName}/${z}/${x}/${y}`;
let fields = _.uniq(requestBody.docvalue_fields.concat(requestBody.stored_fields));
fields = fields.filter((f) => f !== geometryFieldName);
const body = {
grid_precision: 0, // no aggs
exact_bounds: true,
extent: 4096, // full resolution,
query: requestBody.query,
fields,
fields: mergeFields(
[
requestBody.docvalue_fields as Field[] | undefined,
requestBody.stored_fields as Field[] | undefined,
],
[geometryFieldName]
),
runtime_mappings: requestBody.runtime_mappings,
track_total_hits: requestBody.size + 1,
};

View file

@ -0,0 +1,40 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
// can not use "import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey"
// SearchRequest is incorrectly typed and does not support Field as object
// https://github.com/elastic/elasticsearch-js/issues/1615
export type Field =
| string
| {
field: string;
format: string;
};
export function mergeFields(
fieldsList: Array<Field[] | undefined>,
excludeNames: string[]
): Field[] {
const fieldNames: string[] = [];
const mergedFields: Field[] = [];
fieldsList.forEach((fields) => {
if (!fields) {
return;
}
fields.forEach((field) => {
const fieldName = typeof field === 'string' ? field : field.field;
if (!excludeNames.includes(fieldName) && !fieldNames.includes(fieldName)) {
fieldNames.push(fieldName);
mergedFields.push(field);
}
});
});
return mergedFields;
}

View file

@ -28,7 +28,7 @@ export default function ({ getService }) {
`/api/maps/mvt/getTile/2/1/1.pbf\
?geometryFieldName=geo.coordinates\
&index=logstash-*\
&requestBody=(_source:!f,docvalue_fields:!(bytes,geo.coordinates,machine.os.raw),query:(bool:(filter:!((match_all:()),(range:(%27@timestamp%27:(format:strict_date_optional_time,gte:%272015-09-20T00:00:00.000Z%27,lte:%272015-09-20T01:00:00.000Z%27)))),must:!(),must_not:!(),should:!())),runtime_mappings:(),script_fields:(),size:10000,stored_fields:!(bytes,geo.coordinates,machine.os.raw))`
&requestBody=(_source:!f,docvalue_fields:!(bytes,geo.coordinates,machine.os.raw,(field:'@timestamp',format:epoch_millis)),query:(bool:(filter:!((match_all:()),(range:(%27@timestamp%27:(format:strict_date_optional_time,gte:%272015-09-20T00:00:00.000Z%27,lte:%272015-09-20T01:00:00.000Z%27)))),must:!(),must_not:!(),should:!())),runtime_mappings:(),script_fields:(),size:10000,stored_fields:!(bytes,geo.coordinates,machine.os.raw,'@timestamp'))`
)
.set('kbn-xsrf', 'kibana')
.responseType('blob')
@ -48,6 +48,7 @@ export default function ({ getService }) {
expect(feature.extent).to.be(4096);
expect(feature.id).to.be(undefined);
expect(feature.properties).to.eql({
'@timestamp': '1442709961071',
_id: 'AU_x3_BsGFA8no6Qjjug',
_index: 'logstash-2015.09.20',
bytes: 9252,