Skip to content

Commit 38ca0e5

Browse files
authored
Merge pull request #1672 from terrestris/group-search-results
Allow to group search results by category or layer title
2 parents 63de36c + a7404db commit 38ca0e5

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Several global settings for the client can be configured via the [`gis-client-co
5252
| search.coreName | Solr core name. | 'search' |
5353
| search.defaultUseViewBox | Whether the search is restricted to the current view box. | true |
5454
| search.useNominatim | Whether to use Nominatim. | true |
55+
| search.groupByCategory | Groups search results by 'category' text field. If disabled, the layer title will be used instead. | true |
5556
| search.useSolrHighlighting | Enable / disable solr search result highlighting. | true |
5657
| search.delay | Delay in milliseconds before search is triggered (debouncing). | 1000 |
5758
| search.minChars | Minimum search term length for the search to be triggered. | 3 |

resources/config/gis-client-config.js

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var clientConfig = {
2626
coreName: 'search',
2727
defaultUseViewBox: true,
2828
useNominatim: true,
29+
groupByCategory: true,
2930
useSolrHighlighting: true,
3031
delay: 1000,
3132
minChars: 3,

src/components/MultiSearch/index.tsx

+24-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import ClientConfiguration from 'clientConfig';
2424
import _groupBy from 'lodash/groupBy';
2525
import _isNil from 'lodash/isNil';
2626

27-
import { getUid } from 'ol';
2827
import {
2928
Extent as OlExtent
3029
} from 'ol/extent';
@@ -50,6 +49,10 @@ import useMap from '@terrestris/react-geo/dist/Hook/useMap';
5049
import SearchResultsPanel, {
5150
Category as ResultCategory
5251
} from '@terrestris/react-geo/dist/Panel/SearchResultsPanel/SearchResultsPanel';
52+
import {
53+
WmsLayer,
54+
isWmsLayer
55+
} from '@terrestris/react-geo/dist/Util/typeUtils';
5356

5457
import {
5558
SearchConfig
@@ -382,8 +385,26 @@ export const MultiSearch: React.FC<MultiSearchProps> = ({
382385
if (dataSearchResults?.length > 0) {
383386

384387
const wktFormat = new OlFormatWKT();
385-
// 1. group by category
386-
const categories = _groupBy(dataSearchResults, res => res?.category[0]);
388+
389+
// 1. group by category or layer title
390+
let categories;
391+
if (ClientConfiguration.search?.groupByCategory) {
392+
categories = _groupBy(dataSearchResults, res => res?.category[0]);
393+
} else {
394+
const layers = map.getAllLayers().filter(l => l.get('searchable'));
395+
const resultsWithLayerName = dataSearchResults.map(result => {
396+
const layerTitle = layers.filter(l => isWmsLayer(l))
397+
.find((l) => (l as WmsLayer).getSource()?.getParams()?.LAYERS === result.featureType[0])
398+
?.get('name');
399+
400+
return {
401+
layerTitle,
402+
...result
403+
} as DataSearchResult;
404+
});
405+
categories = _groupBy(resultsWithLayerName, res => res?.layerTitle);
406+
}
407+
387408
// 2. build features
388409
Object.keys(categories).forEach(category => {
389410
const features = categories[category].map(dsResult => {

src/global.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ declare module 'clientConfig' {
2727
type SearchConfiguration = {
2828
solrBasePath?: string;
2929
useNominatim?: boolean;
30+
groupByCategory?: boolean;
3031
useSolrHighlighting?: boolean;
3132
defaultUseViewBox?: boolean;
3233
delay?: number;

0 commit comments

Comments
 (0)