1 package Koha::ElasticSearch;
3 # Copyright 2013 Catalyst IT
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use base qw(Class::Accessor);
28 use Data::Dumper; # TODO remove
30 __PACKAGE__->mk_ro_accessors(qw( index ));
32 # Constants to refer to the standard index names
33 Readonly our $BIBLIOS_INDEX => 'biblios';
34 Readonly our $AUTHORITIES_INDEX => 'authorities';
38 Koha::ElasticSearch - Base module for things using elasticsearch
46 The name of the index to use, generally 'biblios' or 'authorities'.
56 my $self = $class->SUPER::new(@_);
57 # Check for a valid index
58 croak('No index name provided') unless $self->index;
62 =head2 get_elasticsearch_params
64 my $params = $self->get_elasticsearch_params();
66 This provides a hashref that contains the parameters for connecting to the
67 ElasicSearch servers, in the form:
70 'servers' => ['127.0.0.1:9200', 'anotherserver:9200'],
71 'index_name' => 'koha_instance',
74 This is configured by the following in the C<config> block in koha-conf.xml:
77 <server>127.0.0.1:9200</server>
78 <server>anotherserver:9200</server>
79 <index_name>koha_instance</index_name>
84 sub get_elasticsearch_params {
87 # Copy the hash so that we're not modifying the original
88 my $conf = C4::Context->config('elasticsearch');
89 die "No 'elasticsearch' block is defined in koha-conf.xml.\n" if ( !$conf );
90 my $es = { %{ $conf } };
92 # Helpfully, the multiple server lines end up in an array for us anyway
93 # if there are multiple ones, but not if there's only one.
94 my $server = $es->{server};
96 if ( ref($server) eq 'ARRAY' ) {
98 # store it called 'servers'
99 $es->{servers} = $server;
102 $es->{servers} = [$server];
105 die "No elasticsearch servers were specified in koha-conf.xml.\n";
107 die "No elasticserver index_name was specified in koha-conf.xml.\n"
108 if ( !$es->{index_name} );
109 # Append the name of this particular index to our namespace
110 $es->{index_name} .= '_' . $self->index;
114 =head2 get_elasticsearch_settings
116 my $settings = $self->get_elasticsearch_settings();
118 This provides the settings provided to elasticsearch when an index is created.
119 These can do things like define tokenisation methods.
121 A hashref containing the settings is returned.
125 sub get_elasticsearch_settings {
128 # Ultimately this should come from a file or something, and not be
135 tokenizer => 'keyword',
136 filter => 'lowercase',
138 analyser_standard => {
139 tokenizer => 'standard',
140 filter => 'lowercase',
149 =head2 get_elasticsearch_mappings
151 my $mappings = $self->get_elasticsearch_mappings();
153 This provides the mappings that get passed to elasticsearch when an index is
158 sub get_elasticsearch_mappings {
166 include_in_all => "false",
172 $self->_foreach_mapping(
174 my ( undef, $name, $type, $facet ) = @_;
176 # TODO if this gets any sort of complexity to it, it should
177 # be broken out into its own function.
179 # TODO be aware of date formats, but this requires pre-parsing
180 # as ES will simply reject anything with an invalid date.
185 $mappings->{data}{properties}{$name} = {
186 search_analyzer => "analyser_standard",
187 index_analyzer => "analyser_standard",
191 search_analyzer => "analyser_phrase",
192 index_analyzer => "analyser_phrase",
197 $mappings->{data}{properties}{$name}{null_value} = 0
198 if $type eq 'boolean';
200 $mappings->{data}{properties}{ $name . '__facet' } = {
202 index => "not_analyzed",
210 # Provides the rules for data conversion.
211 sub get_fixer_rules {
214 my $marcflavour = lc C4::Context->preference('marcflavour');
216 $self->_foreach_mapping(
218 my ( undef, $name, $type, $facet, $marcs ) = @_;
219 my $field = $marcs->{$marcflavour};
220 return unless defined $marcs->{$marcflavour};
223 # There's a bug when using 'split' with something that
225 # The split makes everything into nested arrays, but that's not
226 # really a big deal, ES doesn't mind.
227 $options = '-split => 1' unless $field =~ m|_/| || $type eq 'sum';
228 push @rules, "marc_map('$field','${name}', $options)";
230 push @rules, "marc_map('$field','${name}__facet', $options)";
232 if ( $type eq 'boolean' ) {
234 # boolean gets special handling, basically if it doesn't exist,
235 # it's added and set to false. Otherwise we can't query it.
237 "unless exists('$name') add_field('$name', 0) end";
239 if ($type eq 'sum' ) {
240 push @rules, "sum('$name')";
248 =head2 _foreach_mapping
250 $self->_foreach_mapping(
252 my ( $id, $name, $type, $facet, $marcs ) = @_;
253 my $marc = $marcs->{marc21};
257 This allows you to apply a function to each entry in the elasticsearch mappings
258 table, in order to build the mappings for whatever is needed.
260 In the provided function, the files are:
266 An ID number, corresponding to the entry in the database.
270 The field name for elasticsearch (corresponds to the 'mapping' column in the
275 The type for this value, e.g. 'string'.
279 True if this value should be facetised. This only really makes sense if the
280 field is understood by the facet processing code anyway.
284 A hashref containing the MARC field specifiers for each MARC type. It's quite
285 possible for this to be undefined if there is otherwise an entry in a
292 sub _foreach_mapping {
293 my ( $self, $sub ) = @_;
295 # TODO use a caching framework here
296 my $database = Koha::Database->new();
297 my $schema = $database->schema();
299 $schema->resultset('ElasticsearchMapping')
300 ->search( { indexname => $self->index } );
301 for my $row ( $rs->all ) {
308 marc21 => $row->marc21,
309 unimarc => $row->unimarc,
310 normarc => $row->normarc
324 =item Chris Cormack C<< <chrisc@catalyst.net.nz> >>
326 =item Robin Sheat C<< <robin@catalyst.net.nz> >>