Skip to content

Instantly share code, notes, and snippets.

@barbasa
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barbasa/5602b97bd80db2d151f1 to your computer and use it in GitHub Desktop.
Save barbasa/5602b97bd80db2d151f1 to your computer and use it in GitHub Desktop.
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