/SolrSearch

a QGIS-Plugin connecting the locator search to Apache Solr

Primary LanguagePythonGNU General Public License v3.0GPL-3.0

SolrSearch

SolrSearch is a QGIS-plugin to integrate an Apache Solr installation into the locator filter:
Bildschirmfoto 2021-08-29 um 19 50 58
This plugin allows QGIS to connect to a search platform realised by Apache Solr. Depending on the data in the search platform, the user has the option of a full text search with a spatial component to change the map position according to the selected search result.

Usage

Just type in the search string and a request will be send to the configured Apache Solr. The search results will be grouped by their cores. A double click will zoom the map canvas on the search result and a red polygon around the result will be displayed. This red polygon will disappear, when the search string will be adjusted or removed.

Installation

The plugin itself is available via the QGIS Plugin repository and directly within QGIS from the Plugin Manager. An addtional installation of Apache Solr is mandatory for using the plugin.

Configuration

The plugin will be shipped with a configuration file in json format named settings.json:
Bildschirmfoto 2021-08-29 um 20 00 51
The parameters for working with an Apache Solr installation has to be stored in this file.

Path to settings.json

At startup the plugin looks for QgsSettings to get the path to the settings-file. If this QgsSettings are not set, the plugin looks in its directory for this file.

#read the settings
settings_path = QgsSettings().value("SolrSearch/settings_path", str(pathlib.Path(__file__).parent.resolve()) + "/settings.json")
self.settings = SolrSearchSettings(settings_path)

To run this plugin in an production environment, make sure that each user/profile has this QgsSettings set.

settings.json

The shipped settings file is just an example and has to be edited before using it, because there is no working Apache Solr installation configured into it:

{
	"prefix": "solr",
	"max_results": 10,
	"point_scale": 2500,
	"solr_cores": [
		{
			"internal_name": "core",
			"external_name": "Solr Core",
			"url": "http://localhost:8080/solr/core/select?",
			"query": {
				"query_field": "text",
				"query_prefix": "(",
				"query_suffix": ")",
				"connection": "+"
			},
			"icon_path": "",
			"result_field": "content",
			"geom_field": "geo"
		}
	]
}

JSON Schema

parameter type explanation
prefix String The prefix of this plugin in the locator search bar.
max_results Integer The maximum number of search results that should be shown. This value will be used for each configured Solr Core, i.e. if two cores will be queried by this plugin, there will be 20 or less search results.
point_scale Integer The number of the map scale after clicking on a search result. The plugin will zoom to the spatial information of a search result and settings this number as the map scale.
solr_cores Array An array of all Solr Cores that will be queried.
internal_name String The name of the Solr Core. This value will be used for the query.
external_name String The name of the Solr Core that will be displayed in the locator search bar.
url String The url of the Solr Core excluding any HTTP-Get-parameter.
query Object An object containing additional information for creating a HTTP-Get-Request on a Solr Core.
query_field String The name of the field that should be queried with the fq-parameter, could also be *.
query_prefix String A prefix that will be added to the beginning of the query string.
query_suffix String A suffix that will be added to the end of the query string.
connection String The plugin splits all entered search strings by spaces. The value of this parameter will be used to connect the splitted string, e.g. to query multiple strings with AND or OR.
icon_path String The relative path to a icon (png, jpg, gif, etc.) that will be displayed on front of the search results of this core. The icon has to be be located inside the plugin folder.
result_field String The name of the json item that should be displayed as a search result in the locator search bar. The type of this field has to be string. The complete field content will be displayed.
geom_field String The name of the geometry field. The Geometry has to be a Well-Known-Text (WKT) with a single geometry.

Apache Solr Configuration

It is mandatory to load spatial information into the Solr Cores, so that the plugin can zoom the map canvas on the spatial component of the search results. The spatial data has to be stored as Well-Known-Text (WKT) and with single geometries only.

Limitations

The current version of the plugin has the following limitations:

  • only one column or all via * can be queried
  • the inserted search string will be splitted by spaces, all substrings will be connected via logical operators for the query
  • only the logical operator AND is supported to connect search strings
  • the HTTP-Get-parameters q and fq will be used for the query in one request

Following the creation of the request with core as a json object from the configuration file:

#create the request url
url = (core.get("url")
    + "q="
    + core.get("query").get("query_field")
    + ":*"
    + "&fq="
    + core.get("query").get("query_field")
    + ":"
    + core.get("query").get("query_prefix")
    + "*"
    + search_string
    + "*"
    + core.get("query").get("query_suffix")
    + "&q.op=AND"
    + "&wt=json&rows="
    + str(self.settings.getMaxRows()))