Allow to configure the Solr spatial query sent

This commit is contained in:
amercader 2022-09-06 10:31:51 +02:00
parent a358afce5c
commit 89070be10c
2 changed files with 76 additions and 2 deletions

View File

@ -303,10 +303,17 @@ you need to split the geometry in order to fit the parts. Not indexing""")
if not search_params.get("fq_list"):
search_params["fq_list"] = []
spatial_field = "spatial_bbox" if search_backend == "solr" else "spatial_geom"
spatial_field = (
"spatial_bbox" if search_backend == "solr" else "spatial_geom"
)
default_spatial_query = "{{!field f={spatial_field}}}Intersects(ENVELOPE({minx}, {maxx}, {maxy}, {miny}))"
spatial_query = config.get(
"ckanext.spatial.solr_query", default_spatial_query)
search_params["fq_list"].append(
"{{!field f={spatial_field}}}Intersects(ENVELOPE({minx}, {maxx}, {maxy}, {miny}))".format(
spatial_query.format(
spatial_field=spatial_field, **bbox)
)

View File

@ -244,6 +244,39 @@ class TestBBoxSearch(SpatialTestBase):
assert result["count"] == 1
assert result["results"][0]["id"] == dataset["id"]
def test_custom_spatial_query(self, monkeypatch, ckan_config):
"""
xxxxxx
xxxxxxx xxx
xxx xxx
xx xx
xxxx xxx
x xxxxxx
xxx xxxx
xxxx
"""
dataset = factories.Dataset(extras=[{"key": "spatial", "value": extents["nz"]}])
result = helpers.call_action(
"package_search", extras={"ext_bbox": "175,-39.5,176.5,-39"}
)
assert result["count"] == 1
assert result["results"][0]["id"] == dataset["id"]
monkeypatch.setitem(
ckan_config,
"ckanext.spatial.solr_query",
"{{!field f={spatial_field}}}Contains(ENVELOPE({minx}, {maxx}, {maxy}, {miny}))")
result = helpers.call_action(
"package_search", extras={"ext_bbox": "175,-39.5,176.5,-39"}
)
assert result["count"] == 0
@pytest.mark.usefixtures("clean_db", "clean_index", "harvest_setup")
@pytest.mark.ckan_config("ckanext.spatial.search_backend", "solr-spatial-field")
@ -530,3 +563,37 @@ class TestSpatialFieldSearch(SpatialTestBase):
)
assert result["count"] == 0
def test_custom_spatial_query(self, monkeypatch, ckan_config):
"""
xxxxxx
xxxxxxx xxx
xxx xxx
xx xx
xxxx xxx
x xxxxxx
xxx xxxx
xxxx
"""
dataset = factories.Dataset(extras=[{"key": "spatial", "value": extents["nz"]}])
result = helpers.call_action(
"package_search", extras={"ext_bbox": "175,-39.5,176.5,-39"}
)
assert result["count"] == 1
assert result["results"][0]["id"] == dataset["id"]
monkeypatch.setitem(
ckan_config,
"ckanext.spatial.solr_query",
"{{!field f={spatial_field}}}Contains(ENVELOPE({minx}, {maxx}, {maxy}, {miny}))")
result = helpers.call_action(
"package_search", extras={"ext_bbox": "175,-39.5,176.5,-39"}
)
assert result["count"] == 0