From 8b6451c49028967e81835fec73d860ee4901b42b Mon Sep 17 00:00:00 2001 From: Robin Sheat Date: Thu, 12 Mar 2015 18:11:30 +1300 Subject: [PATCH] Bug 12478: phrase searching for authorities is happier Signed-off-by: Nick Clemens Signed-off-by: Jesse Weaver Signed-off-by: Tomas Cohen Arazi Signed-off-by: Kyle M Hall Signed-off-by: Brendan Gallagher --- Koha/ElasticSearch.pm | 14 ++++++++++---- Koha/SearchEngine/Elasticsearch/QueryBuilder.pm | 11 ++++++++--- Koha/SearchEngine/Elasticsearch/Search.pm | 2 +- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/Koha/ElasticSearch.pm b/Koha/ElasticSearch.pm index dfc9556349..56afaeffc6 100644 --- a/Koha/ElasticSearch.pm +++ b/Koha/ElasticSearch.pm @@ -133,13 +133,13 @@ sub get_elasticsearch_settings { analyzer => { analyser_phrase => { tokenizer => 'keyword', - filter => 'lowercase', + filter => ['lowercase'], }, analyser_standard => { tokenizer => 'standard', - filter => 'lowercase', + filter => ['lowercase'], } - } + }, } } }; @@ -166,6 +166,11 @@ sub get_elasticsearch_mappings { include_in_all => "false", type => "string", }, + '_all.phrase' => { + search_analyzer => "analyser_phrase", + index_analyzer => "analyser_phrase", + type => "string", + }, } } }; @@ -190,7 +195,8 @@ sub get_elasticsearch_mappings { phrase => { search_analyzer => "analyser_phrase", index_analyzer => "analyser_phrase", - type => "string" + type => "string", + copy_to => "_all.phrase", }, }, }; diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm index 3fc238c36e..7e3522455a 100644 --- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm +++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm @@ -269,7 +269,10 @@ sub build_authorities_query { if ($op eq 'is' || $op eq '=') { # look for something that matches completely # note, '=' is about numerical vals. May need special handling. - push @filter_parts, { term => { $wh => $val }}; + # _allphrase is a special field that only groups the exact + # matches. Also, we lowercase our search because the ES + # index lowercases its values. + push @filter_parts, { term => { "$wh.phrase" => lc $val }}; } elsif ($op eq 'exact') { # left and right truncation, otherwise an exact phrase push @query_parts, { match_phrase => { $wh => $val }}; @@ -283,6 +286,8 @@ sub build_authorities_query { # 'should' behaves like 'or', if we want 'and', use 'must' my $query_part = { bool => { should => \@query_parts } }; my $filter_part = { bool => { should => \@filter_parts }}; + # extract the sort stuff + my %sort = ( sort => [ $search->{sort} ] ) if exists $search->{sort}; my $query; if (@filter_parts) { $query = { query => { filtered => { filter => $filter_part, query => $query_part }}}; @@ -375,7 +380,7 @@ sub build_authorities_query_compat { for ( my $i = 0 ; $i < @$value ; $i++ ) { push @searches, { - where => $marclist->[$i], + where => $koha_to_index_name{$marclist->[$i]}, operator => $operator->[$i], value => $value->[$i], }; @@ -388,7 +393,7 @@ sub build_authorities_query_compat { : undef; if ($sort_field) { my $sort_order = ( $orderby =~ /Asc$/ ) ? 'asc' : 'desc'; - %sort = ( $orderby => $sort_order, ); + %sort = ( $sort_field => $sort_order, ); } my %search = ( searches => \@searches, diff --git a/Koha/SearchEngine/Elasticsearch/Search.pm b/Koha/SearchEngine/Elasticsearch/Search.pm index aa8640f769..a9adcba3b6 100644 --- a/Koha/SearchEngine/Elasticsearch/Search.pm +++ b/Koha/SearchEngine/Elasticsearch/Search.pm @@ -106,7 +106,7 @@ sub count { my $params = $self->get_elasticsearch_params(); $self->store( - Catmandu::Store::ElasticSearch->new( %$params, trace_calls => 1, ) ) + Catmandu::Store::ElasticSearch->new( %$params, trace_calls => 0, ) ) unless $self->store; my $searcher = $self->store->bag->searcher(query => $query); -- 2.39.2