map { s/^mc-//r } grep { defined($_) && $_ ne '' && $_ =~ /^mc-/ } @parts;
return () unless @norm_parts + @mc_parts;
return ( @norm_parts, @mc_parts )[0] if @norm_parts + @mc_parts == 1;
- my $grouped_mc =
- @mc_parts ? '(' . ( join ' OR ', map { "($_)" } @mc_parts ) . ')' : ();
-
- # Handy trick: $x || () inside a join means that if $x ends up as an
- # empty string, it gets replaced with (), which makes join ignore it.
- # (bad effect: this'll also happen to '0', this hopefully doesn't matter
- # in this case.)
- join( ' AND ',
- join( ' AND ', map { "($_)" } @norm_parts ) || (),
- $grouped_mc || () );
+
+ # Group limits by field, so they can be OR'ed together
+ my %mc_limits;
+ foreach my $mc_part (@mc_parts) {
+ my ($field, $value) = split /:/, $mc_part, 2;
+ $mc_limits{$field} //= [];
+ push @{ $mc_limits{$field} }, $value;
+ }
+
+ @mc_parts = map {
+ sprintf('%s:(%s)', $_, join (' OR ', @{ $mc_limits{$_} }));
+ } sort keys %mc_limits;
+
+ @norm_parts = map { "($_)" } @norm_parts;
+
+ return join( ' AND ', @norm_parts, @mc_parts);
}
=head2 _make_phrases
use Modern::Perl;
-use Test::More tests => 5;
+use Test::More tests => 6;
use t::lib::Mocks;
use_ok('Koha::SearchEngine::Elasticsearch::QueryBuilder');
is($res, 'test another part', 'unbalanced curly brackets replaced correctly');
};
+subtest '_join_queries' => sub {
+ plan tests => 6;
+
+ my $params = {
+ index => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX,
+ };
+ my $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new($params);
+
+ my $query;
+
+ $query = $qb->_join_queries('foo');
+ is($query, 'foo', 'should work with a single param');
+
+ $query = $qb->_join_queries(undef, '', 'foo', '', undef);
+ is($query, 'foo', 'should ignore undef or empty queries');
+
+ $query = $qb->_join_queries('foo', 'bar');
+ is($query, '(foo) AND (bar)', 'should join queries with an AND');
+
+ $query = $qb->_join_queries('homebranch:foo', 'onloan:false');
+ is($query, '(homebranch:foo) AND (onloan:false)', 'should also work when field is specified');
+
+ $query = $qb->_join_queries('homebranch:foo', 'mc-itype:BOOK', 'mc-itype:EBOOK');
+ is($query, '(homebranch:foo) AND itype:(BOOK OR EBOOK)', 'should join with OR when using an "mc-" field');
+
+ $query = $qb->_join_queries('homebranch:foo', 'mc-itype:BOOK', 'mc-itype:EBOOK', 'mc-location:SHELF');
+ is($query, '(homebranch:foo) AND itype:(BOOK OR EBOOK) AND location:(SHELF)', 'should join "mc-" parts with AND if not the same field');
+};
+
1;