Bug 31652

Summary: Add geo-search
Product: Koha Reporter: Thomas Klausner <domm>
Component: Searching - ElasticsearchAssignee: Mark Hofstetter <koha>
Status: RESOLVED FIXED QA Contact:
Severity: new feature    
Priority: P5 - low CC: arthur.suzuki, caroline.cyr-la-rose, david, dcook, flyingendpaper, fridolin.somers, jzairo, kelly, koha, lucas, nick
Version: Main   
Hardware: All   
OS: All   
See Also: https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=25453
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=18066
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=18345
Change sponsored?: Sponsored Patch complexity: Medium patch
Documentation contact: Caroline Cyr La Rose Documentation submission: https://gitlab.com/koha-community/koha-manual/-/merge_requests/938
Text to go in the release notes:
This lays the groundwork for geographical searching using Elasticsearch 7+. This includes: - New search types for Elasticsearch search mappings to store latitude and longitude values and index them (using values from 034$s and 034$t). - Extending the QueryBuilder to allow for building advanced Elasticsearch queries (for example, geo_distance) that cannot be represented in a simple string query. To use this new feature now in the OPAC, install and enable the HKS3GeoSearch plugin (https://github.com/HKS3/HKS3GeoSearch).
Version(s) released in:
24.05.00
Circulation function:
Bug Depends on: 36151    
Bug Blocks:    
Attachments: Bug 31652: Add geo-search
Bug 31652: Add geo-search
Bug 31652: Add geo-search
Bug 31652: Add geo-search
Bug 31652: Add geo-search
Bug 31652: (follow-up) Fix update and remove fixed fixme
Bug 31652: Add geo-search
Bug 31652: Add geo-search
MARC21 cartographic bib record with coordinates
Another MARC21 catalog record with coordinates
A third MARC21 bib record with coordinates

Description Thomas Klausner 2022-09-29 11:38:56 UTC
A customer wants to add a geo-search to Koha:

* Store lat/lng in biblio 034$s and 034$t
* Add lat/lng to ElasticSearch index as geo_point: https://www.elastic.co/guide/en/elasticsearch/reference/7.17/geo-point.html
* Add support for ElasticSearch geo-queries: https://www.elastic.co/guide/en/elasticsearch/reference/7.17/geo-queries.html
* Use Leaflet.js to render maps and pins

We (HKS3, koha-support.eu) have already implemented most of it. We will provide our changes as a patch (or a set of patches) and hope that they will be merged into the main Koha source code.

It will take a bit more time to actually provide the code, but I create this ticket now so we have an "anchor" if anybody wants to discuss this feature etc.

Greetings,
domm
Comment 1 Thomas Klausner 2022-10-06 11:39:40 UTC
Created attachment 141406 [details] [review]
Bug 31652: Add geo-search

This patch adds geosearch to Koha (using Elasticsearch 7). ElasticSearch
search_mappings get new types to store lat/lon, which can be indexed
from MARC 034$s and 034$t. There is a small change to the DB to allow a
new value in search_field.type ENUM.

The QueryBuilder is extended to allow for building advanced
ElasticSearch Querys (eg geo_distance) that cannot be represented in a
simple string query. The UI for searching (including showing the results
on a OSM/Leaflet map) is implemented in a separate plugin
(https://github.com/HKS3/HKS3GeoSearch)

Test Plan:

* make sure you're running ElasticSearch 7
  (eg via `curl http://es:9200?pretty | grep number`)
* apply patch
* Set system preference "SaerchEngine" to "Elasticsearch"
* got to a Framework, check Editor for 034$s and 034$t and save
* got to some books (in the correct framework) and enter some lat and lon into 034$s and 034$t (for example lat=48.216, lon=16.395)
* Run the elasticsearch indexer, maybe limited on the books you edited (-bn 123 -bn 456):
  misc/search_tools/rebuild_elasticsearch.pl -b -v
* You can check if the indexing worked by inspecting the document in elasticsearch:
  * get the biblionumber (eg 123)
  * curl http://es:9200/koha_kohadev_biblios/_doc/123?pretty | grep -A5 geolocation
  * You should get back a JSON fragment containing the lat/lon you stored
* You can query elasticsearch directly:
  * Run the following curl command, but adapt the value for lat/lng and/or the distance (in meters)
  * curl -X GET "http://es:9200/koha_kohadev_biblios/_search?pretty" -H 'Content-Type: application/json' -d '{"query": {"bool":{"must":{"match_all":{}},"filter":{"geo_distance":{"distance":100000,"geolocation":{"lat":48.2,"lon":16.4}}}}}}'
* To run the search via Koha, you need to either install and use https://github.com/HKS3/HKS3GeoSearch or create a handcrafted query string:
  * handcrafted query string:
    * /cgi-bin/koha/opac-search.pl?advsearch=1&idx=geolocation&q=lat:48.25+lng:18.35+distance:100km&do=Search
  * HKS3GeoSearch
    * install the plugin and enable it
    * got to OPAC / Advanced Search
    * There is a new input box "Geographic Search" where you can enter lat/long/radius
    * On the search result page a map is shown with pins for each found biblioitem

Sponsored-by: ZAMG - Zentralanstalt für Meterologie und Geodynamik, Austria - https://www.zamg.ac.at/

This is a combination of 17 commits.
Comment 2 David Nind 2023-05-02 23:25:58 UTC
The patch no longer applies 8-(...

git bz apply 31652

Bug 31652 - Add geo-search

141406 - Bug 31652: Add geo-search

Apply? [(y)es, (n)o, (i)nteractive] y
Applying: Bug 31652: Add geo-search
.git/rebase-apply/patch:323: trailing whitespace.
# can be single or multiple parameters separated by comma: kw,right-Truncation 
warning: 1 line adds whitespace errors.
Using index info to reconstruct a base tree...
M       Koha/SearchEngine/Elasticsearch.pm
M       Koha/SearchEngine/Elasticsearch/QueryBuilder.pm
M       Koha/SearchEngine/Elasticsearch/Search.pm
M       admin/searchengine/elasticsearch/field_config.yaml
M       admin/searchengine/elasticsearch/mappings.yaml
M       installer/data/mysql/kohastructure.sql
M       koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt
M       opac/opac-search.pl
.git/rebase-apply/patch:323: trailing whitespace.
# can be single or multiple parameters separated by comma: kw,right-Truncation 
warning: 1 line adds whitespace errors.
Falling back to patching base and 3-way merge...
Auto-merging opac/opac-search.pl
Auto-merging koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt
CONFLICT (content): Merge conflict in koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt
Auto-merging installer/data/mysql/kohastructure.sql
CONFLICT (content): Merge conflict in installer/data/mysql/kohastructure.sql
Auto-merging admin/searchengine/elasticsearch/mappings.yaml
Auto-merging admin/searchengine/elasticsearch/field_config.yaml
CONFLICT (content): Merge conflict in admin/searchengine/elasticsearch/field_config.yaml
Auto-merging Koha/SearchEngine/Elasticsearch/Search.pm
Auto-merging Koha/SearchEngine/Elasticsearch/QueryBuilder.pm
Auto-merging Koha/SearchEngine/Elasticsearch.pm
error: Failed to merge in the changes.
Patch failed at 0001 Bug 31652: Add geo-search
Comment 3 Thomas Klausner 2023-07-20 13:23:26 UTC
Created attachment 153720 [details] [review]
Bug 31652: Add geo-search

This patch adds geosearch to Koha (using Elasticsearch 7). ElasticSearch
search_mappings get new types to store lat/lon, which can be indexed
from MARC 034$s and 034$t. There is a small change to the DB to allow a
new value in search_field.type ENUM.

The QueryBuilder is extended to allow for building advanced
ElasticSearch Querys (eg geo_distance) that cannot be represented in a
simple string query. The UI for searching (including showing the results
on a OSM/Leaflet map) is implemented in a separate plugin
(https://github.com/HKS3/HKS3GeoSearch)

Test Plan:

* make sure you're running ElasticSearch 7
  (eg via `curl http://es:9200?pretty | grep number`)
* apply patch
* Set system preference "SaerchEngine" to "Elasticsearch"
* got to a Framework, check Editor for 034$s and 034$t and save
* got to some books (in the correct framework) and enter some lat and lon into 034$s and 034$t (for example lat=48.216, lon=16.395)
* Run the elasticsearch indexer, maybe limited on the books you edited (-bn 123 -bn 456):
  misc/search_tools/rebuild_elasticsearch.pl -b -v
* You can check if the indexing worked by inspecting the document in elasticsearch:
  * get the biblionumber (eg 123)
  * curl http://es:9200/koha_kohadev_biblios/_doc/123?pretty | grep -A5 geolocation
  * You should get back a JSON fragment containing the lat/lon you stored
* You can query elasticsearch directly:
  * Run the following curl command, but adapt the value for lat/lng and/or the distance (in meters)
  * curl -X GET "http://es:9200/koha_kohadev_biblios/_search?pretty" -H 'Content-Type: application/json' -d '{"query": {"bool":{"must":{"match_all":{}},"filter":{"geo_distance":{"distance":100000,"geolocation":{"lat":48.2,"lon":16.4}}}}}}'
* To run the search via Koha, you need to either install and use https://github.com/HKS3/HKS3GeoSearch or create a handcrafted query string:
  * handcrafted query string:
    * /cgi-bin/koha/opac-search.pl?advsearch=1&idx=geolocation&q=lat:48.25+lng:18.35+distance:100km&do=Search
  * HKS3GeoSearch
    * install the plugin and enable it
    * got to OPAC / Advanced Search
    * There is a new input box "Geographic Search" where you can enter lat/long/radius
    * On the search result page a map is shown with pins for each found biblioitem

Sponsored-by: ZAMG - Zentralanstalt für Meterologie und Geodynamik, Austria - https://www.zamg.ac.at/

This is a combination of 17 commits.
Comment 4 Thomas Klausner 2023-07-20 13:25:18 UTC
I've updated the patch and it should apply now (again) on current master. Please try to signoff again, I'd love to have this in master before Perl Koha Conference (where I'll do a talk on this patch!)
Comment 5 David Cook 2023-07-20 23:47:01 UTC
Comment on attachment 153720 [details] [review]
Bug 31652: Add geo-search

Review of attachment 153720 [details] [review]:
-----------------------------------------------------------------

::: Koha/SearchEngine/Elasticsearch/Indexer.pm
@@ +117,4 @@
>      }
>  
>      my $documents = $self->marc_records_to_documents($records);
> +

It looks like this file might not be altered beyond whitespace? Looks like the whitespace change could be removed to simplify the overall patch.

::: Koha/SearchEngine/Elasticsearch/Search.pm
@@ +93,5 @@
>      my $elasticsearch = $self->get_elasticsearch();
> +
> +    # XXX investigate where empty query_string is coming from
> +    delete $query->{query}->{query_string} if
> +      $query->{query}->{query_string} && !%{$query->{query}->{query_string}};

Is this a pre-existing problem or introduced in this patch?
Comment 6 Mark Hofstetter 2023-08-08 07:39:45 UTC
This is/was a pre-existing problem
Comment 7 Thomas Klausner 2023-08-11 19:17:12 UTC
Created attachment 154388 [details] [review]
Bug 31652: Add geo-search

This patch adds geosearch to Koha (using Elasticsearch 7). ElasticSearch
search_mappings get new types to store lat/lon, which can be indexed
from MARC 034$s and 034$t. There is a small change to the DB to allow a
new value in search_field.type ENUM.

The QueryBuilder is extended to allow for building advanced
ElasticSearch Querys (eg geo_distance) that cannot be represented in a
simple string query.

We added a new SystemPreference `GeoSearchEnabled`. If it is set to
`true`, we show a new advanced search field that allows for input of lat/lng
and distance (radius). On the search result page, a we render a map
using leaflet.js with pins for each found item. Therefor we also added a
new api endpoint that returns lat/lng for each biblio in the selection.

Test Plan:

* make sure you're running ElasticSearch 7
  (eg via `curl http://es:9200?pretty | grep number`)
* apply patch
* Set system preference "SearchEngine" to "Elasticsearch"
* got to a Framework, check Editor for 034$s and 034$t and save
* got to some books (in the correct framework) and enter some lat and lon into 034$s and 034$t (for example lat=48.216, lon=16.395)
* Run the elasticsearch indexer, maybe limited on the books you edited (-bn 123 -bn 456):
  misc/search_tools/rebuild_elasticsearch.pl -b -v
* You can check if the indexing worked by inspecting the document in elasticsearch:
  * get the biblionumber (eg 123)
  * curl http://es:9200/koha_kohadev_biblios/_doc/123?pretty | grep -A5 geolocation
  * You should get back a JSON fragment containing the lat/lon you stored
* You can query elasticsearch directly:
  * Run the following curl command, but adapt the value for lat/lng and/or the distance (in meters)
  * curl -X GET "http://es:9200/koha_kohadev_biblios/_search?pretty" -H 'Content-Type: application/json' -d '{"query": {"bool":{"must":{"match_all":{}},"filter":{"geo_distance":{"distance":100000,"geolocation":{"lat":48.2,"lon":16.4}}}}}}'
* To run the search via Koha:
  * Set system preference "GeoSearchEnabled" to "true"
  * got to OPAC / Advanced Search
  * There is a new input box "Geographic Search" where you can enter lat/long/radius, for example lat=48.2, lng=16.3, distance=100km
  * On the search result page a map is shown with pins for each found biblioitem

Sponsored-by: ZAMG - Zentralanstalt für Meterologie und Geodynamik, Austria - https://www.zamg.ac.at/

Authors: Mark Hofstetter <mark@hofstetter.at>, Thomas Klausner <domm@plix.at>
Comment 8 Thomas Klausner 2023-08-11 19:40:30 UTC
The latest patch has some warnings/errors reported from koha-qa.pl. Plus I'm not totally happy with the way the search params are handled. So I guess it will need a bit more work.
Comment 9 Lucas Gass (lukeg) 2023-08-14 17:01:50 UTC
(In reply to Thomas Klausner from comment #8)
> The latest patch has some warnings/errors reported from koha-qa.pl. Plus I'm
> not totally happy with the way the search params are handled. So I guess it
> will need a bit more work.

Based on this I am setting this to FQA, to get it out of the 'Needs signoff' queue
Comment 10 Thomas Klausner 2023-08-21 14:36:34 UTC
Here's some old-ish work on improving ElasticSearch, which could be used/integrated here (as pointed out by Jonathan during PerlKohaCon)

https://github.com/fredericd/Koha/compare/master...newelastic
Comment 11 Nick Clemens (kidclamp) 2023-08-22 15:21:50 UTC
(In reply to Thomas Klausner from comment #10)
> Here's some old-ish work on improving ElasticSearch, which could be
> used/integrated here (as pointed out by Jonathan during PerlKohaCon)
> 
> https://github.com/fredericd/Koha/compare/master...newelastic

This should go onto a new bug - if we want to change searching at large let's do that, then base this on top of that work
Comment 12 Fridolin Somers 2023-08-28 18:55:59 UTC
Is is necessary to use external server for static files ?

https://unpkg.com/leaflet@1.9.1/dist/leaflet.css
https://unpkg.com/leaflet@1.9.1/dist/leaflet.js

Can't we embed in sources ?
Comment 13 Mark Hofstetter 2023-08-31 07:36:30 UTC
I don't see a problem in embedding the files if that is "the prefered way of doing things"
Comment 14 Fridolin Somers 2023-08-31 22:21:48 UTC
Lets see what QA says
Comment 15 David Cook 2023-09-01 00:05:04 UTC
Comment on attachment 154388 [details] [review]
Bug 31652: Add geo-search

Review of attachment 154388 [details] [review]:
-----------------------------------------------------------------

::: koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-results.tt
@@ +592,5 @@
>          [% Asset.js("js/openlibrary.js") | $raw %]
>      [% END %]
> +    [% IF ( GeoSearchEnabled ) %]
> +        <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.1/dist/leaflet.css" integrity="sha256-sA+zWATbFveLLNqWO2gtiw3HL/lh1giY/Inf1BJ0z14=" crossorigin=""/>
> +        <script src="https://unpkg.com/leaflet@1.9.1/dist/leaflet.js" integrity="sha256-NDI0K41gVbWqfkkaHj15IzU7PtMoelkzyKp8TOaFQ3s=" crossorigin=""></script>

As Frido suggested, we don't link out to externally hosted static files. Lots of reasons for that.

Koha already has leaflet.js and leaflet.css although a much older version: 1.0.3+ed36a04

I'd suggest opening a separate issue to upgrade Leaflet and then having that be a dependency of this issue.
Comment 16 Katrin Fischer 2023-09-10 22:55:44 UTC
I think we should definitely not link to external files but keep shipping our own. I believe updating the ones we ship would be a good first step here and hopefully not too hard. I think maybe they are used for Mana?
Comment 17 Mark Hofstetter 2024-04-09 11:02:54 UTC
git@github.com:HKS3/koha-branches.git

it's in the branch 

topic-es-2312

for those who want to see it, please send me your github id
Comment 18 Mark Hofstetter 2024-04-11 08:17:59 UTC
I now created a public git branch 

https://github.com/HKS3/koha-devel/tree/topic-es-2312

working on a cypress test
Comment 19 Mark Hofstetter 2024-04-11 09:16:25 UTC
Created attachment 164666 [details] [review]
Bug 31652: Add geo-search

This patch adds geosearch to Koha (using Elasticsearch 7). ElasticSearch
search_mappings get new types to store lat/lon, which can be indexed
from MARC 034$s and 034$t. There is a small change to the DB to allow a
new value in search_field.type ENUM.

The QueryBuilder is extended to allow for building advanced
ElasticSearch Querys (eg geo_distance) that cannot be represented in a
simple string query. The UI for searching (including showing the results
on a OSM/Leaflet map) is implemented in a separate plugin
(https://github.com/HKS3/HKS3GeoSearch)

Test Plan:

* make sure you're running ElasticSearch 7
  (eg via `curl http://es:9200?pretty | grep number`)
* apply patch
* Set system preference "SaerchEngine" to "Elasticsearch"
* got to a Framework, check Editor for 034$s and 034$t and save
* got to some books (in the correct framework) and enter some lat and lon into 034$s and 034$t (for example lat=48.216, lon=16.395)
* Run the elasticsearch indexer, maybe limited on the books you edited (-bn 123 -bn 456):
  misc/search_tools/rebuild_elasticsearch.pl -b -v
* You can check if the indexing worked by inspecting the document in elasticsearch:
  * get the biblionumber (eg 123)
  * curl http://es:9200/koha_kohadev_biblios/_doc/123?pretty | grep -A5 geolocation
  * You should get back a JSON fragment containing the lat/lon you stored
* You can query elasticsearch directly:
  * Run the following curl command, but adapt the value for lat/lng and/or the distance (in meters)
  * curl -X GET "http://es:9200/koha_kohadev_biblios/_search?pretty" -H 'Content-Type: application/json' -d '{"query": {"bool":{"must":{"match_all":{}},"filter":{"geo_distance":{"distance":100000,"geolocation":{"lat":48.2,"lon":16.4}}}}}}'
* To run the search via Koha, you need to either install and use https://github.com/HKS3/HKS3GeoSearch or create a handcrafted query string:
  * handcrafted query string:
    * /cgi-bin/koha/opac-search.pl?advsearch=1&idx=geolocation&q=lat:48.25+lng:18.35+distance:100km&do=Search
  * HKS3GeoSearch
    * install the plugin and enable it
    * got to OPAC / Advanced Search
    * There is a new input box "Geographic Search" where you can enter lat/long/radius
    * On the search result page a map is shown with pins for each found biblioitem

Sponsored-by: ZAMG - Zentralanstalt für Meterologie und Geodynamik, Austria - https://www.zamg.ac.at/
Sponsored-by: Geosphere - https://www.geosphere.at/
Comment 20 Nick Clemens (kidclamp) 2024-04-11 13:19:00 UTC
Created attachment 164689 [details] [review]
Bug 31652: Add geo-search

This patch adds geosearch to Koha (using Elasticsearch 7). ElasticSearch
search_mappings get new types to store lat/lon, which can be indexed
from MARC 034$s and 034$t. There is a small change to the DB to allow a
new value in search_field.type ENUM.

The QueryBuilder is extended to allow for building advanced
ElasticSearch Querys (eg geo_distance) that cannot be represented in a
simple string query. The UI for searching (including showing the results
on a OSM/Leaflet map) is implemented in a separate plugin
(https://github.com/HKS3/HKS3GeoSearch)

Test Plan:

* make sure you're running ElasticSearch 7
  (eg via `curl http://es:9200?pretty | grep number`)
* apply patch
* Set system preference "SaerchEngine" to "Elasticsearch"
* got to a Framework, check Editor for 034$s and 034$t and save
* got to some books (in the correct framework) and enter some lat and lon into 034$s and 034$t (for example lat=48.216, lon=16.395)
* Run the elasticsearch indexer, maybe limited on the books you edited (-bn 123 -bn 456):
  misc/search_tools/rebuild_elasticsearch.pl -b -v
* You can check if the indexing worked by inspecting the document in elasticsearch:
  * get the biblionumber (eg 123)
  * curl http://es:9200/koha_kohadev_biblios/_doc/123?pretty | grep -A5 geolocation
  * You should get back a JSON fragment containing the lat/lon you stored
* You can query elasticsearch directly:
  * Run the following curl command, but adapt the value for lat/lng and/or the distance (in meters)
  * curl -X GET "http://es:9200/koha_kohadev_biblios/_search?pretty" -H 'Content-Type: application/json' -d '{"query": {"bool":{"must":{"match_all":{}},"filter":{"geo_distance":{"distance":100000,"geolocation":{"lat":48.2,"lon":16.4}}}}}}'
* To run the search via Koha, you need to either install and use https://github.com/HKS3/HKS3GeoSearch or create a handcrafted query string:
  * handcrafted query string:
    * /cgi-bin/koha/opac-search.pl?advsearch=1&idx=geolocation&q=lat:48.25+lng:18.35+distance:100km&do=Search
  * HKS3GeoSearch
    * install the plugin and enable it
    * got to OPAC / Advanced Search
    * There is a new input box "Geographic Search" where you can enter lat/long/radius
    * On the search result page a map is shown with pins for each found biblioitem

Sponsored-by: ZAMG - Zentralanstalt für Meterologie und Geodynamik, Austria - https://www.zamg.ac.at/
Sponsored-by: Geosphere - https://www.geosphere.at/
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Comment 21 Nick Clemens (kidclamp) 2024-04-11 13:19:04 UTC
Created attachment 164690 [details] [review]
Bug 31652: (follow-up) Fix update and remove fixed fixme

Update test count as well

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Comment 22 Nick Clemens (kidclamp) 2024-04-11 13:20:11 UTC
Worked with Mark and Martin to move this forward - this version simply adds the ability to do a geo-search - it would need work for an interface to make the searching exposed, but I think this will provide a useful base. Passing QA
Comment 23 Katrin Fischer 2024-04-26 16:23:06 UTC
Please check:

1) Are the changes to name / label in kohastructure intended?

   `id` int(11) NOT NULL AUTO_INCREMENT,
-  `name` varchar(255) NOT NULL COMMENT 'the name of the field as it will be stored in the search engine',
-  `label` varchar(255) NOT NULL COMMENT 'the human readable name of the field, for display',
-  `type` enum('','string','date','number','boolean','sum','isbn','stdno','year','callnumber') NOT NULL COMMENT 'what type of data this holds, relevant when storing it in the search engine',
+  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'the name of the field as it will be stored in the search engine',
+  `label` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'the human readable name of the field, for display',
+  `type` enum('','string','date','number','boolean','sum','isbn','stdno','year','callnumber','geo_point') COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'what type of data this holds, relevant when storing it in the search engine',

2) System preference GeoSearchEnabled

I believe this should be removed and due to rewrites? There is no other mention of this pref.

+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/searching.pref
@@ -99,6 +99,13 @@ Searching:
             - "List of search fields (separated by | or ,) that should not be autotruncated by Elasticsearch even if <a href='/cgi-bin/koha/admin/preferences.pl?op=search&searchfield=QueryAutoTruncate'>QueryAutoTruncate</a> is set to 'Yes':"
             - pref: ESPreventAutoTruncate
               class: long
+        -
+            - pref: GeoSearchEnabled
+              type: boolean
+              choices:
+                  1: Enable
+                  0: Disable
+            - 'GeoSearch via Elasticsearch'
     Search form:

3) Capitalization

Usually I just fix these sneakily, but so we don't forget with the other points:

+                                              <option value="geo_point" selected="selected">Geo Point</option>

We only capitalize the first word, so this should read: Geo Point (I know Call Number in the same spot is wrong as well, but no need to add more)


Please fix!
Comment 24 Thomas Klausner 2024-04-27 16:49:22 UTC
Created attachment 165707 [details] [review]
Bug 31652: Add geo-search

This patch adds geosearch to Koha (using Elasticsearch 7). ElasticSearch
search_mappings get new types to store lat/lon, which can be indexed
from MARC 034$s and 034$t. There is a small change to the DB to allow a
new value in search_field.type ENUM.

The QueryBuilder is extended to allow for building advanced
ElasticSearch Querys (eg geo_distance) that cannot be represented in a
simple string query. The UI for searching (including showing the results
on a OSM/Leaflet map) is implemented in a separate plugin
(https://github.com/HKS3/HKS3GeoSearch)

Test Plan:

* make sure you're running ElasticSearch 7
  (eg via `curl http://es:9200?pretty | grep number`)
* apply patch
* Set system preference "SaerchEngine" to "Elasticsearch"
* got to a Framework, check Editor for 034$s and 034$t and save
* got to some books (in the correct framework) and enter some lat and lon into 034$s and 034$t (for example lat=48.216, lon=16.395)
* Run the elasticsearch indexer, maybe limited on the books you edited (-bn 123 -bn 456):
  misc/search_tools/rebuild_elasticsearch.pl -b -v
* You can check if the indexing worked by inspecting the document in elasticsearch:
  * get the biblionumber (eg 123)
  * curl http://es:9200/koha_kohadev_biblios/_doc/123?pretty | grep -A5 geolocation
  * You should get back a JSON fragment containing the lat/lon you stored
* You can query elasticsearch directly:
  * Run the following curl command, but adapt the value for lat/lng and/or the distance (in meters)
  * curl -X GET "http://es:9200/koha_kohadev_biblios/_search?pretty" -H 'Content-Type: application/json' -d '{"query": {"bool":{"must":{"match_all":{}},"filter":{"geo_distance":{"distance":100000,"geolocation":{"lat":48.2,"lon":16.4}}}}}}'
* To run the search via Koha, you need to either install and use https://github.com/HKS3/HKS3GeoSearch or create a handcrafted query string:
  * handcrafted query string:
    * /cgi-bin/koha/opac-search.pl?advsearch=1&idx=geolocation&q=lat:48.25+lng:18.35+distance:100km&do=Search
  * HKS3GeoSearch
    * install the plugin and enable it
    * got to OPAC / Advanced Search
    * There is a new input box "Geographic Search" where you can enter lat/long/radius
    * On the search result page a map is shown with pins for each found biblioitem

Sponsored-by: ZAMG - Zentralanstalt für Meterologie und Geodynamik, Austria - https://www.zamg.ac.at/
Sponsored-by: Geosphere - https://www.geosphere.at/
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Additional finetuning:

- Fix update and remove fixed fixme
- Update test count as well
- fix last small issues raised in Comment 23
Comment 25 Thomas Klausner 2024-04-27 16:58:15 UTC
Created attachment 165708 [details] [review]
Bug 31652: Add geo-search

This patch adds geosearch to Koha (using Elasticsearch 7). ElasticSearch
search_mappings get new types to store lat/lon, which can be indexed
from MARC 034$s and 034$t. There is a small change to the DB to allow a
new value in search_field.type ENUM.

The QueryBuilder is extended to allow for building advanced
ElasticSearch Querys (eg geo_distance) that cannot be represented in a
simple string query. The UI for searching (including showing the results
on a OSM/Leaflet map) is implemented in a separate plugin
(https://github.com/HKS3/HKS3GeoSearch)

Test Plan:

* make sure you're running ElasticSearch 7
  (eg via `curl http://es:9200?pretty | grep number`)
* apply patch
* got to a Framework, check Editor for 034$s and 034$t and save
* got to some books (in the correct framework) and enter some lat and lon into 034$s and 034$t (for example lat=48.216, lon=16.395)
* Run the elasticsearch indexer, maybe limited on the books you edited (-bn 123 -bn 456):
  misc/search_tools/rebuild_elasticsearch.pl -b -v
* You can check if the indexing worked by inspecting the document in elasticsearch:
  * get the biblionumber (eg 123)
  * curl http://es:9200/koha_kohadev_biblios/_doc/123?pretty | grep -A5 geolocation
  * You should get back a JSON fragment containing the lat/lon you stored
* You can query elasticsearch directly:
  * Run the following curl command, but adapt the value for lat/lng and/or the distance (in meters)
  * curl -X GET "http://es:9200/koha_kohadev_biblios/_search?pretty" -H 'Content-Type: application/json' -d '{"query": {"bool":{"must":{"match_all":{}},"filter":{"geo_distance":{"distance":100000,"geolocation":{"lat":48.2,"lon":16.4}}}}}}'
* To run the search via Koha, you need to either install and use https://github.com/HKS3/HKS3GeoSearch or create a handcrafted query string:
  * handcrafted query string:
    * /cgi-bin/koha/opac-search.pl?advsearch=1&idx=geolocation&q=lat:48.25+lng:18.35+distance:100km&do=Search
  * HKS3GeoSearch
    * install the plugin and enable it
    * got to OPAC / Advanced Search
    * There is a new input box "Geographic Search" where you can enter lat/long/radius
    * On the search result page a map is shown with pins for each found biblioitem

Sponsored-by: ZAMG - Zentralanstalt für Meterologie und Geodynamik, Austria - https://www.zamg.ac.at/
Sponsored-by: Geosphere - https://www.geosphere.at/
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Additional finetuning:

- Fix update and remove fixed fixme
- Update test count as well
- fix last small issues raised in Comment 23
Comment 26 Katrin Fischer 2024-04-27 17:23:49 UTC
Please switch back to PQA when you are done!
Comment 27 Thomas Klausner 2024-04-27 20:00:11 UTC
I assume you mean status = "Passed QA"? I've now set it to this :-)
Comment 28 David Nind 2024-04-27 20:21:55 UTC
I thought I would have a go at testing, even though it has passed QA.

Unfortunately, the patches don't apply - sha1/fake ancestor error:

git bz apply 31652

Bug 31652 - Add geo-search

164690 - Bug 31652: (follow-up) Fix update and remove fixed fixme
165708 - Bug 31652: Add geo-search

Apply? [(y)es, (n)o, (i)nteractive] y
Applying: Bug 31652: (follow-up) Fix update and remove fixed fixme
error: sha1 information is lacking or useless (Koha/SearchEngine/Elasticsearch/Search.pm).
error: could not build fake ancestor
Patch failed at 0001 Bug 31652: (follow-up) Fix update and remove fixed fixme
Comment 29 Katrin Fischer 2024-04-28 08:51:09 UTC
I think the patch sequence is wrong now. I was going to do 2 git bz apply -i here to fix on applying.
Comment 30 David Nind 2024-04-28 09:52:36 UTC
(In reply to Katrin Fischer from comment #29)
> I think the patch sequence is wrong now. I was going to do 2 git bz apply -i
> here to fix on applying.

I tried that, but it didn't work for me 8-)
Comment 31 Thomas Klausner 2024-04-28 16:27:49 UTC
When creating the current patch, I did a rebase on the fresh main and squashed the three commits (original, "(follow-up)" and my fixes according to Katrins comment) into one commit.

So there should only be one patch:
  Bug 31652: Add geo-search (14.16 KB, patch)
  2024-04-27 16:58 UTC, Thomas Klausner 


Not sure how/if I can obsolte this one:
  Bug 31652: (follow-up) Fix update and remove fixed fixme (2.72 KB, patch)
  2024-04-11 13:19 UTC, Nick Clemens 

Or if it is possible (or how to do it) to tell git bz to only use my rebased/squashed patch
Comment 32 David Nind 2024-04-28 18:08:47 UTC
(In reply to Thomas Klausner from comment #31)
> When creating the current patch, I did a rebase on the fresh main and
> squashed the three commits (original, "(follow-up)" and my fixes according
> to Katrins comment) into one commit.
> 
> So there should only be one patch:
>   Bug 31652: Add geo-search (14.16 KB, patch)
>   2024-04-27 16:58 UTC, Thomas Klausner 
> 
> 
> Not sure how/if I can obsolte this one:
>   Bug 31652: (follow-up) Fix update and remove fixed fixme (2.72 KB, patch)
>   2024-04-11 13:19 UTC, Nick Clemens 
> 
> Or if it is possible (or how to do it) to tell git bz to only use my
> rebased/squashed patch

You can obsolete the patch from Bugzilla:
1. Under the attachments section, click the 'Details' link beside the patch to obsolete.
2. In the title/header for the patch, click 'Edit details' again.
3. Click the tick box beside 'obsolete'.
4. Add a comment.
5. Click 'Submit'.
Comment 33 Thomas Klausner 2024-04-28 19:38:37 UTC
I obsolted the old patch, now git bz works again:

  kohadev-koha@kohadevbox:koha(another_31652_try)$ git bz apply 31652

  Bug 31652 - Add geo-search

  165708 - Bug 31652: Add geo-search

  Apply? [(y)es, (n)o, (i)nteractive] y
  Applying: Bug 31652: Add geo-search


Please try again
Comment 34 David Nind 2024-04-28 21:24:30 UTC
Hi Thomas.

I can confirm the bug now applies!

I had a go at testing (not that this is required, as it has already passed QA). Things I noted:
- database update required after applying the patch
- also needed to reset the Elasticsearch mappings
- I didn't get any search results in the OPAC
- wasn't sure how to install the HKS3GeoSearch plugin 

Here are my testing notes (using KTD):
1. Start up KTD with Elasticsearch: ktd --es7 up
2. Apply the patch
3. Update the database: updatedatabase
4. Restart everything
5. Edit the books framework and make 034$s and 034$t visible in the editor
6. Add example coordinates for a couple of records (I added lat=48.216 and lon=16.395 to records 262 and 126)
7. Reset the Elasticsearch mappings (Administration > Catalog > Search engine configuration (Elasticsearch) > Reset mappings)
8. Rebuild the search index: koha-elasticsearch --rebuild -d -b -a kohadev
9. Check if indexing working: curl http://es:9200/koha_kohadev_biblios/_doc/123?pretty | grep -A5 geolocation
- The result I got:

curl http://es:9200/koha_kohadev_biblios/_doc/262?pretty | grep -A5 geolocation
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1078    "geolocation" : [ 0      0      0 --:--:-- --:--:-- --:--:--     0
3      {
         "lon" : "16.395",
         "lat" : "48.216"
1      }
0    ],
0 10783    0     0  2164k      0 --:--:-- --:--:-- --:--:-- 2632k

10. Query Elasticsearch directly:

- Result:
curl -X GET "http://es:9200/koha_kohadev_biblios/_search?pretty" -H 'Content-Type: application/json' -d '{"query": {"bool":{"must":{"match_all":{}},"filter":{"geo_distance":{"distance":100000,"geolocation":{"lat":48.2,"lon":16.4}}}}}}'
{
....

A lot of information returned - basically the details for the two records that I added the coordinates to.
...
}

11. OPAC query:

http://127.0.0.1:8080/cgi-bin/koha/opac-search.pl?advsearch=1&idx=geolocation&q=lat:48.25+lng:18.35+distance:100km&do=Search
- Puts details in the search field: lat:48.25 lng:18.35 distance:100km
- Results displayed:

No results found!
No results found for that in catalog. 

12. HKS3GeoSearch Koha Plugin:
- No release available. Does this mean I have to clone the repository into the plugins folder? (I tried cloning the repository into /var/lib/koha/kohadev/plugins, but nothing appears in the list of plugins.)

David
Comment 35 Katrin Fischer 2024-04-29 13:20:28 UTC
Pushed for 24.05!

Well done everyone, thank you!
Comment 36 Fridolin Somers 2024-05-24 10:02:17 UTC
Not backported to 23.11.x
Comment 37 Katrin Fischer 2024-05-24 10:22:33 UTC
I've tried to add some release notes - please correct/extend as you see fit.
Comment 38 David Nind 2024-05-24 22:51:56 UTC
I've made some edits to the release notes.

I was able to install the plugin, having the kpz available made that much easier.

Results from searching:
1. I used the latitude and longitude from the test plan and added these to some records.
2. The curl queries return the results as in the test plan (the lat and lon values are in the Elasticsearch details).
3. If I search in the OPAC using the example query, the map appears, but it says no search results found.
4. If I search for a word in the records, the map appears with search results, but there are no pins showing on the map.

Is there something else I need to do?

Here is what I did (using KTD on the latest main, tested using ktd --es7 up and jktd --es8 up):
1. Installed the plugin, enabled, and restarted everything.
2. Made 034$s and 034$t visible in the editor for the BKS framework.
3. Added some latitude and longitude values to two records (I used the values from the test plan - lat=48.216, lon=16.395).
4. Checked that the mappings exist under Search engine configuration (Elasticsearch) for geolocation_lat and geolocation_lon. They did, so I didn't reset the mappings (I think that is right).
5. I reindexed, even though I'm not sure that this is required (koha-elasticsearch --rebuild -d -b -a kohadev).
6. OPAC keyword search - searched with a keyword that returns the records I added values to: 
   - Result ==> Map appears (without pins), with the search results including the records with the latitude and longitude values
7. Direct query (from the test plan): <opac-url>:8080/cgi-bin/koha/opac-search.pl?advsearch=1&idx=geolocation&q=lat:48.25+lng:18.35+distance:100km&do=Search
  . Result: Map appears, but no results
8. If I change the 100km to 1000km in the query, then the results are listed and there is a pin for one of the records (but not the other).
9. Something else I noted: These details appear in the search box - lat:48.25 lng:18.35 distance:1000km. If I click the search button again, the URL is changed so that it has %3A instead of a +, and there are no results (<opac-url>:8080/cgi-bin/koha/opac-search.pl?idx=&q=lat%3A48.25+lng%3A18.35+distance%3A1000km&weight_search=1).

Apart from a very basic knowledge about latitude and longitude, I'm guessing the query is saying give me any records that are X km from the latitude and longitude.

Are you able to provide some latitude and longitude values that could be added to some records, and the queries to use to display some pins on the map? (For queries, either the values to enter in the advanced search form field, or the direct query.)

This would make it easy for people to try it out with something that works. (And it would be great to demonstrate all your hard work!)
Comment 39 David Nind 2024-05-24 22:58:35 UTC
Just noting this presentation, will be of great help when updating the documentation (even though some things have changed since it was shared): https://domm.plix.at/talks/2023_helsinki_koha_geosearch/
Comment 40 Mark Hofstetter 2024-05-26 21:27:08 UTC
hi,

I added 

https://github.com/HKS3/HKS3GeoSearch/blob/dev/TO_TEST.md

https://github.com/HKS3/HKS3GeoSearch/tree/dev/scripts

https://github.com/HKS3/HKS3GeoSearch/blob/dev/scripts/insert_geo_data.pl

which adds 3 geo data point to 3 books (hardcoded for the dev install)

so if now do a search around 

http://kohadev.mydnsname.org:8080/cgi-bin/koha/opac-search.pl?advsearch=1&idx=geolocation&q=lat:48+lng:16+distance:50km&do=Search

3 books should be found

we are working on the a point and click interface to search
Comment 41 David Nind 2024-05-27 22:30:27 UTC
Thanks Mark, that really helps!

I added the example co-ordinates to the example records, and used the query - the pins were nicely displayed in the OPAC.

Feel free to cc me in on any future bugs, as I'm happy to help with testing and sign off.

David
Comment 42 Heather 2024-05-28 14:15:08 UTC
Created attachment 167223 [details]
MARC21 cartographic bib record with coordinates

Bib record 446 for a nautical chart, from our catalog, keys.bywatersolutions.com
Comment 43 Heather 2024-05-28 14:16:04 UTC
Created attachment 167224 [details]
Another MARC21 catalog record with coordinates

Bib record 447 for a nautical chart with coordinates, from our catalog, keys.bywatersolutions.com
Comment 44 Heather 2024-05-28 14:17:01 UTC
Created attachment 167225 [details]
A third MARC21 bib record with coordinates

Bib record no.41753 for a nautical chart, with coordinates, from our catalog, keys.bywatersolutions.com
Comment 45 Heather 2024-05-28 14:19:11 UTC
Hi!  I've uploaded three fully cataloged cartographic records with correctly entered coordinates for nautical charts from our catalog, in case they will help!  And for extra fun, they link to onlin images of the charts in case anyone would like to take a look.:)

I am super excited about this development--I've been taking the time to enter coordinates into cartographic records for years and years and years with hopes that they'd be useful in the future!

Cheerio!
h2, A Real Cataloger:)
Comment 46 Caroline Cyr La Rose 2024-07-25 20:31:56 UTC
This bug seems to change things mostly in the "backend" of Koha, paving the way for further changes. But right now, nothing much to change in the manual as far as I could see. I added the new mappings, but that's it.
Comment 47 David Nind 2024-07-25 21:56:04 UTC
(In reply to Caroline Cyr La Rose from comment #46)
> This bug seems to change things mostly in the "backend" of Koha, paving the
> way for further changes. But right now, nothing much to change in the manual
> as far as I could see. I added the new mappings, but that's it.

That's the case (see comments 37 to 40 for how I tested, after it was pushed). It is usable now using the plugin mentioned in the release notes.