{"action":"created","comment":{"url":"https://api.github.com/repos/18F/e-manifest/pulls/comments/48907478","id":48907478,"diff_hunk":"@@ -0,0 +1,147 @@\n+require 'elasticsearch/dsl'\n+require_relative 'fielded_search'\n+\n+class QueryDSL\n+  include Elasticsearch::DSL\n+\n+  MAX_RESULTS = 100\n+\n+  attr_reader :params, :current_user, :query_str\n+\n+  def initialize(args)\n+    @current_user = args[:current_user]\n+    @params = args[:params]\n+    @query_str = @params[:q]\n+    build_dsl\n+  end\n+\n+  def to_hash\n+    @dsl.to_hash\n+  end\n+\n+  def default_operator\n+    params[:operator] || \"and\"\n+  end\n+\n+  def apply_authz?\n+    current_user != nil\n+  end\n+\n+  def composite_query_string\n+    stringify_clauses [query_str, client_query].select(&:present?)\n+  end\n+\n+  def humanized_query_string\n+    stringify_clauses [query_str, client_query_humanized].select(&:present?)\n+  end\n+\n+  def client_query\n+    fielded = params[:aq]\n+    munge_fielded_params(fielded) if fielded\n+    FieldedSearch.new(fielded)\n+  end\n+\n+  private\n+\n+  def stringify_clauses(clauses)\n+    if clauses.length == 2\n+      clauses.map { |c| \"(#{c})\" }.join(\" AND \")\n+    elsif clauses.length == 1\n+      clauses[0].to_s\n+    else\n+      \"\"\n+    end\n+  end\n+\n+  def client_query_humanized\n+    client_query.humanized(current_user.client_model)\n+  end\n+\n+  def munge_fielded_params(fielded)\n+    if fielded[:created_at].present? && fielded[:created_within].present?\n+      munge_created_at_field(fielded)\n+    end\n+    # do not calculate more than once, or when created_at is null\n+    fielded.delete(:created_within)\n+  end\n+\n+  def munge_created_at_field(fielded)\n+    high_end_range = Time.zone.parse(fielded[:created_at])\n+    within_parsed = fielded[:created_within].match(/^(\\d+) (\\w+)/)\n+    return unless high_end_range && within_parsed\n+    low_end_range = high_end_range.utc - within_parsed[1].to_i.send(within_parsed[2])\n+    fielded[:created_at] = \"[#{low_end_range} TO #{high_end_range.utc}]\"\n+  end\n+\n+  def build_dsl\n+    @dsl = Elasticsearch::DSL::Search::Search.new\n+    add_query\n+    add_filter\n+    add_sort\n+    add_pagination\n+  end\n+\n+  def add_query\n+    searchdsl = self\n+    @dsl.query = Query.new\n+    @dsl.query do\n+      query_string do\n+        query searchdsl.composite_query_string\n+        default_operator searchdsl.default_operator\n+      end\n+    end\n+  end\n+\n+  def add_filter\n+    searchdsl = self\n+    if apply_authz?\n+      @dsl.filter = Filter.new\n+      @dsl.filter do\n+        bool do\n+          must do\n+            term \"subscribers.id\" => searchdsl.current_user.id.to_s","path":"lib/query_dsl.rb","position":102,"original_position":102,"commit_id":"25fab3affb9260d958bb6d573c4af0742561b37e","original_commit_id":"7f1c67dbe63ebf36b2c60d910f47cae665e552c6","user":{"login":"jessieay","id":601515,"avatar_url":"https://avatars.githubusercontent.com/u/601515?v=3","gravatar_id":"","url":"https://api.github.com/users/jessieay","html_url":"https://github.com/jessieay","followers_url":"https://api.github.com/users/jessieay/followers","following_url":"https://api.github.com/users/jessieay/following{/other_user}","gists_url":"https://api.github.com/users/jessieay/gists{/gist_id}","starred_url":"https://api.github.com/users/jessieay/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jessieay/subscriptions","organizations_url":"https://api.github.com/users/jessieay/orgs","repos_url":"https://api.github.com/users/jessieay/repos","events_url":"https://api.github.com/users/jessieay/events{/privacy}","received_events_url":"https://api.github.com/users/jessieay/received_events","type":"User","site_admin":false},"body":"since `current_user` can be anything but `nil` to get to this point  (`current_user != nil`), isn't there a risk that we would get a `noMethodErrror` when calling `id` ? ","created_at":"2016-01-05T22:49:11Z","updated_at":"2016-01-05T22:49:11Z","html_url":"https://github.com/18F/e-manifest/pull/14#discussion_r48907478","pull_request_url":"https://api.github.com/repos/18F/e-manifest/pulls/14","_links":{"self":{"href":"https://api.github.com/repos/18F/e-manifest/pulls/comments/48907478"},"html":{"href":"https://github.com/18F/e-manifest/pull/14#discussion_r48907478"},"pull_request":{"href":"https://api.github.com/repos/18F/e-manifest/pulls/14"}}},"pull_request":{"url":"https://api.github.com/repos/18F/e-manifest/pulls/14","id":55141244,"html_url":"https://github.com/18F/e-manifest/pull/14","diff_url":"https://github.com/18F/e-manifest/pull/14.diff","patch_url":"https://github.com/18F/e-manifest/pull/14.patch","issue_url":"https://api.github.com/repos/18F/e-manifest/issues/14","number":14,"state":"open","locked":false,"title":"Elasticsearch","user":{"login":"pkarman","id":1205061,"avatar_url":"https://avatars.githubusercontent.com/u/1205061?v=3","gravatar_id":"","url":"https://api.github.com/users/pkarman","html_url":"https://github.com/pkarman","followers_url":"https://api.github.com/users/pkarman/followers","following_url":"https://api.github.com/users/pkarman/following{/other_user}","gists_url":"https://api.github.com/users/pkarman/gists{/gist_id}","starred_url":"https://api.github.com/users/pkarman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pkarman/subscriptions","organizations_url":"https://api.github.com/users/pkarman/orgs","repos_url":"https://api.github.com/users/pkarman/repos","events_url":"https://api.github.com/users/pkarman/events{/privacy}","received_events_url":"https://api.github.com/users/pkarman/received_events","type":"User","site_admin":false},"body":"This PR adds several features:\r\n\r\n* search with Elasticsearch\r\n* background worker with Sidekiq\r\n* search API requires `q` (query) or `aq` (advanced query) params\r\n* rspec uses DatabaseCleaner\r\n* `rake serve` now uses foreman to run web and worker together\r\n* the `/sidekiq` web endpoint requires configurable basic authentication\r\n\r\nSee stories: \r\n\r\n* https://trello.com/c/xtxYU5Mp\r\n* https://trello.com/c/rlERjxud","created_at":"2016-01-05T20:51:08Z","updated_at":"2016-01-05T22:49:11Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3c7fa5b571642c145150fae6ff2aaac7ed831c66","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/18F/e-manifest/pulls/14/commits","review_comments_url":"https://api.github.com/repos/18F/e-manifest/pulls/14/comments","review_comment_url":"https://api.github.com/repos/18F/e-manifest/pulls/comments{/number}","comments_url":"https://api.github.com/repos/18F/e-manifest/issues/14/comments","statuses_url":"https://api.github.com/repos/18F/e-manifest/statuses/25fab3affb9260d958bb6d573c4af0742561b37e","head":{"label":"18F:elasticsearch","ref":"elasticsearch","sha":"25fab3affb9260d958bb6d573c4af0742561b37e","user":{"login":"18F","id":6233994,"avatar_url":"https://avatars.githubusercontent.com/u/6233994?v=3","gravatar_id":"","url":"https://api.github.com/users/18F","html_url":"https://github.com/18F","followers_url":"https://api.github.com/users/18F/followers","following_url":"https://api.github.com/users/18F/following{/other_user}","gists_url":"https://api.github.com/users/18F/gists{/gist_id}","starred_url":"https://api.github.com/users/18F/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/18F/subscriptions","organizations_url":"https://api.github.com/users/18F/orgs","repos_url":"https://api.github.com/users/18F/repos","events_url":"https://api.github.com/users/18F/events{/privacy}","received_events_url":"https://api.github.com/users/18F/received_events","type":"Organization","site_admin":false},"repo":{"id":38887537,"name":"e-manifest","full_name":"18F/e-manifest","owner":{"login":"18F","id":6233994,"avatar_url":"https://avatars.githubusercontent.com/u/6233994?v=3","gravatar_id":"","url":"https://api.github.com/users/18F","html_url":"https://github.com/18F","followers_url":"https://api.github.com/users/18F/followers","following_url":"https://api.github.com/users/18F/following{/other_user}","gists_url":"https://api.github.com/users/18F/gists{/gist_id}","starred_url":"https://api.github.com/users/18F/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/18F/subscriptions","organizations_url":"https://api.github.com/users/18F/orgs","repos_url":"https://api.github.com/users/18F/repos","events_url":"https://api.github.com/users/18F/events{/privacy}","received_events_url":"https://api.github.com/users/18F/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/18F/e-manifest","description":"The EPA e-Manifest project.","fork":false,"url":"https://api.github.com/repos/18F/e-manifest","forks_url":"https://api.github.com/repos/18F/e-manifest/forks","keys_url":"https://api.github.com/repos/18F/e-manifest/keys{/key_id}","collaborators_url":"https://api.github.com/repos/18F/e-manifest/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/18F/e-manifest/teams","hooks_url":"https://api.github.com/repos/18F/e-manifest/hooks","issue_events_url":"https://api.github.com/repos/18F/e-manifest/issues/events{/number}","events_url":"https://api.github.com/repos/18F/e-manifest/events","assignees_url":"https://api.github.com/repos/18F/e-manifest/assignees{/user}","branches_url":"https://api.github.com/repos/18F/e-manifest/branches{/branch}","tags_url":"https://api.github.com/repos/18F/e-manifest/tags","blobs_url":"https://api.github.com/repos/18F/e-manifest/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/18F/e-manifest/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/18F/e-manifest/git/refs{/sha}","trees_url":"https://api.github.com/repos/18F/e-manifest/git/trees{/sha}","statuses_url":"https://api.github.com/repos/18F/e-manifest/statuses/{sha}","languages_url":"https://api.github.com/repos/18F/e-manifest/languages","stargazers_url":"https://api.github.com/repos/18F/e-manifest/stargazers","contributors_url":"https://api.github.com/repos/18F/e-manifest/contributors","subscribers_url":"https://api.github.com/repos/18F/e-manifest/subscribers","subscription_url":"https://api.github.com/repos/18F/e-manifest/subscription","commits_url":"https://api.github.com/repos/18F/e-manifest/commits{/sha}","git_commits_url":"https://api.github.com/repos/18F/e-manifest/git/commits{/sha}","comments_url":"https://api.github.com/repos/18F/e-manifest/comments{/number}","issue_comment_url":"https://api.github.com/repos/18F/e-manifest/issues/comments{/number}","contents_url":"https://api.github.com/repos/18F/e-manifest/contents/{+path}","compare_url":"https://api.github.com/repos/18F/e-manifest/compare/{base}...{head}","merges_url":"https://api.github.com/repos/18F/e-manifest/merges","archive_url":"https://api.github.com/repos/18F/e-manifest/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/18F/e-manifest/downloads","issues_url":"https://api.github.com/repos/18F/e-manifest/issues{/number}","pulls_url":"https://api.github.com/repos/18F/e-manifest/pulls{/number}","milestones_url":"https://api.github.com/repos/18F/e-manifest/milestones{/number}","notifications_url":"https://api.github.com/repos/18F/e-manifest/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/18F/e-manifest/labels{/name}","releases_url":"https://api.github.com/repos/18F/e-manifest/releases{/id}","created_at":"2015-07-10T15:26:47Z","updated_at":"2015-12-07T23:09:12Z","pushed_at":"2016-01-05T22:37:53Z","git_url":"git://github.com/18F/e-manifest.git","ssh_url":"git@github.com:18F/e-manifest.git","clone_url":"https://github.com/18F/e-manifest.git","svn_url":"https://github.com/18F/e-manifest","homepage":null,"size":21433,"stargazers_count":3,"watchers_count":3,"language":"CSS","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4,"mirror_url":null,"open_issues_count":5,"forks":4,"open_issues":5,"watchers":3,"default_branch":"master"}},"base":{"label":"18F:master","ref":"master","sha":"a150a1cdb1c5466e28a38828152535d16a61e712","user":{"login":"18F","id":6233994,"avatar_url":"https://avatars.githubusercontent.com/u/6233994?v=3","gravatar_id":"","url":"https://api.github.com/users/18F","html_url":"https://github.com/18F","followers_url":"https://api.github.com/users/18F/followers","following_url":"https://api.github.com/users/18F/following{/other_user}","gists_url":"https://api.github.com/users/18F/gists{/gist_id}","starred_url":"https://api.github.com/users/18F/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/18F/subscriptions","organizations_url":"https://api.github.com/users/18F/orgs","repos_url":"https://api.github.com/users/18F/repos","events_url":"https://api.github.com/users/18F/events{/privacy}","received_events_url":"https://api.github.com/users/18F/received_events","type":"Organization","site_admin":false},"repo":{"id":38887537,"name":"e-manifest","full_name":"18F/e-manifest","owner":{"login":"18F","id":6233994,"avatar_url":"https://avatars.githubusercontent.com/u/6233994?v=3","gravatar_id":"","url":"https://api.github.com/users/18F","html_url":"https://github.com/18F","followers_url":"https://api.github.com/users/18F/followers","following_url":"https://api.github.com/users/18F/following{/other_user}","gists_url":"https://api.github.com/users/18F/gists{/gist_id}","starred_url":"https://api.github.com/users/18F/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/18F/subscriptions","organizations_url":"https://api.github.com/users/18F/orgs","repos_url":"https://api.github.com/users/18F/repos","events_url":"https://api.github.com/users/18F/events{/privacy}","received_events_url":"https://api.github.com/users/18F/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/18F/e-manifest","description":"The EPA e-Manifest project.","fork":false,"url":"https://api.github.com/repos/18F/e-manifest","forks_url":"https://api.github.com/repos/18F/e-manifest/forks","keys_url":"https://api.github.com/repos/18F/e-manifest/keys{/key_id}","collaborators_url":"https://api.github.com/repos/18F/e-manifest/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/18F/e-manifest/teams","hooks_url":"https://api.github.com/repos/18F/e-manifest/hooks","issue_events_url":"https://api.github.com/repos/18F/e-manifest/issues/events{/number}","events_url":"https://api.github.com/repos/18F/e-manifest/events","assignees_url":"https://api.github.com/repos/18F/e-manifest/assignees{/user}","branches_url":"https://api.github.com/repos/18F/e-manifest/branches{/branch}","tags_url":"https://api.github.com/repos/18F/e-manifest/tags","blobs_url":"https://api.github.com/repos/18F/e-manifest/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/18F/e-manifest/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/18F/e-manifest/git/refs{/sha}","trees_url":"https://api.github.com/repos/18F/e-manifest/git/trees{/sha}","statuses_url":"https://api.github.com/repos/18F/e-manifest/statuses/{sha}","languages_url":"https://api.github.com/repos/18F/e-manifest/languages","stargazers_url":"https://api.github.com/repos/18F/e-manifest/stargazers","contributors_url":"https://api.github.com/repos/18F/e-manifest/contributors","subscribers_url":"https://api.github.com/repos/18F/e-manifest/subscribers","subscription_url":"https://api.github.com/repos/18F/e-manifest/subscription","commits_url":"https://api.github.com/repos/18F/e-manifest/commits{/sha}","git_commits_url":"https://api.github.com/repos/18F/e-manifest/git/commits{/sha}","comments_url":"https://api.github.com/repos/18F/e-manifest/comments{/number}","issue_comment_url":"https://api.github.com/repos/18F/e-manifest/issues/comments{/number}","contents_url":"https://api.github.com/repos/18F/e-manifest/contents/{+path}","compare_url":"https://api.github.com/repos/18F/e-manifest/compare/{base}...{head}","merges_url":"https://api.github.com/repos/18F/e-manifest/merges","archive_url":"https://api.github.com/repos/18F/e-manifest/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/18F/e-manifest/downloads","issues_url":"https://api.github.com/repos/18F/e-manifest/issues{/number}","pulls_url":"https://api.github.com/repos/18F/e-manifest/pulls{/number}","milestones_url":"https://api.github.com/repos/18F/e-manifest/milestones{/number}","notifications_url":"https://api.github.com/repos/18F/e-manifest/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/18F/e-manifest/labels{/name}","releases_url":"https://api.github.com/repos/18F/e-manifest/releases{/id}","created_at":"2015-07-10T15:26:47Z","updated_at":"2015-12-07T23:09:12Z","pushed_at":"2016-01-05T22:37:53Z","git_url":"git://github.com/18F/e-manifest.git","ssh_url":"git@github.com:18F/e-manifest.git","clone_url":"https://github.com/18F/e-manifest.git","svn_url":"https://github.com/18F/e-manifest","homepage":null,"size":21433,"stargazers_count":3,"watchers_count":3,"language":"CSS","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4,"mirror_url":null,"open_issues_count":5,"forks":4,"open_issues":5,"watchers":3,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/18F/e-manifest/pulls/14"},"html":{"href":"https://github.com/18F/e-manifest/pull/14"},"issue":{"href":"https://api.github.com/repos/18F/e-manifest/issues/14"},"comments":{"href":"https://api.github.com/repos/18F/e-manifest/issues/14/comments"},"review_comments":{"href":"https://api.github.com/repos/18F/e-manifest/pulls/14/comments"},"review_comment":{"href":"https://api.github.com/repos/18F/e-manifest/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/18F/e-manifest/pulls/14/commits"},"statuses":{"href":"https://api.github.com/repos/18F/e-manifest/statuses/25fab3affb9260d958bb6d573c4af0742561b37e"}}}}