Created
September 20, 2016 13:40
-
-
Save wearethefoos/a4b4b557ada12cbc2d53318cc05ebc38 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'net/http' | |
require 'json' | |
class Search | |
TOPICS = ["Day 1 - Frontend", "Day 2 - Basic Programming", "Day 3 - Loops & Breaks", "Day 4 - OOP", "Day 5 - Intro to Web with Sinatra & Git", "Day 6 - Intro to Ruby on Rails", "Day 7 - Hackathon I", "Day 08 - Rails, Deployment, Pull requests, Code review", "Day 09 - ActiveRecord", "Day 10 - Rails Testing with RSpec", "Day 11 - Users with Devise & Authorization with CanCan(Can)", "Day 12 - File uploads with Carrierwave", "Day 13 - Javascript 1", "Day 14 - SQL and Relations", "Day 15 - Javascript 2 & JQuery", "Day 16 - AJAX & REST", "Day 17 - Integration testing", "Day 18", "Day 19", "Day 20", "Day 21", "Day 22", "Day 23 - Javascript 3", "Day 24 - React 1", "Day 25 - React 2", "Day 26 - React 3", "Day 27 - Testing with Enzyme", "Day 28. FeathersJS", "Advanced Session :: Introduction to d3", "Advanced Session :: Introduction to Docker", "Advanced Session :: Redux", "Advanced Session :: Styling with Sass", "Codaisseur Cloud", "Git & Github", "Regular expressions", "Shortcuts You Should Know", "Terminal"] | |
URL = 'http://localhost:9200/reader/articles/_search' | |
def initialize(tag) | |
@tag = tag | |
end | |
def search! | |
http = Net::HTTP.new('localhost', 9200) | |
response = http.request_post "/reader/articles/_search", query.to_json | |
JSON.parse(response.body) | |
end | |
def query | |
{ | |
aggs: { | |
articles: { | |
filters: { | |
filters: topic_filters | |
}, | |
aggs: { | |
@tag => { | |
filter: { | |
match: { | |
tags: { | |
query: @tag, | |
operator: :and | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
end | |
def topic_filters | |
filters = {} | |
TOPICS.each do |topic| | |
filters[topic] = { | |
match: { | |
topics: { | |
query: topic, | |
operator: :and | |
} | |
} | |
} | |
end | |
return filters | |
end | |
end | |
tag = ARGV.join(" ") | |
search = Search.new(tag) | |
results = search.search! | |
puts | |
puts "----------------------------------------" | |
puts "Days with articles tagged '#{tag}'" | |
puts "----------------------------------------" | |
puts | |
results["aggregations"]["articles"]["buckets"].each do |topic, result| | |
next if result[tag]["doc_count"] == 0 | |
puts "#{topic}: #{result[tag]["doc_count"]} / #{result["doc_count"]}" | |
end | |
puts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment