Skip to content

Commit

Permalink
feat: display search filters in map layer config panel
Browse files Browse the repository at this point in the history
Adding filters to map layer allows user to do more accurate and
customized searches

Signed-off-by: Yulong Ruan <[email protected]>
  • Loading branch information
ruanyl committed Dec 20, 2022
1 parent a0553cf commit 48e5aad
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React, { useEffect, useState } from 'react';
import React, { useCallback, useEffect, useState } from 'react';
import {
EuiComboBox,
EuiFlexItem,
Expand All @@ -20,11 +20,11 @@ import {
} from '@elastic/eui';
import { i18n } from '@osd/i18n';
import { FormattedMessage } from '@osd/i18n/react';
import { IndexPattern, IndexPatternField } from '../../../../../../src/plugins/data/public';
import { Filter, IndexPattern, IndexPatternField } from '../../../../../../src/plugins/data/public';
import { useOpenSearchDashboards } from '../../../../../../src/plugins/opensearch_dashboards_react/public';
import { MapServices } from '../../../types';
import { DocumentLayerSpecification } from '../../../model/mapLayerType';
import _, { Dictionary } from "lodash";
import _, { Dictionary } from 'lodash';

interface Props {
setSelectedLayerConfig: Function;
Expand All @@ -41,7 +41,7 @@ export const DocumentLayerSource = ({
services: {
savedObjects: { client: savedObjectsClient },
data: {
ui: { IndexPatternSelect },
ui: { IndexPatternSelect, SearchBar },
indexPatterns,
},
},
Expand Down Expand Up @@ -137,6 +137,16 @@ export const DocumentLayerSource = ({
setSelectedLayerConfig({ ...selectedLayerConfig, source });
};

const onFiltersUpdated = useCallback(
(filters: Filter[]) => {
setSelectedLayerConfig({
...selectedLayerConfig,
source: { ...selectedLayerConfig.source, filters },
});
},
[selectedLayerConfig]
);

useEffect(() => {
const selectIndexPattern = async () => {
if (selectedLayerConfig.source.indexPatternId) {
Expand Down Expand Up @@ -276,6 +286,23 @@ export const DocumentLayerSource = ({
</EuiCollapsibleNavGroup>
</EuiPanel>
<EuiSpacer size="m" />
<EuiPanel>
<EuiCollapsibleNavGroup
title="Filters"
titleSize="xxs"
isCollapsible={true}
initialIsOpen={true}
>
<SearchBar
appName="maps-dashboards"
showQueryBar={false}
indexPatterns={indexPattern ? [indexPattern] : []}
filters={selectedLayerConfig.source.filters ?? []}
onFiltersUpdated={onFiltersUpdated}
/>
</EuiCollapsibleNavGroup>
</EuiPanel>
<EuiSpacer size="m" />
<EuiPanel paddingSize="s">
<EuiCollapsibleNavGroup
title="Tool Tips"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ import { MapServices } from '../../types';
import {
IOpenSearchDashboardsSearchResponse,
isCompleteResponse,
buildOpenSearchQuery,
} from '../../../../../src/plugins/data/common';
import { IndexPattern } from '../../../../../src/plugins/data/public';

interface MaplibreRef {
current: Maplibre | null;
Expand All @@ -52,7 +54,7 @@ interface Props {
const LayerControlPanel = memo(({ maplibreRef, setLayers, layers }: Props) => {
const {
services: {
data: { search },
data: { search, indexPatterns },
notifications,
},
} = useOpenSearchDashboards<MapServices>();
Expand Down Expand Up @@ -83,12 +85,17 @@ const LayerControlPanel = memo(({ maplibreRef, setLayers, layers }: Props) => {
if (sourceConfig.showTooltips === true && sourceConfig.tooltipFields.length > 0) {
sourceFields.push(...sourceConfig.tooltipFields);
}
let indexPattern: IndexPattern | undefined = undefined;
if (layer.source.indexPatternId) {
indexPattern = await indexPatterns.get(layer.source.indexPatternId);
}
const request = {
params: {
index: indexPatternRefName,
size: layer.source.documentRequestNumber,
body: {
_source: sourceFields,
query: buildOpenSearchQuery(indexPattern, [], layer.source.filters),
},
},
};
Expand Down
3 changes: 3 additions & 0 deletions maps_dashboards/public/model/mapLayerType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { Filter } from '../../../../src/plugins/data/public';

/* eslint @typescript-eslint/consistent-type-definitions: ["error", "type"] */
export type MapLayerSpecification = OSMLayerSpecification | DocumentLayerSpecification;

Expand Down Expand Up @@ -38,6 +40,7 @@ export type DocumentLayerSpecification = {
documentRequestNumber: number;
showTooltips: boolean;
tooltipFields: string[];
filters: Filter[];
};
style: {
fillColor: string;
Expand Down

0 comments on commit 48e5aad

Please sign in to comment.