{"action":"created","comment":{"url":"https://api.github.com/repos/inspirehep/inspire-next/pulls/comments/46414712","id":46414712,"diff_hunk":"@@ -154,6 +159,52 @@ def migrate_chunk(chunk, broken_output=None, dry_run=False):\n         models_committed.connect(record_modification)\n \n \n+@celery.task(ignore_result=True)\n+def add_citation_counts():\n+    from elasticsearch.helpers import bulk as es_bulk\n+    from elasticsearch.helpers import scan as es_scan\n+    from collections import Counter\n+\n+    # list of update-jsons for each record\n+    records_to_update = []\n+\n+    # lookup dictionary where key: recid of the record\n+    # and value: number of records that cite that record\n+    citations_lookup = Counter()\n+    for record in es_scan(\n+            es,\n+            query={\n+                \"query\": {\n+                    \"match_all\": {}},\n+                \"size\": CHUNK_SIZE},\n+            scroll=u'1m',\n+            index=\"hep\",\n+            doc_type=\"record\"):\n+\n+        # update lookup dictionary based on references of the record\n+        if 'references' in record['_source']:\n+            references = record['_source']['references']\n+            for reference in references:\n+                recid = unicode(reference.get('recid'))\n+                if recid is not None:\n+                    citations_lookup[recid] += 1\n+\n+        # prepare json to update the record\n+        json = {'_op_type': 'update',\n+                '_index': 'hep',\n+                '_type': 'record',\n+                '_id': record['_id'],\n+                }\n+        records_to_update.append(json)\n+\n+    for json in records_to_update:\n+        citation_count = citations_lookup[json['_id']]\n+        json.update({'doc': {'citation_count': citation_count}})","path":"inspirehep/modules/migrator/tasks.py","position":123,"original_position":118,"commit_id":"86748fe7e06d4d8048847f538a803a0184028224","original_commit_id":"db1d51a44c7bd2f09d1a90e8f1e1fc25b633987a","user":{"login":"tomaszgy","id":10139760,"avatar_url":"https://avatars.githubusercontent.com/u/10139760?v=3","gravatar_id":"","url":"https://api.github.com/users/tomaszgy","html_url":"https://github.com/tomaszgy","followers_url":"https://api.github.com/users/tomaszgy/followers","following_url":"https://api.github.com/users/tomaszgy/following{/other_user}","gists_url":"https://api.github.com/users/tomaszgy/gists{/gist_id}","starred_url":"https://api.github.com/users/tomaszgy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tomaszgy/subscriptions","organizations_url":"https://api.github.com/users/tomaszgy/orgs","repos_url":"https://api.github.com/users/tomaszgy/repos","events_url":"https://api.github.com/users/tomaszgy/events{/privacy}","received_events_url":"https://api.github.com/users/tomaszgy/received_events","type":"User","site_admin":false},"body":"I believe it's correct - https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html#_updates_with_a_partial_document","created_at":"2015-12-02T13:44:11Z","updated_at":"2015-12-02T13:44:11Z","html_url":"https://github.com/inspirehep/inspire-next/pull/588#discussion_r46414712","pull_request_url":"https://api.github.com/repos/inspirehep/inspire-next/pulls/588","_links":{"self":{"href":"https://api.github.com/repos/inspirehep/inspire-next/pulls/comments/46414712"},"html":{"href":"https://github.com/inspirehep/inspire-next/pull/588#discussion_r46414712"},"pull_request":{"href":"https://api.github.com/repos/inspirehep/inspire-next/pulls/588"}}},"pull_request":{"url":"https://api.github.com/repos/inspirehep/inspire-next/pulls/588","id":52144198,"html_url":"https://github.com/inspirehep/inspire-next/pull/588","diff_url":"https://github.com/inspirehep/inspire-next/pull/588.diff","patch_url":"https://github.com/inspirehep/inspire-next/pull/588.patch","issue_url":"https://api.github.com/repos/inspirehep/inspire-next/issues/588","number":588,"state":"open","locked":false,"title":"citations: add migrator command","user":{"login":"tomaszgy","id":10139760,"avatar_url":"https://avatars.githubusercontent.com/u/10139760?v=3","gravatar_id":"","url":"https://api.github.com/users/tomaszgy","html_url":"https://github.com/tomaszgy","followers_url":"https://api.github.com/users/tomaszgy/followers","following_url":"https://api.github.com/users/tomaszgy/following{/other_user}","gists_url":"https://api.github.com/users/tomaszgy/gists{/gist_id}","starred_url":"https://api.github.com/users/tomaszgy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tomaszgy/subscriptions","organizations_url":"https://api.github.com/users/tomaszgy/orgs","repos_url":"https://api.github.com/users/tomaszgy/repos","events_url":"https://api.github.com/users/tomaszgy/events{/privacy}","received_events_url":"https://api.github.com/users/tomaszgy/received_events","type":"User","site_admin":false},"body":"citations: add migrator command to count citations count for all records added during migration\r\n\r\nSigned-off-by: Tomasz <tomaszgy@gmail.com>","created_at":"2015-11-30T16:16:43Z","updated_at":"2015-12-02T13:44:11Z","closed_at":null,"merged_at":null,"merge_commit_sha":"6b7ee8fd533ba5e6a6efcbe05a773dffe8de2b7d","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/inspirehep/inspire-next/pulls/588/commits","review_comments_url":"https://api.github.com/repos/inspirehep/inspire-next/pulls/588/comments","review_comment_url":"https://api.github.com/repos/inspirehep/inspire-next/pulls/comments{/number}","comments_url":"https://api.github.com/repos/inspirehep/inspire-next/issues/588/comments","statuses_url":"https://api.github.com/repos/inspirehep/inspire-next/statuses/86748fe7e06d4d8048847f538a803a0184028224","head":{"label":"tomaszgy:citations_migrator","ref":"citations_migrator","sha":"86748fe7e06d4d8048847f538a803a0184028224","user":{"login":"tomaszgy","id":10139760,"avatar_url":"https://avatars.githubusercontent.com/u/10139760?v=3","gravatar_id":"","url":"https://api.github.com/users/tomaszgy","html_url":"https://github.com/tomaszgy","followers_url":"https://api.github.com/users/tomaszgy/followers","following_url":"https://api.github.com/users/tomaszgy/following{/other_user}","gists_url":"https://api.github.com/users/tomaszgy/gists{/gist_id}","starred_url":"https://api.github.com/users/tomaszgy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tomaszgy/subscriptions","organizations_url":"https://api.github.com/users/tomaszgy/orgs","repos_url":"https://api.github.com/users/tomaszgy/repos","events_url":"https://api.github.com/users/tomaszgy/events{/privacy}","received_events_url":"https://api.github.com/users/tomaszgy/received_events","type":"User","site_admin":false},"repo":{"id":45853625,"name":"inspire-next","full_name":"tomaszgy/inspire-next","owner":{"login":"tomaszgy","id":10139760,"avatar_url":"https://avatars.githubusercontent.com/u/10139760?v=3","gravatar_id":"","url":"https://api.github.com/users/tomaszgy","html_url":"https://github.com/tomaszgy","followers_url":"https://api.github.com/users/tomaszgy/followers","following_url":"https://api.github.com/users/tomaszgy/following{/other_user}","gists_url":"https://api.github.com/users/tomaszgy/gists{/gist_id}","starred_url":"https://api.github.com/users/tomaszgy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tomaszgy/subscriptions","organizations_url":"https://api.github.com/users/tomaszgy/orgs","repos_url":"https://api.github.com/users/tomaszgy/repos","events_url":"https://api.github.com/users/tomaszgy/events{/privacy}","received_events_url":"https://api.github.com/users/tomaszgy/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/tomaszgy/inspire-next","description":"INSPIRE overlay repository to be used with https://github.com/inspirehep/invenio@labs branch","fork":true,"url":"https://api.github.com/repos/tomaszgy/inspire-next","forks_url":"https://api.github.com/repos/tomaszgy/inspire-next/forks","keys_url":"https://api.github.com/repos/tomaszgy/inspire-next/keys{/key_id}","collaborators_url":"https://api.github.com/repos/tomaszgy/inspire-next/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/tomaszgy/inspire-next/teams","hooks_url":"https://api.github.com/repos/tomaszgy/inspire-next/hooks","issue_events_url":"https://api.github.com/repos/tomaszgy/inspire-next/issues/events{/number}","events_url":"https://api.github.com/repos/tomaszgy/inspire-next/events","assignees_url":"https://api.github.com/repos/tomaszgy/inspire-next/assignees{/user}","branches_url":"https://api.github.com/repos/tomaszgy/inspire-next/branches{/branch}","tags_url":"https://api.github.com/repos/tomaszgy/inspire-next/tags","blobs_url":"https://api.github.com/repos/tomaszgy/inspire-next/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/tomaszgy/inspire-next/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/tomaszgy/inspire-next/git/refs{/sha}","trees_url":"https://api.github.com/repos/tomaszgy/inspire-next/git/trees{/sha}","statuses_url":"https://api.github.com/repos/tomaszgy/inspire-next/statuses/{sha}","languages_url":"https://api.github.com/repos/tomaszgy/inspire-next/languages","stargazers_url":"https://api.github.com/repos/tomaszgy/inspire-next/stargazers","contributors_url":"https://api.github.com/repos/tomaszgy/inspire-next/contributors","subscribers_url":"https://api.github.com/repos/tomaszgy/inspire-next/subscribers","subscription_url":"https://api.github.com/repos/tomaszgy/inspire-next/subscription","commits_url":"https://api.github.com/repos/tomaszgy/inspire-next/commits{/sha}","git_commits_url":"https://api.github.com/repos/tomaszgy/inspire-next/git/commits{/sha}","comments_url":"https://api.github.com/repos/tomaszgy/inspire-next/comments{/number}","issue_comment_url":"https://api.github.com/repos/tomaszgy/inspire-next/issues/comments{/number}","contents_url":"https://api.github.com/repos/tomaszgy/inspire-next/contents/{+path}","compare_url":"https://api.github.com/repos/tomaszgy/inspire-next/compare/{base}...{head}","merges_url":"https://api.github.com/repos/tomaszgy/inspire-next/merges","archive_url":"https://api.github.com/repos/tomaszgy/inspire-next/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/tomaszgy/inspire-next/downloads","issues_url":"https://api.github.com/repos/tomaszgy/inspire-next/issues{/number}","pulls_url":"https://api.github.com/repos/tomaszgy/inspire-next/pulls{/number}","milestones_url":"https://api.github.com/repos/tomaszgy/inspire-next/milestones{/number}","notifications_url":"https://api.github.com/repos/tomaszgy/inspire-next/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/tomaszgy/inspire-next/labels{/name}","releases_url":"https://api.github.com/repos/tomaszgy/inspire-next/releases{/id}","created_at":"2015-11-09T17:13:36Z","updated_at":"2015-11-09T17:13:43Z","pushed_at":"2015-12-02T13:40:11Z","git_url":"git://github.com/tomaszgy/inspire-next.git","ssh_url":"git@github.com:tomaszgy/inspire-next.git","clone_url":"https://github.com/tomaszgy/inspire-next.git","svn_url":"https://github.com/tomaszgy/inspire-next","homepage":"https://labs.inspirehep.net","size":33373,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"inspirehep:master","ref":"master","sha":"7a9d2ecd1a9e4d947e7cfdbe5df713a96702a271","user":{"login":"inspirehep","id":356664,"avatar_url":"https://avatars.githubusercontent.com/u/356664?v=3","gravatar_id":"","url":"https://api.github.com/users/inspirehep","html_url":"https://github.com/inspirehep","followers_url":"https://api.github.com/users/inspirehep/followers","following_url":"https://api.github.com/users/inspirehep/following{/other_user}","gists_url":"https://api.github.com/users/inspirehep/gists{/gist_id}","starred_url":"https://api.github.com/users/inspirehep/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/inspirehep/subscriptions","organizations_url":"https://api.github.com/users/inspirehep/orgs","repos_url":"https://api.github.com/users/inspirehep/repos","events_url":"https://api.github.com/users/inspirehep/events{/privacy}","received_events_url":"https://api.github.com/users/inspirehep/received_events","type":"Organization","site_admin":false},"repo":{"id":15221515,"name":"inspire-next","full_name":"inspirehep/inspire-next","owner":{"login":"inspirehep","id":356664,"avatar_url":"https://avatars.githubusercontent.com/u/356664?v=3","gravatar_id":"","url":"https://api.github.com/users/inspirehep","html_url":"https://github.com/inspirehep","followers_url":"https://api.github.com/users/inspirehep/followers","following_url":"https://api.github.com/users/inspirehep/following{/other_user}","gists_url":"https://api.github.com/users/inspirehep/gists{/gist_id}","starred_url":"https://api.github.com/users/inspirehep/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/inspirehep/subscriptions","organizations_url":"https://api.github.com/users/inspirehep/orgs","repos_url":"https://api.github.com/users/inspirehep/repos","events_url":"https://api.github.com/users/inspirehep/events{/privacy}","received_events_url":"https://api.github.com/users/inspirehep/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/inspirehep/inspire-next","description":"INSPIRE overlay repository to be used with https://github.com/inspirehep/invenio@labs branch","fork":false,"url":"https://api.github.com/repos/inspirehep/inspire-next","forks_url":"https://api.github.com/repos/inspirehep/inspire-next/forks","keys_url":"https://api.github.com/repos/inspirehep/inspire-next/keys{/key_id}","collaborators_url":"https://api.github.com/repos/inspirehep/inspire-next/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/inspirehep/inspire-next/teams","hooks_url":"https://api.github.com/repos/inspirehep/inspire-next/hooks","issue_events_url":"https://api.github.com/repos/inspirehep/inspire-next/issues/events{/number}","events_url":"https://api.github.com/repos/inspirehep/inspire-next/events","assignees_url":"https://api.github.com/repos/inspirehep/inspire-next/assignees{/user}","branches_url":"https://api.github.com/repos/inspirehep/inspire-next/branches{/branch}","tags_url":"https://api.github.com/repos/inspirehep/inspire-next/tags","blobs_url":"https://api.github.com/repos/inspirehep/inspire-next/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/inspirehep/inspire-next/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/inspirehep/inspire-next/git/refs{/sha}","trees_url":"https://api.github.com/repos/inspirehep/inspire-next/git/trees{/sha}","statuses_url":"https://api.github.com/repos/inspirehep/inspire-next/statuses/{sha}","languages_url":"https://api.github.com/repos/inspirehep/inspire-next/languages","stargazers_url":"https://api.github.com/repos/inspirehep/inspire-next/stargazers","contributors_url":"https://api.github.com/repos/inspirehep/inspire-next/contributors","subscribers_url":"https://api.github.com/repos/inspirehep/inspire-next/subscribers","subscription_url":"https://api.github.com/repos/inspirehep/inspire-next/subscription","commits_url":"https://api.github.com/repos/inspirehep/inspire-next/commits{/sha}","git_commits_url":"https://api.github.com/repos/inspirehep/inspire-next/git/commits{/sha}","comments_url":"https://api.github.com/repos/inspirehep/inspire-next/comments{/number}","issue_comment_url":"https://api.github.com/repos/inspirehep/inspire-next/issues/comments{/number}","contents_url":"https://api.github.com/repos/inspirehep/inspire-next/contents/{+path}","compare_url":"https://api.github.com/repos/inspirehep/inspire-next/compare/{base}...{head}","merges_url":"https://api.github.com/repos/inspirehep/inspire-next/merges","archive_url":"https://api.github.com/repos/inspirehep/inspire-next/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/inspirehep/inspire-next/downloads","issues_url":"https://api.github.com/repos/inspirehep/inspire-next/issues{/number}","pulls_url":"https://api.github.com/repos/inspirehep/inspire-next/pulls{/number}","milestones_url":"https://api.github.com/repos/inspirehep/inspire-next/milestones{/number}","notifications_url":"https://api.github.com/repos/inspirehep/inspire-next/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/inspirehep/inspire-next/labels{/name}","releases_url":"https://api.github.com/repos/inspirehep/inspire-next/releases{/id}","created_at":"2013-12-16T09:11:58Z","updated_at":"2015-10-13T09:48:55Z","pushed_at":"2015-12-02T13:42:26Z","git_url":"git://github.com/inspirehep/inspire-next.git","ssh_url":"git@github.com:inspirehep/inspire-next.git","clone_url":"https://github.com/inspirehep/inspire-next.git","svn_url":"https://github.com/inspirehep/inspire-next","homepage":"https://labs.inspirehep.net","size":37839,"stargazers_count":8,"watchers_count":8,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":24,"mirror_url":null,"open_issues_count":95,"forks":24,"open_issues":95,"watchers":8,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/inspirehep/inspire-next/pulls/588"},"html":{"href":"https://github.com/inspirehep/inspire-next/pull/588"},"issue":{"href":"https://api.github.com/repos/inspirehep/inspire-next/issues/588"},"comments":{"href":"https://api.github.com/repos/inspirehep/inspire-next/issues/588/comments"},"review_comments":{"href":"https://api.github.com/repos/inspirehep/inspire-next/pulls/588/comments"},"review_comment":{"href":"https://api.github.com/repos/inspirehep/inspire-next/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/inspirehep/inspire-next/pulls/588/commits"},"statuses":{"href":"https://api.github.com/repos/inspirehep/inspire-next/statuses/86748fe7e06d4d8048847f538a803a0184028224"}}}}