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);
54 my $results = $browser->browse($prefix, $field, \%options);
56 Does a prefix search for C<$prefix>, looking in C<$field>. Options are:
62 The number of results to return. For Koha browse purposes, this should
63 probably be fairly high. Defaults to 500.
67 How much allowing for typos and misspellings is done. If 0, then it must match
68 exactly. If unspecified, it defaults to '1', which is probably the most useful.
69 Otherwise, it is a number specifying the Levenshtein edit distance relative to
70 the string length, according to the following lengths:
80 C<fuzziness> edits allowed
84 C<fuzziness>+1 edits allowed
88 In all cases the maximum number of edits allowed is two (an elasticsearch
95 This returns an arrayref of hashrefs. Each hashref contains a "text" element
96 that contains the field as returned. There may be other fields in that
97 hashref too, but they're less likely to be important.
99 The array will be ordered as returned from Elasticsearch, which seems to be
100 in order of some form of relevance.
105 my ($self, $prefix, $field, $options) = @_;
107 my $query = $self->_build_query($prefix, $field, $options);
108 my $elasticsearch = $self->get_elasticsearch();
109 my $conf = $self->get_elasticsearch_params();
110 my $results = $elasticsearch->search(
111 index => $conf->{index_name},
115 return $results->{suggest}{suggestions}[0]{options};
120 my $query = $self->_build_query($prefix, $field, $options);
122 Arguments are the same as for L<browse>. This will return a query structure
123 for elasticsearch to use.
128 my ( $self, $prefix, $field, $options ) = @_;
130 $options = {} unless $options;
131 my $f = $options->{fuzziness} // 1;
132 my $l = length($prefix);
143 $fuzzie = 2 if $fuzzie > 2;
145 my $size = $options->{count} // 500;
147 # this is an annoying thing, if we set size to 0 it gets rewritten
148 # to 10. There's a bug somewhere in one of the libraries.
154 field => $field . '__suggestion',
157 fuzziness => $fuzzie,
174 =item Robin Sheat << <robin@catalyst.net.nz> >>