From 78850b4a15b6751d69796c8558ad6c44703de374 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Cohen=20Arazi?= Date: Tue, 28 Apr 2026 09:05:47 -0300 Subject: [PATCH] Bug 42485: Disable date and numeric detection on ES mappings When ElasticsearchMARCFormat is set to ARRAY, the marc_data_array field uses dynamic:true mapping. Elasticsearch auto-detects field types based on the first document indexed. If a MARC subfield value looks like a date (e.g. 2024-01-15 in 952$e), ES locks that subfield to type date. Subsequent records with non-date values in the same subfield fail to index with a document_parsing_exception. This patch sets date_detection and numeric_detection to false at the mapping root level, so ES treats all dynamically mapped fields as text instead of guessing types. Test plan: 1. $ ktd --search-engine es8 --name test-es --proxy up -d $ ktd --name test-es --wait-ready 180 $ ktd --name test-es --shell 2. k$ koha-mysql kohadev -e "UPDATE systempreferences SET value='ARRAY' WHERE variable='ElasticsearchMARCFormat';" k$ echo 'flush_all' | nc -q1 memcached 11211 k$ koha-mysql kohadev -e "DELETE FROM biblio_metadata; DELETE FROM items; DELETE FROM biblioitems; DELETE FROM biblio;" k$ rebuild_elasticsearch.pl -d -r --biblios 3. Add a record with date-like 952$e and index it: k$ koha-shell kohadev -p -c 'perl -MC4::Biblio -MMARC::Record -MMARC::Field -MKoha::Item -e " my \$r = MARC::Record->new(); \$r->append_fields( MARC::Field->new(q{245},q{1},q{0},a=>q{Record one}), MARC::Field->new(q{942},q{ },q{ },c=>q{BK})); my (\$bn) = C4::Biblio::AddBiblio(\$r, q{}); Koha::Item->new({biblionumber=>\$bn, biblioitemnumber=>\$bn, homebranch=>q{CPL}, holdingbranch=>q{CPL}, itype=>q{BK}, booksellerid=>q{2024-01-15}})->store; print qq{Added biblio \$bn\n};"' k$ rebuild_elasticsearch.pl --biblios -bn 4. Check the mapping for 952$e: k$ curl -s es:9200/koha_kohadev_biblios/_mapping/field/\ marc_data_array.fields.952.subfields.e => FAIL: type is "date" (should be "text") 5. Add a record with non-date 952$e and index it: k$ koha-shell kohadev -p -c 'perl -MC4::Biblio -MMARC::Record -MMARC::Field -MKoha::Item -e " my \$r = MARC::Record->new(); \$r->append_fields( MARC::Field->new(q{245},q{1},q{0},a=>q{Record two}), MARC::Field->new(q{942},q{ },q{ },c=>q{BK})); my (\$bn) = C4::Biblio::AddBiblio(\$r, q{}); Koha::Item->new({biblionumber=>\$bn, biblioitemnumber=>\$bn, homebranch=>q{CPL}, holdingbranch=>q{CPL}, itype=>q{BK}, booksellerid=>q{My Vendor Name}})->store; print qq{Added biblio \$bn\n};"' k$ rebuild_elasticsearch.pl --biblios -bn -v -v -v => FAIL: document_parsing_exception - failed to parse date field [My Vendor Name] 6. Apply this patch, repeat from step 2 7. Repeat step 4: => SUCCESS: type is "text" 8. Repeat step 5: => SUCCESS: No indexing errors 9. Sign off :-D Signed-off-by: Andrew Fuerste Henry Signed-off-by: Martin Renvoize Signed-off-by: Lucas Gass --- Koha/SearchEngine/Elasticsearch.pm | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Koha/SearchEngine/Elasticsearch.pm b/Koha/SearchEngine/Elasticsearch.pm index ef5976c263..577dcda12b 100644 --- a/Koha/SearchEngine/Elasticsearch.pm +++ b/Koha/SearchEngine/Elasticsearch.pm @@ -272,6 +272,15 @@ sub get_elasticsearch_mappings { $mappings->{properties}{'match-heading'} = _get_elasticsearch_field_config( 'search', 'text' ); $mappings->{properties}{'subject-heading-thesaurus'} = _get_elasticsearch_field_config( 'search', 'text' ); } + + # Disable date and numeric detection to prevent Elasticsearch from + # auto-detecting field types in marc_data_array. Without this, a + # subfield value that looks like a date (e.g. "2024-01-15") causes + # ES to lock that subfield to type "date", and subsequent non-date + # values fail to index. (Bug 42485) + $mappings->{date_detection} = JSON::PP::false; + $mappings->{numeric_detection} = JSON::PP::false; + $all_mappings{ $self->index } = $mappings; } $self->sort_fields( \%{ $sort_fields{ $self->index } } );