69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/mini_search/inverted_index.rb', line 69
def search(raw_terms, operator: 'or')
processed_terms = @querying_pipeline.execute(raw_terms)
results_by_terms = processed_terms.map do |term|
@inverted_index[term] || []
end
return [] unless results_by_terms.any?
idfs = generate_idfs(processed_terms)
any_term_matched_documents = results_by_terms.flatten(1).group_by do |document, _tf|
document.fetch(:id)
end
operator_specific_matched_documents = any_term_matched_documents.select do |_document_id, document_and_tfs|
match_terms_according_operator?(document_and_tfs,
processed_terms,
operator)
end
document_and_scores = operator_specific_matched_documents.map do |document_id, document_and_tfs|
{
document: @documents.dig(document_id, :document),
score: calculate_score(@documents.fetch(document_id), document_and_tfs, idfs)
}
end
documents = document_and_scores
.sort_by { |item| -item[:score] }
{ documents: documents, idfs: idfs, processed_terms: processed_terms }
end
|