Method: Wgit::DSL#search

Defined in:
lib/wgit/dsl.rb

#search(query, stream: $stdout, top_result_only: true, include_score: false, case_sensitive: false, whole_sentence: true, limit: 10, skip: 0, sentence_limit: 80) {|doc| ... } ⇒ Array<Wgit::Document>

Performs a search of the database's indexed documents and pretty prints the results in a search engine-esque format. See Wgit::Database::DatabaseAdapter#search and Wgit::Document#search! for details of how the search methods work.

Parameters:

  • query (String)

    The text query to search with.

  • stream (nil, #puts) (defaults to: $stdout)

    Any object that respond_to?(:puts). It is used to output text somewhere e.g. a file or STDERR. Use nil for no output.

  • case_sensitive (Boolean) (defaults to: false)

    Whether character case must match.

  • whole_sentence (Boolean) (defaults to: true)

    Whether multiple words should be searched for separately.

  • limit (Integer) (defaults to: 10)

    The max number of results to print.

  • skip (Integer) (defaults to: 0)

    The number of DB records to skip.

  • sentence_limit (Integer) (defaults to: 80)

    The max length of each result's text snippet.

Yields:

  • (doc)

    Given each search result (Wgit::Document) returned from the database containing only its matching #text.

Returns:



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/wgit/dsl.rb', line 268

def search(
  query, stream: $stdout,
  top_result_only: true, include_score: false,
  case_sensitive: false, whole_sentence: true,
  limit: 10, skip: 0, sentence_limit: 80
)
  stream ||= File.open(File::NULL, 'w')

  results = get_db.search(
    query, case_sensitive:, whole_sentence:, limit:, skip:)

  results.each do |doc|
    doc.search_text!(
      query, case_sensitive:, whole_sentence:, sentence_limit:)
    yield(doc) if block_given?
  end

  if top_result_only
    Wgit::Utils.pprint_top_search_results(results, include_score:, stream:)
  else
    Wgit::Utils.pprint_all_search_results(results, include_score:, stream:)
  end

  results
end