Skip to content

Commit

Permalink
Document#search now totals the text score for matching results
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeltelford committed Oct 17, 2024
1 parent 803fb91 commit bef51c1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/wgit/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -471,15 +471,18 @@ def search(
assert_type(text, String)

text = text.strip
next if results[text]

matches = text.scan(regex).count
next unless matches.positive?

index = text.index(regex) # Index of first match.
Wgit::Utils.format_sentence_length(text, index, sentence_limit)

results[text] = matches * weight
# For duplicate matching text, total the text score.
text_score = matches * weight
existing_score = results[text]
text_score += existing_score if existing_score

results[text] = text_score
end
end

Expand Down
21 changes: 21 additions & 0 deletions test/test_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,27 @@ def test_search__set_search_fields
], results)
end

def test_search__total_dup_results
# Two matching fields that when formatted will be 80 chars long.
# The text result will be a single string with a text score of 2 (because of the dup).
doc = Wgit::Document.new "http://www.mytestsite.com/home", <<~HTML
<p>Note: The text search index lists all document fields to be searched by MongoDB when calling Wgit::Database#search. Therefore, you should append this list with any other fields that you want searched. For example, if you extend the API then you might want to search your new fields in the database by adding them to the index above. This can be done programmatically with:</p>
<hr>
<p>Note: The text search index lists all document fields to be searched by MongoDB when calling Wgit::Database#search. Therefore, you should append this list with any other fields that you want searched. For example, if you extend the API then you might want to search your new fields in the database by adding them to the index above. This can be done programmatically with:</p>
HTML

results = doc.search("Wgit::Database") do |results_hash|
assert_equal(
{ " to be searched by MongoDB when calling Wgit::Database#search. Therefore, you sh" => 2 },
results_hash
)
end
assert_equal(
[" to be searched by MongoDB when calling Wgit::Database#search. Therefore, you sh"],
results
)
end

def test_search_text
doc = Wgit::Document.new "http://www.mytestsite.com/home", @html
orig_text = doc.text
Expand Down

0 comments on commit bef51c1

Please sign in to comment.