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

(-)a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm (-6 / +20 lines)
Lines 788-794 sub _sort_field { Link Here
788
    my $query = $self->_truncate_terms($query);
788
    my $query = $self->_truncate_terms($query);
789
789
790
Given a string query this function appends '*' wildcard  to all terms except
790
Given a string query this function appends '*' wildcard  to all terms except
791
operands.
791
operands and double quoted strings.
792
792
793
=cut
793
=cut
794
794
Lines 796-806 sub _truncate_terms { Link Here
796
    my ( $self, $query ) = @_;
796
    my ( $self, $query ) = @_;
797
    my @stops = qw/and or not/;
797
    my @stops = qw/and or not/;
798
    my @new_terms;
798
    my @new_terms;
799
    my @split_query = split /[\(\s\)]/, $query;
799
    my @quote_split = split /(["])([^"]+)\1/, $query; 
800
    foreach my $term (@split_query) {
800
    #Above splits the string based on matching pairs of double quotes
801
        next if ( $term eq '' || $term eq ' ' );
801
    #In practice we get ('','"','donald duck',' ','"','the mouse',' and pete') 
802
        $term .= "*" unless ( ( grep { lc($term) =~ /^$_$/ } @stops ) || ( $term =~ /\*$/ ) );
802
    #given the string '"donald duck" "the mouse" and pete'
803
        push @new_terms, $term;
803
    #so we ignore empties, quote the ones after a '"' and split the rest on spaces
804
    for (my $i=0; $i < @quote_split; $i++ ) {
805
        next if ( $quote_split[$i] eq '' || $quote_split[$i] eq ' ' );
806
        if ( $quote_split[$i] eq '"' ){
807
            $i++;
808
            $quote_split[$i] = '"'.$quote_split[$i].'"';
809
            push @new_terms, $quote_split[$i]
810
        } else {
811
            my @space_split = split /[\(\s\)]/, $quote_split[$i];
812
            foreach my $term (@space_split) {
813
                next if ( $term eq '' || $term eq ' ' );
814
                $term .= "*" unless ( ( grep { lc($term) =~ /^$_$/ } @stops ) || ( $term =~ /\*$/ ) );
815
                push @new_terms, $term;
816
            }
817
        }
804
    }
818
    }
805
    $query = join ' ', @new_terms;
819
    $query = join ' ', @new_terms;
806
    return $query;
820
    return $query;
(-)a/t/db_dependent/Koha_SearchEngine_Elasticsearch_Search.t (-2 / +37 lines)
Lines 81-87 subtest 'json2marc' => sub { Link Here
81
};
81
};
82
82
83
subtest 'build_query tests' => sub {
83
subtest 'build_query tests' => sub {
84
    plan tests => 10;
84
    plan tests => 15;
85
85
86
    t::lib::Mocks::mock_preference('DisplayLibraryFacets','both');
86
    t::lib::Mocks::mock_preference('DisplayLibraryFacets','both');
87
    my $query = $builder->build_query();
87
    my $query = $builder->build_query();
Lines 135-139 subtest 'build_query tests' => sub { Link Here
135
        "(donald* duck*)",
135
        "(donald* duck*)",
136
        "query with '*' is unaltered when QueryAutoTruncate is enabled"
136
        "query with '*' is unaltered when QueryAutoTruncate is enabled"
137
    );
137
    );
138
139
    ( undef, $query ) = $builder->build_query_compat( undef, ['donald duck and the mouse'] );
140
    is(
141
        $query->{query}{query_string}{query},
142
        "(donald* duck* and the* mouse*)",
143
        "individual words are all truncated and stopwords ignored"
144
    );
145
146
    ( undef, $query ) = $builder->build_query_compat( undef, ['*'] );
147
    is(
148
        $query->{query}{query_string}{query},
149
        "(*)",
150
        "query of just '*' is unaltered when QueryAutoTruncate is enabled"
151
    );
152
153
    ( undef, $query ) = $builder->build_query_compat( undef, ['"donald duck"'] );
154
    is(
155
        $query->{query}{query_string}{query},
156
        '("donald duck")',
157
        "query with quotes is unaltered when QueryAutoTruncate is enabled"
158
    );
159
160
161
    ( undef, $query ) = $builder->build_query_compat( undef, ['"donald duck" and "the mouse"'] );
162
    is(
163
        $query->{query}{query_string}{query},
164
        '("donald duck" and "the mouse")',
165
        "all quoted strings are unaltered if more than one in query"
166
    );
167
168
    ( undef, $query ) = $builder->build_query_compat( undef, ['barcode:123456'] );
169
    is(
170
        $query->{query}{query_string}{query},
171
        '(barcode:123456*)',
172
        "query of specific field is truncated"
173
    );
138
};
174
};
139
175
140
- 

Return to bug 18374