1 package Koha::SearchEngine::Elasticsearch::Browse;
3 # Copyright 2015 Catalyst IT
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 Koha::SearchEngine::ElasticSearch::Browse - browse functions for Elasticsearch
27 Koha::SearchEngine::Elasticsearch::Browse->new( { index => 'biblios' } );
28 my $results = $browser->browse(
35 foreach my $r (@$results) {
36 push @hits, $r->{text};
41 This provides an easy interface to the "browse" functionality. Essentially,
42 it does a fast prefix search on defined fields. The fields have to be marked
43 as "suggestible" in the database when indexing takes place.
49 use base qw(Koha::SearchEngine::Elasticsearch);
52 use Catmandu::Store::ElasticSearch;
54 Koha::SearchEngine::Elasticsearch::Browse->mk_accessors(qw( store ));
58 my $results = $browser->browse($prefix, $field, \%options);
60 Does a prefix search for C<$prefix>, looking in C<$field>. Options are:
66 The number of results to return. For Koha browse purposes, this should
67 probably be fairly high. Defaults to 500.
71 How much allowing for typos and misspellings is done. If 0, then it must match
72 exactly. If unspecified, it defaults to '1', which is probably the most useful.
73 Otherwise, it is a number specifying the Levenshtein edit distance relative to
74 the string length, according to the following lengths:
84 C<fuzziness> edits allowed
88 C<fuzziness>+1 edits allowed
92 In all cases the maximum number of edits allowed is two (an elasticsearch
99 This returns an arrayref of hashrefs. Each hashref contains a "text" element
100 that contains the field as returned. There may be other fields in that
101 hashref too, but they're less likely to be important.
103 The array will be ordered as returned from Elasticsearch, which seems to be
104 in order of some form of relevance.
109 my ($self, $prefix, $field, $options) = @_;
111 my $params = $self->get_elasticsearch_params();
113 Catmandu::Store::ElasticSearch->new(
116 ) unless $self->store;
118 my $query = $self->_build_query($prefix, $field, $options);
119 my $results = $self->store->bag->search(%$query);
120 return $results->{suggest}{suggestions}[0]{options};
125 my $query = $self->_build_query($prefix, $field, $options);
127 Arguments are the same as for L<browse>. This will return a query structure
128 for elasticsearch to use.
133 my ( $self, $prefix, $field, $options ) = @_;
135 $options = {} unless $options;
136 my $f = $options->{fuzziness} // 1;
137 my $l = length($prefix);
148 $fuzzie = 2 if $fuzzie > 2;
150 my $size = $options->{count} // 500;
152 # this is an annoying thing, if we set size to 0 it gets rewritten
153 # to 10. There's a bug somewhere in one of the libraries.
159 field => $field . '__suggestion',
162 fuzziness => $fuzzie,
179 =item Robin Sheat << <robin@catalyst.net.nz> >>