Skip to content

Commit

Permalink
Merge pull request #1672 from terrestris/group-search-results
Browse files Browse the repository at this point in the history
Allow to group search results by category or layer title
  • Loading branch information
LukasLohoff authored Jun 28, 2024
2 parents 63de36c + a7404db commit 38ca0e5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Several global settings for the client can be configured via the [`gis-client-co
| search.coreName | Solr core name. | 'search' |
| search.defaultUseViewBox | Whether the search is restricted to the current view box. | true |
| search.useNominatim | Whether to use Nominatim. | true |
| search.groupByCategory | Groups search results by 'category' text field. If disabled, the layer title will be used instead. | true |
| search.useSolrHighlighting | Enable / disable solr search result highlighting. | true |
| search.delay | Delay in milliseconds before search is triggered (debouncing). | 1000 |
| search.minChars | Minimum search term length for the search to be triggered. | 3 |
Expand Down
1 change: 1 addition & 0 deletions resources/config/gis-client-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var clientConfig = {
coreName: 'search',
defaultUseViewBox: true,
useNominatim: true,
groupByCategory: true,
useSolrHighlighting: true,
delay: 1000,
minChars: 3,
Expand Down
27 changes: 24 additions & 3 deletions src/components/MultiSearch/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import ClientConfiguration from 'clientConfig';
import _groupBy from 'lodash/groupBy';
import _isNil from 'lodash/isNil';

import { getUid } from 'ol';
import {
Extent as OlExtent
} from 'ol/extent';
Expand All @@ -50,6 +49,10 @@ import useMap from '@terrestris/react-geo/dist/Hook/useMap';
import SearchResultsPanel, {
Category as ResultCategory
} from '@terrestris/react-geo/dist/Panel/SearchResultsPanel/SearchResultsPanel';
import {
WmsLayer,
isWmsLayer
} from '@terrestris/react-geo/dist/Util/typeUtils';

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

const wktFormat = new OlFormatWKT();
// 1. group by category
const categories = _groupBy(dataSearchResults, res => res?.category[0]);

// 1. group by category or layer title
let categories;
if (ClientConfiguration.search?.groupByCategory) {
categories = _groupBy(dataSearchResults, res => res?.category[0]);
} else {
const layers = map.getAllLayers().filter(l => l.get('searchable'));
const resultsWithLayerName = dataSearchResults.map(result => {
const layerTitle = layers.filter(l => isWmsLayer(l))
.find((l) => (l as WmsLayer).getSource()?.getParams()?.LAYERS === result.featureType[0])
?.get('name');

return {
layerTitle,
...result
} as DataSearchResult;
});
categories = _groupBy(resultsWithLayerName, res => res?.layerTitle);
}

// 2. build features
Object.keys(categories).forEach(category => {
const features = categories[category].map(dsResult => {
Expand Down
1 change: 1 addition & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ declare module 'clientConfig' {
type SearchConfiguration = {
solrBasePath?: string;
useNominatim?: boolean;
groupByCategory?: boolean;
useSolrHighlighting?: boolean;
defaultUseViewBox?: boolean;
delay?: number;
Expand Down

0 comments on commit 38ca0e5

Please sign in to comment.