From e5a73815f580d296572e11b71b5f3ed320bbe912 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Fri, 30 May 2014 12:42:48 +0100 Subject: Add helper to highlight and excerpt by regex Backport of https://github.com/rails/rails/pull/11793/ Contains integration tests to check that it works as expected with ActsAsXapian. --- .../integration/xapian_search_highlighting_spec.rb | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 spec/integration/xapian_search_highlighting_spec.rb (limited to 'spec/integration') diff --git a/spec/integration/xapian_search_highlighting_spec.rb b/spec/integration/xapian_search_highlighting_spec.rb new file mode 100644 index 000000000..7bd64c995 --- /dev/null +++ b/spec/integration/xapian_search_highlighting_spec.rb @@ -0,0 +1,29 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe 'highlighting search results' do + include HighlightHelper + + it 'ignores stopwords' do + phrase = 'department of humpadinking' + search = ActsAsXapian::Search.new([PublicBody], phrase, :limit => 1) + matches = search.words_to_highlight(:regex => true) + highlight_matches(phrase, matches).should == 'department of humpadinking' + end + + it 'ignores case' do + search_phrase = 'department of humpadinking' + search = ActsAsXapian::Search.new([PublicBody], search_phrase, :limit => 1) + matches = search.words_to_highlight(:regex => true) + highlight_matches('Department of Humpadinking', matches).should == 'Department of Humpadinking' + end + + it 'highlights stemmed words' do + phrase = 'department' + search = ActsAsXapian::Search.new([PublicBody], phrase, :limit => 1) + matches = search.words_to_highlight(:regex => true) + + search.words_to_highlight(:regex => false).should == ['depart'] + highlight_matches(phrase, matches).should == 'department' + end + +end -- cgit v1.2.3 From f23b89f3474847cdd14ba892c5a7259964e18148 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Tue, 24 Jun 2014 16:08:32 +0100 Subject: Handle unhelpful stemming Stemming returns 'bore' as the word to highlight which can't be matched in the original phrase. Also removes duplicates from the results --- spec/integration/xapian_search_highlighting_spec.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'spec/integration') diff --git a/spec/integration/xapian_search_highlighting_spec.rb b/spec/integration/xapian_search_highlighting_spec.rb index 7bd64c995..65a34cf91 100644 --- a/spec/integration/xapian_search_highlighting_spec.rb +++ b/spec/integration/xapian_search_highlighting_spec.rb @@ -26,4 +26,14 @@ describe 'highlighting search results' do highlight_matches(phrase, matches).should == 'department' end + it 'highlights stemmed words even if the stem is unhelpful' do + # Stemming returns 'bore' as the word to highlight which can't be + # matched in the original phrase. + phrase = 'boring' + search = ActsAsXapian::Search.new([PublicBody], phrase, :limit => 1) + matches = search.words_to_highlight(:regex => true, :include_original => true) + + highlight_matches(phrase, matches).should == 'boring' + end + end -- cgit v1.2.3