View | Details | Raw Unified | Return to bug 18345
Collapse All | Expand All

(-)a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm (-1 / +90 lines)
Lines 48-53 use URI::Escape; Link Here
48
48
49
use C4::Context;
49
use C4::Context;
50
50
51
 =head2 build_term_query
52
53
    my $query = $builder->build_query($search, %options);
54
55
C<$search> is an arrayref where each element is a hashref containing
56
a part of the search.
57
58
The hashref must contain a 'term', which is the string, and may contain other
59
things to adjust how the search is performed:
60
61
=over 4
62
63
=item field
64
65
the field to search on, as defined in the C<search_field> table. If
66
unspecified, all fields will be searched over.
67
68
=item exact
69
70
do an exact, literal, phrase match rather than a tokenised search. This will
71
also ignore things like truncation and fuzziness.
72
73
=back
74
75
The C<%options> may be:
76
77
=over 4
78
79
=item and_or
80
81
either "and" or "or", and determines how multiple terms are combined. Default
82
is "and."
83
84
=back
85
86
=head3 Returns
87
88
a query structure intended to be passed to elasticsearch.
89
90
=head3 Note
91
92
This is pretty limited, it's intended that more options be added as necessary
93
to support new functionality.
94
95
=head3 Note 2
96
97
This ultimately ought to be replaced with a query parser driver, but that code
98
is not documented so it'll take a while to figure out.
99
100
=cut
101
102
sub build_term_query {
103
    my ( $self, $search, %options ) = @_;
104
105
    my @match_parts;
106
107
    foreach my $s (@$search) {
108
        my ( $m_type, $f_suffix, $query_name );
109
110
        # This accounts for different naming of things based on our options
111
        if ( $s->{exact} ) {
112
            $m_type     = 'term';
113
            $f_suffix   = '.raw';
114
            $query_name = 'value';
115
        }
116
        else {
117
            $m_type     = 'match';
118
            $f_suffix   = '';
119
            $query_name = 'query';
120
        }
121
        my $field = $s->{field} // "_all";
122
        my $m = {
123
            $m_type => {
124
                "$s->{field}$f_suffix" => {
125
                    $query_name => $s->{term},
126
                },
127
            },
128
        };
129
        push @match_parts, $m;
130
    }
131
    my $query;
132
    if ( $options{and_or} && lc( $options{and_or} ) eq 'or' ) {
133
        $query->{bool}{should} = \@match_parts;
134
    }
135
    else {
136
        $query->{bool}{must} = \@match_parts;
137
    }
138
    return { query => $query };
139
}
140
51
=head2 build_query
141
=head2 build_query
52
142
53
    my $simple_query = $builder->build_query("hello", %options)
143
    my $simple_query = $builder->build_query("hello", %options)
54
- 

Return to bug 18345