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 'nodes' => ['127.0.0.1:9200', 'anotherserver:9200'],
71 'index_name' => 'koha_instance_index',
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 'nodes' (which is used by newer Search::Elasticsearch)
99 $es->{nodes} = $server;
102 $es->{nodes} = [$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",
170 search_analyzer => "analyser_phrase",
171 index_analyzer => "analyser_phrase",
177 my $marcflavour = lc C4::Context->preference('marcflavour');
178 $self->_foreach_mapping(
180 my ( $name, $type, $facet, $marc_type ) = @_;
181 return if $marc_type ne $marcflavour;
182 # TODO if this gets any sort of complexity to it, it should
183 # be broken out into its own function.
185 # TODO be aware of date formats, but this requires pre-parsing
186 # as ES will simply reject anything with an invalid date.
191 $mappings->{data}{properties}{$name} = {
192 search_analyzer => "analyser_standard",
193 index_analyzer => "analyser_standard",
197 search_analyzer => "analyser_phrase",
198 index_analyzer => "analyser_phrase",
200 copy_to => "_all.phrase",
204 $mappings->{data}{properties}{$name}{null_value} = 0
205 if $type eq 'boolean';
207 $mappings->{data}{properties}{ $name . '__facet' } = {
209 index => "not_analyzed",
217 # Provides the rules for data conversion.
218 sub get_fixer_rules {
221 my $marcflavour = lc C4::Context->preference('marcflavour');
223 $self->_foreach_mapping(
225 my ( $name, $type, $facet, $marc_type, $marc_field ) = @_;
226 return if $marc_type ne $marcflavour;
229 # There's a bug when using 'split' with something that
231 # The split makes everything into nested arrays, but that's not
232 # really a big deal, ES doesn't mind.
233 $options = '-split => 1' unless $marc_field =~ m|_/| || $type eq 'sum';
234 push @rules, "marc_map('$marc_field','${name}', $options)";
236 push @rules, "marc_map('$marc_field','${name}__facet', $options)";
238 if ( $type eq 'boolean' ) {
240 # boolean gets special handling, basically if it doesn't exist,
241 # it's added and set to false. Otherwise we can't query it.
243 "unless exists('$name') add_field('$name', 0) end";
245 if ($type eq 'sum' ) {
246 push @rules, "sum('$name')";
253 =head2 _foreach_mapping
255 $self->_foreach_mapping(
257 my ( $name, $type, $facet, $marc_type, $marc_field ) = @_;
258 return unless $marc_type eq 'marc21';
259 print "Data comes from: " . $marc_field . "\n";
263 This allows you to apply a function to each entry in the elasticsearch mappings
264 table, in order to build the mappings for whatever is needed.
266 In the provided function, the files are:
272 The field name for elasticsearch (corresponds to the 'mapping' column in the
277 The type for this value, e.g. 'string'.
281 True if this value should be facetised. This only really makes sense if the
282 field is understood by the facet processing code anyway.
286 A string that indicates the MARC type that this mapping is for, e.g. 'marc21',
287 'unimarc', 'normarc'.
291 A string that describes the MARC field that contains the data to extract.
292 These are of a form suited to Catmandu's MARC fixers.
298 sub _foreach_mapping {
299 my ( $self, $sub ) = @_;
301 # TODO use a caching framework here
302 my $database = Koha::Database->new();
303 my $schema = $database->schema();
305 $schema->resultset('SearchMarcMap')
306 ->search( { index_name => $self->index } );
307 for my $row ( $rs->all ) {
308 my $marc_type = $row->marc_type;
309 my $marc_field = $row->marc_field;
310 my $facet = $row->facet;
311 my $search_field = $row->search_fields();
312 for my $sf ( $search_field->all ) {
332 =item Chris Cormack C<< <chrisc@catalyst.net.nz> >>
334 =item Robin Sheat C<< <robin@catalyst.net.nz> >>