Skip to content

Commit

Permalink
[Maps] use pre-indexed shapes in shape filters when shape is stored i…
Browse files Browse the repository at this point in the history
…n elasticsearch (#47171)

* [Maps] use pre-indexed shapes in shape filters when shape is stored in elasticsearch

* add _index to properties so it can be used in pre-indexed context

* update TooltipControl snapshot
  • Loading branch information
nreese authored Oct 3, 2019
1 parent a6681fe commit 16f8b7c
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ export class GeometryFilterForm extends Component {
fill
onClick={this._onSubmit}
isDisabled={!this.state.geometryLabel || !this.state.geoFieldTag}
isLoading={this.props.isLoading}
>
{this.props.buttonLabel}
</EuiButton>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,46 @@ import { GeometryFilterForm } from '../../components/geometry_filter_form';

export class FeatureGeometryFilterForm extends Component {

_createFilter = ({ geometryLabel, indexPatternId, geoFieldName, geoFieldType, relation }) => {
state = {
isLoading: false,
}

componentDidMount() {
this._isMounted = true;
}

componentWillUnmount() {
this._isMounted = false;
}

_loadPreIndexedShape = async () => {
this.setState({
isLoading: true,
});

let preIndexedShape;
try {
preIndexedShape = await this.props.loadPreIndexedShape();
} catch (err) {
// ignore error, just fall back to using geometry if preIndexedShape can not be fetched
}

if (this._isMounted) {
this.setState({ isLoading: false });
}

return preIndexedShape;
}

_createFilter = async ({ geometryLabel, indexPatternId, geoFieldName, geoFieldType, relation }) => {
const preIndexedShape = await this._loadPreIndexedShape();
if (!this._isMounted) {
// do not create filter if component is unmounted
return;
}

const filter = createSpatialFilterWithGeometry({
preIndexedShape,
geometry: this.props.geometry,
geometryLabel,
indexPatternId,
Expand Down Expand Up @@ -65,6 +103,7 @@ export class FeatureGeometryFilterForm extends Component {
onSubmit={this._createFilter}
isFilterGeometryClosed={this.props.geometry.type !== GEO_JSON_TYPE.LINE_STRING
&& this.props.geometry.type !== GEO_JSON_TYPE.MULTI_LINE_STRING}
isLoading={this.state.isLoading}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,15 @@ export class FeatureTooltip extends React.Component {
);
}

_loadCurrentFeaturePreIndexedShape = () => {
const filteredFeatures = this._filterFeatures();
const currentFeature = filteredFeatures[this.state.pageNumber];
return this.props.loadPreIndexedShape({
layerId: currentFeature.layerId,
featureId: currentFeature.id
});
}

render() {
const filteredFeatures = this._filterFeatures();
const currentFeature = filteredFeatures[this.state.pageNumber];
Expand All @@ -355,6 +364,7 @@ export class FeatureTooltip extends React.Component {
geometry={currentFeatureGeometry}
geoFields={filteredGeoFields}
addFilters={this.props.addFilters}
loadPreIndexedShape={this._loadCurrentFeaturePreIndexedShape}
/>
);
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ export class TooltipControl extends React.Component {
if (!tooltipLayer) {
return null;
}

const targetFeature = tooltipLayer.getFeatureById(featureId);
if (!targetFeature) {
return null;
Expand All @@ -264,13 +265,28 @@ export class TooltipControl extends React.Component {
if (!tooltipLayer) {
return [];
}

const targetFeature = tooltipLayer.getFeatureById(featureId);
if (!targetFeature) {
return [];
}
return await tooltipLayer.getPropertiesForTooltip(targetFeature.properties);
};

_loadPreIndexedShape = async ({ layerId, featureId }) => {
const tooltipLayer = this._findLayerById(layerId);
if (!tooltipLayer) {
return null;
}

const targetFeature = tooltipLayer.getFeatureById(featureId);
if (!targetFeature) {
return null;
}

return await tooltipLayer.getSource().getPreIndexedShape(targetFeature.properties);
};

_findLayerById = (layerId) => {
return this.props.layerList.find(layer => {
return layer.getId() === layerId;
Expand Down Expand Up @@ -308,6 +324,7 @@ export class TooltipControl extends React.Component {
anchorLocation={this.props.tooltipState.location}
findLayerById={this._findLayerById}
geoFields={this.props.geoFields}
loadPreIndexedShape={this._loadPreIndexedShape}
/>
</EuiText>
);
Expand Down
16 changes: 12 additions & 4 deletions x-pack/legacy/plugins/maps/public/elasticsearch_geo_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ export function createSpatialFilterWithGeometry(options) {
}

function createGeometryFilterWithMeta({
preIndexedShape,
geometry,
geometryLabel,
indexPatternId,
Expand All @@ -320,14 +321,21 @@ function createGeometryFilterWithMeta({
};

if (geoFieldType === ES_GEO_FIELD_TYPE.GEO_SHAPE) {
const shapeQuery = {
relation
};

if (preIndexedShape) {
shapeQuery.indexed_shape = preIndexedShape;
} else {
shapeQuery.shape = geometry;
}

return {
meta,
geo_shape: {
ignore_unmapped: true,
[geoFieldName]: {
shape: geometry,
relation
}
[geoFieldName]: shapeQuery
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export class ESSearchSource extends AbstractESSource {

const indexPattern = await this._getIndexPattern();
const unusedMetaFields = indexPattern.metaFields.filter(metaField => {
return metaField !== '_id';
return !['_id', '_index'].includes(metaField);
});
const flattenHit = hit => {
const properties = indexPattern.flattenHit(hit);
Expand Down Expand Up @@ -412,4 +412,14 @@ export class ESSearchSource extends AbstractESSource {
topHitsSize: this._descriptor.topHitsSize,
};
}

async getPreIndexedShape(properties) {
const geoField = await this._getGeoField();

return {
index: properties._index, // Can not use index pattern title because it may reference many indices
id: properties._id,
path: geoField.name,
};
}
}
5 changes: 5 additions & 0 deletions x-pack/legacy/plugins/maps/public/layers/sources/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ export class AbstractSource {
supportsElasticsearchFilters() {
return false;
}

// Returns geo_shape indexed_shape context for spatial quering by pre-indexed shapes
async getPreIndexedShape(/* properties */) {
return null;
}
}


0 comments on commit 16f8b7c

Please sign in to comment.