Last active
August 29, 2015 14:11
-
-
Save barbasa/5602b97bd80db2d151f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get '/search' => sub { | |
my $keywords = params->{keywords}; | |
# … OMISSIS …. | |
my $results = _get_results_from_elasticsearch(); | |
unless(scalar @$results){ | |
my $e = Search::Elasticsearch->new( nodes => 'localhost:9200' ); | |
my $all_results = $e->search( | |
index => 'catalogue', | |
type => '.percolator', | |
); | |
# When creating a new document in the percolator index the ID is mandatory | |
my $maxidx = $all_results->{hits}>{total} + 1; | |
# Store the query that didn't return any match! | |
# The percolator interface of the perl module doesn’t expose writes, | |
# so we need to use the normal interface | |
$e->create( | |
index => 'catalogue', | |
type => '.percolator', | |
id => $maxidx, | |
body => { | |
query => { | |
match => { | |
field_to_match => $keywords, | |
}, | |
}, | |
# Any data can be added here! | |
#The $user_id in this case can be used to email the user later on when the product get into the catalogues | |
user_id => $user_id, | |
}, | |
); | |
redirect 'no_results.html'; | |
} else { | |
# … display the results ... | |
} | |
# … OMISSIS ... | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment