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
  • Loading branch information
nreese committed Oct 2, 2019
1 parent 51d734e commit a8581b7
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 5 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,48 @@ 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;
}

console.log(preIndexedShape);

const filter = createSpatialFilterWithGeometry({
preIndexedShape,
geometry: this.props.geometry,
geometryLabel,
indexPatternId,
Expand Down Expand Up @@ -65,6 +105,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
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 @@ -412,4 +412,15 @@ export class ESSearchSource extends AbstractESSource {
topHitsSize: this._descriptor.topHitsSize,
};
}

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

return {
index: indexPattern.title,
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 @@ -118,6 +118,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 a8581b7

Please sign in to comment.