Class: Jekyll::Commands::Search

Inherits:
Command
  • Object
show all
Defined in:
lib/jekyll_search.rb

Class Method Summary collapse

Class Method Details

.init_with_program(prog) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/jekyll_search.rb', line 84

def init_with_program(prog)
  prog.command(:search) do |c|
    c.syntax "search query"
    c.description 'Searches a search index in Elasticsearch.'

    c.action do |args, options|
      query = args.join(' ').strip
      options["serving"] = false
      Search.process(options, query)
    end
  end
end

.process(options, query) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/jekyll_search.rb', line 97

def process(options, query)
  options = configuration_from_options(options)
  site = Jekyll::Site.new(options)
  settings = site.config['search']

  client = Elasticsearch::Client.new host: settings['host'], log: false
  result = client.search index: settings['index']['name'], body: { query: { match: { content: query } }, highlight: { fields: { content: {} }} }

  puts "Query: #{query}"
  puts "Total: #{result['hits']['total']}"
  puts "Max score: #{result['hits']['max_score']}"
  for hit in result['hits']['hits']
    puts "Hit at #{hit['_source']['url']} (#{hit['_score']})"
    hit['highlight']['content'].each { |c| puts '- ' + c }
  end
end