Method: MiniSearch::InvertedIndex#search

Defined in:
lib/mini_search/inverted_index.rb

#search(raw_terms, operator: 'or') ⇒ Object



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)

  # gets the documents that matches each term
  results_by_terms = processed_terms.map do |term|
    @inverted_index[term] || []
  end

  return [] unless results_by_terms.any?

  idfs = generate_idfs(processed_terms)

  # We flat and group by document id
  any_term_matched_documents = results_by_terms.flatten(1).group_by do |document, _tf|
    document.fetch(:id)
  end

  # We select documents based on operator
  # if operator AND
  #   we select only documents that matched all terms
  # else
  #   we select everthing
  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

  # map to a { document:, score: } structure.
  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

  # sort by scores and wraps in a more convenient structure.
  documents = document_and_scores
    .sort_by { |item| -item[:score] }

  { documents: documents, idfs: idfs, processed_terms: processed_terms }
end