{"action":"created","comment":{"url":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments/28282996","id":28282996,"diff_hunk":"@@ -0,0 +1,451 @@\n+/*\n+ * Licensed to Elasticsearch under one or more contributor\n+ * license agreements. See the NOTICE file distributed with\n+ * this work for additional information regarding copyright\n+ * ownership. Elasticsearch licenses this file to you under\n+ * the Apache License, Version 2.0 (the \"License\"); you may\n+ * not use this file except in compliance with the License.\n+ * You may obtain a copy of the License at\n+ *\n+ *    http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing,\n+ * software distributed under the License is distributed on an\n+ * \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n+ * KIND, either express or implied.  See the License for the\n+ * specific language governing permissions and limitations\n+ * under the License.\n+ */\n+\n+package org.elasticsearch.action.fieldstats;\n+\n+import org.apache.lucene.util.BytesRef;\n+import org.elasticsearch.common.io.stream.StreamInput;\n+import org.elasticsearch.common.io.stream.StreamOutput;\n+import org.elasticsearch.common.io.stream.Streamable;\n+import org.elasticsearch.common.joda.FormatDateTimeFormatter;\n+import org.elasticsearch.common.joda.Joda;\n+import org.elasticsearch.common.xcontent.ToXContent;\n+import org.elasticsearch.common.xcontent.XContentBuilder;\n+import org.elasticsearch.common.xcontent.XContentBuilderString;\n+\n+import java.io.IOException;\n+\n+/**\n+ */\n+public abstract class FieldStats<T> implements Streamable, ToXContent {\n+\n+    private byte type;\n+    private long maxDoc;\n+    private long docCount;\n+    private long sumDocFreq;\n+    private long sumTotalTermFreq;\n+    protected T minValue;\n+    protected T maxValue;\n+\n+    protected FieldStats() {\n+    }\n+\n+    protected FieldStats(int type, long maxDoc, long docCount, long sumDocFreq, long sumTotalTermFreq) {\n+        this.type = (byte) type;\n+        this.maxDoc = maxDoc;\n+        this.docCount = docCount;\n+        this.sumDocFreq = sumDocFreq;\n+        this.sumTotalTermFreq = sumTotalTermFreq;\n+    }\n+\n+    /**\n+     * @return the total number of documents.\n+     *\n+     * Note that, documents marked as deleted that haven't yet been merged way aren't taken into account.\n+     */\n+    public long getMaxDoc() {\n+        return maxDoc;\n+    }\n+\n+    /**\n+     * @return the number of documents that have at least one term for this field, or -1 if this measurement isn't available.\n+     *\n+     * Note that, documents marked as deleted that haven't yet been merged way aren't taken into account.\n+     */\n+    public long getDocCount() {\n+        return docCount;\n+    }\n+\n+    /**\n+     * @return A ratio as percentage of between all the documents that have at least one value for this field and all documents. (doc_count / max_doc)","path":"src/main/java/org/elasticsearch/action/fieldstats/FieldStats.java","position":76,"original_position":76,"commit_id":"d3809695f05b528506b49185bfabc8bd84d1592e","original_commit_id":"d3809695f05b528506b49185bfabc8bd84d1592e","user":{"login":"rmuir","id":504194,"avatar_url":"https://avatars.githubusercontent.com/u/504194?v=3","gravatar_id":"","url":"https://api.github.com/users/rmuir","html_url":"https://github.com/rmuir","followers_url":"https://api.github.com/users/rmuir/followers","following_url":"https://api.github.com/users/rmuir/following{/other_user}","gists_url":"https://api.github.com/users/rmuir/gists{/gist_id}","starred_url":"https://api.github.com/users/rmuir/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rmuir/subscriptions","organizations_url":"https://api.github.com/users/rmuir/orgs","repos_url":"https://api.github.com/users/rmuir/repos","events_url":"https://api.github.com/users/rmuir/events{/privacy}","received_events_url":"https://api.github.com/users/rmuir/received_events","type":"User","site_admin":false},"body":"and maybe drop any improved description here too","created_at":"2015-04-13T21:50:56Z","updated_at":"2015-04-13T21:50:56Z","html_url":"https://github.com/elastic/elasticsearch/pull/10523#discussion_r28282996","pull_request_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/10523","_links":{"self":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments/28282996"},"html":{"href":"https://github.com/elastic/elasticsearch/pull/10523#discussion_r28282996"},"pull_request":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/10523"}}},"pull_request":{"url":"https://api.github.com/repos/elastic/elasticsearch/pulls/10523","id":33023442,"html_url":"https://github.com/elastic/elasticsearch/pull/10523","diff_url":"https://github.com/elastic/elasticsearch/pull/10523.diff","patch_url":"https://github.com/elastic/elasticsearch/pull/10523.patch","issue_url":"https://api.github.com/repos/elastic/elasticsearch/issues/10523","number":10523,"state":"open","locked":false,"title":"Add field stats api","user":{"login":"martijnvg","id":580421,"avatar_url":"https://avatars.githubusercontent.com/u/580421?v=3","gravatar_id":"","url":"https://api.github.com/users/martijnvg","html_url":"https://github.com/martijnvg","followers_url":"https://api.github.com/users/martijnvg/followers","following_url":"https://api.github.com/users/martijnvg/following{/other_user}","gists_url":"https://api.github.com/users/martijnvg/gists{/gist_id}","starred_url":"https://api.github.com/users/martijnvg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/martijnvg/subscriptions","organizations_url":"https://api.github.com/users/martijnvg/orgs","repos_url":"https://api.github.com/users/martijnvg/repos","events_url":"https://api.github.com/users/martijnvg/events{/privacy}","received_events_url":"https://api.github.com/users/martijnvg/received_events","type":"User","site_admin":false},"body":"The field stats api should return field level statistics such as lowest, highest values and number of documents that have at least one value for a field.\r\n\r\nAn api like this can be useful to explore a data set you don't know much about. For example you can figure at with the lowest and highest response times are, so that you can create a histogram or range aggregation with sane settings.\r\n\r\nThis api doesn't run a search to figure this statistics out, but rather use the Lucene index look these statics up (using `Terms` class in Lucene). So finding out these stats for fields is cheap and quick.\r\n\r\nThe min/max values are based on the type of the field. So for a numeric field min/max are numbers and date field the min/max date and other fields the min/max are term based. \r\n\r\nThe api is quite straight forward it expects a list of fields and optionally an index can be provided:\r\n(ran on a stack exchange dump)\r\n\r\n```\r\ncurl -XGET \"http://localhost:9200/_field_stats?fields=comment_count,view_count,answer_count,creation_date,display_name\"\r\n```\r\n\r\nThe response looks like this:\r\n\r\n```json\r\n{\r\n   \"_shards\": {\r\n      \"total\": 1,\r\n      \"successful\": 1,\r\n      \"failed\": 0\r\n   },\r\n   \"fields\": {\r\n      \"comment_count\": {\r\n         \"doc_count\": 202940,\r\n         \"min_value\": 0,\r\n         \"max_value\": 98\r\n      },\r\n      \"creation_date\": {\r\n         \"doc_count\": 564633,\r\n         \"min_value\": \"2008-08-01T16:37:51.513Z\",\r\n         \"max_value\": \"2013-06-02T03:23:11.593Z\"\r\n      },\r\n      \"display_name\": {\r\n         \"doc_count\": 126741,\r\n         \"min_value\": \"0\",\r\n         \"max_value\": \"정혜선\"\r\n      },\r\n      \"answer_count\": {\r\n         \"doc_count\": 139885,\r\n         \"min_value\": 0,\r\n         \"max_value\": 160\r\n      },\r\n      \"view_count\": {\r\n         \"doc_count\": 152357,\r\n         \"min_value\": 1,\r\n         \"max_value\": 506955\r\n      },\r\n   }\r\n}\r\n```\r\n\r\nThis is a work in progress. Things to be done are documentation and tests.","created_at":"2015-04-10T07:20:36Z","updated_at":"2015-04-13T21:50:56Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d197a8c4d1f9d073c43ec5e9ccd70c5a22dcc036","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/10523/commits","review_comments_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/10523/comments","review_comment_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments{/number}","comments_url":"https://api.github.com/repos/elastic/elasticsearch/issues/10523/comments","statuses_url":"https://api.github.com/repos/elastic/elasticsearch/statuses/d3809695f05b528506b49185bfabc8bd84d1592e","head":{"label":"martijnvg:field_stats","ref":"field_stats","sha":"d3809695f05b528506b49185bfabc8bd84d1592e","user":{"login":"martijnvg","id":580421,"avatar_url":"https://avatars.githubusercontent.com/u/580421?v=3","gravatar_id":"","url":"https://api.github.com/users/martijnvg","html_url":"https://github.com/martijnvg","followers_url":"https://api.github.com/users/martijnvg/followers","following_url":"https://api.github.com/users/martijnvg/following{/other_user}","gists_url":"https://api.github.com/users/martijnvg/gists{/gist_id}","starred_url":"https://api.github.com/users/martijnvg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/martijnvg/subscriptions","organizations_url":"https://api.github.com/users/martijnvg/orgs","repos_url":"https://api.github.com/users/martijnvg/repos","events_url":"https://api.github.com/users/martijnvg/events{/privacy}","received_events_url":"https://api.github.com/users/martijnvg/received_events","type":"User","site_admin":false},"repo":{"id":3584739,"name":"elasticsearch","full_name":"martijnvg/elasticsearch","owner":{"login":"martijnvg","id":580421,"avatar_url":"https://avatars.githubusercontent.com/u/580421?v=3","gravatar_id":"","url":"https://api.github.com/users/martijnvg","html_url":"https://github.com/martijnvg","followers_url":"https://api.github.com/users/martijnvg/followers","following_url":"https://api.github.com/users/martijnvg/following{/other_user}","gists_url":"https://api.github.com/users/martijnvg/gists{/gist_id}","starred_url":"https://api.github.com/users/martijnvg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/martijnvg/subscriptions","organizations_url":"https://api.github.com/users/martijnvg/orgs","repos_url":"https://api.github.com/users/martijnvg/repos","events_url":"https://api.github.com/users/martijnvg/events{/privacy}","received_events_url":"https://api.github.com/users/martijnvg/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/martijnvg/elasticsearch","description":"Open Source, Distributed, RESTful Search Engine with grouping","fork":true,"url":"https://api.github.com/repos/martijnvg/elasticsearch","forks_url":"https://api.github.com/repos/martijnvg/elasticsearch/forks","keys_url":"https://api.github.com/repos/martijnvg/elasticsearch/keys{/key_id}","collaborators_url":"https://api.github.com/repos/martijnvg/elasticsearch/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/martijnvg/elasticsearch/teams","hooks_url":"https://api.github.com/repos/martijnvg/elasticsearch/hooks","issue_events_url":"https://api.github.com/repos/martijnvg/elasticsearch/issues/events{/number}","events_url":"https://api.github.com/repos/martijnvg/elasticsearch/events","assignees_url":"https://api.github.com/repos/martijnvg/elasticsearch/assignees{/user}","branches_url":"https://api.github.com/repos/martijnvg/elasticsearch/branches{/branch}","tags_url":"https://api.github.com/repos/martijnvg/elasticsearch/tags","blobs_url":"https://api.github.com/repos/martijnvg/elasticsearch/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/martijnvg/elasticsearch/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/martijnvg/elasticsearch/git/refs{/sha}","trees_url":"https://api.github.com/repos/martijnvg/elasticsearch/git/trees{/sha}","statuses_url":"https://api.github.com/repos/martijnvg/elasticsearch/statuses/{sha}","languages_url":"https://api.github.com/repos/martijnvg/elasticsearch/languages","stargazers_url":"https://api.github.com/repos/martijnvg/elasticsearch/stargazers","contributors_url":"https://api.github.com/repos/martijnvg/elasticsearch/contributors","subscribers_url":"https://api.github.com/repos/martijnvg/elasticsearch/subscribers","subscription_url":"https://api.github.com/repos/martijnvg/elasticsearch/subscription","commits_url":"https://api.github.com/repos/martijnvg/elasticsearch/commits{/sha}","git_commits_url":"https://api.github.com/repos/martijnvg/elasticsearch/git/commits{/sha}","comments_url":"https://api.github.com/repos/martijnvg/elasticsearch/comments{/number}","issue_comment_url":"https://api.github.com/repos/martijnvg/elasticsearch/issues/comments{/number}","contents_url":"https://api.github.com/repos/martijnvg/elasticsearch/contents/{+path}","compare_url":"https://api.github.com/repos/martijnvg/elasticsearch/compare/{base}...{head}","merges_url":"https://api.github.com/repos/martijnvg/elasticsearch/merges","archive_url":"https://api.github.com/repos/martijnvg/elasticsearch/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/martijnvg/elasticsearch/downloads","issues_url":"https://api.github.com/repos/martijnvg/elasticsearch/issues{/number}","pulls_url":"https://api.github.com/repos/martijnvg/elasticsearch/pulls{/number}","milestones_url":"https://api.github.com/repos/martijnvg/elasticsearch/milestones{/number}","notifications_url":"https://api.github.com/repos/martijnvg/elasticsearch/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/martijnvg/elasticsearch/labels{/name}","releases_url":"https://api.github.com/repos/martijnvg/elasticsearch/releases{/id}","created_at":"2012-02-29T18:56:33Z","updated_at":"2014-08-09T22:19:31Z","pushed_at":"2015-04-13T21:26:50Z","git_url":"git://github.com/martijnvg/elasticsearch.git","ssh_url":"git@github.com:martijnvg/elasticsearch.git","clone_url":"https://github.com/martijnvg/elasticsearch.git","svn_url":"https://github.com/martijnvg/elasticsearch","homepage":"http://elasticsearch.org","size":109308,"stargazers_count":12,"watchers_count":12,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":0,"forks":2,"open_issues":0,"watchers":12,"default_branch":"master"}},"base":{"label":"elastic:master","ref":"master","sha":"224c43564b3c4d94398909927732551192a25fd8","user":{"login":"elastic","id":6764390,"avatar_url":"https://avatars.githubusercontent.com/u/6764390?v=3","gravatar_id":"","url":"https://api.github.com/users/elastic","html_url":"https://github.com/elastic","followers_url":"https://api.github.com/users/elastic/followers","following_url":"https://api.github.com/users/elastic/following{/other_user}","gists_url":"https://api.github.com/users/elastic/gists{/gist_id}","starred_url":"https://api.github.com/users/elastic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elastic/subscriptions","organizations_url":"https://api.github.com/users/elastic/orgs","repos_url":"https://api.github.com/users/elastic/repos","events_url":"https://api.github.com/users/elastic/events{/privacy}","received_events_url":"https://api.github.com/users/elastic/received_events","type":"Organization","site_admin":false},"repo":{"id":507775,"name":"elasticsearch","full_name":"elastic/elasticsearch","owner":{"login":"elastic","id":6764390,"avatar_url":"https://avatars.githubusercontent.com/u/6764390?v=3","gravatar_id":"","url":"https://api.github.com/users/elastic","html_url":"https://github.com/elastic","followers_url":"https://api.github.com/users/elastic/followers","following_url":"https://api.github.com/users/elastic/following{/other_user}","gists_url":"https://api.github.com/users/elastic/gists{/gist_id}","starred_url":"https://api.github.com/users/elastic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elastic/subscriptions","organizations_url":"https://api.github.com/users/elastic/orgs","repos_url":"https://api.github.com/users/elastic/repos","events_url":"https://api.github.com/users/elastic/events{/privacy}","received_events_url":"https://api.github.com/users/elastic/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/elastic/elasticsearch","description":"Open Source, Distributed, RESTful Search Engine","fork":false,"url":"https://api.github.com/repos/elastic/elasticsearch","forks_url":"https://api.github.com/repos/elastic/elasticsearch/forks","keys_url":"https://api.github.com/repos/elastic/elasticsearch/keys{/key_id}","collaborators_url":"https://api.github.com/repos/elastic/elasticsearch/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/elastic/elasticsearch/teams","hooks_url":"https://api.github.com/repos/elastic/elasticsearch/hooks","issue_events_url":"https://api.github.com/repos/elastic/elasticsearch/issues/events{/number}","events_url":"https://api.github.com/repos/elastic/elasticsearch/events","assignees_url":"https://api.github.com/repos/elastic/elasticsearch/assignees{/user}","branches_url":"https://api.github.com/repos/elastic/elasticsearch/branches{/branch}","tags_url":"https://api.github.com/repos/elastic/elasticsearch/tags","blobs_url":"https://api.github.com/repos/elastic/elasticsearch/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/elastic/elasticsearch/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/elastic/elasticsearch/git/refs{/sha}","trees_url":"https://api.github.com/repos/elastic/elasticsearch/git/trees{/sha}","statuses_url":"https://api.github.com/repos/elastic/elasticsearch/statuses/{sha}","languages_url":"https://api.github.com/repos/elastic/elasticsearch/languages","stargazers_url":"https://api.github.com/repos/elastic/elasticsearch/stargazers","contributors_url":"https://api.github.com/repos/elastic/elasticsearch/contributors","subscribers_url":"https://api.github.com/repos/elastic/elasticsearch/subscribers","subscription_url":"https://api.github.com/repos/elastic/elasticsearch/subscription","commits_url":"https://api.github.com/repos/elastic/elasticsearch/commits{/sha}","git_commits_url":"https://api.github.com/repos/elastic/elasticsearch/git/commits{/sha}","comments_url":"https://api.github.com/repos/elastic/elasticsearch/comments{/number}","issue_comment_url":"https://api.github.com/repos/elastic/elasticsearch/issues/comments{/number}","contents_url":"https://api.github.com/repos/elastic/elasticsearch/contents/{+path}","compare_url":"https://api.github.com/repos/elastic/elasticsearch/compare/{base}...{head}","merges_url":"https://api.github.com/repos/elastic/elasticsearch/merges","archive_url":"https://api.github.com/repos/elastic/elasticsearch/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/elastic/elasticsearch/downloads","issues_url":"https://api.github.com/repos/elastic/elasticsearch/issues{/number}","pulls_url":"https://api.github.com/repos/elastic/elasticsearch/pulls{/number}","milestones_url":"https://api.github.com/repos/elastic/elasticsearch/milestones{/number}","notifications_url":"https://api.github.com/repos/elastic/elasticsearch/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/elastic/elasticsearch/labels{/name}","releases_url":"https://api.github.com/repos/elastic/elasticsearch/releases{/id}","created_at":"2010-02-08T13:20:56Z","updated_at":"2015-04-13T21:06:30Z","pushed_at":"2015-04-13T21:26:54Z","git_url":"git://github.com/elastic/elasticsearch.git","ssh_url":"git@github.com:elastic/elasticsearch.git","clone_url":"https://github.com/elastic/elasticsearch.git","svn_url":"https://github.com/elastic/elasticsearch","homepage":"http://elasticsearch.org","size":326691,"stargazers_count":10771,"watchers_count":10771,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":3370,"mirror_url":null,"open_issues_count":1087,"forks":3370,"open_issues":1087,"watchers":10771,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/10523"},"html":{"href":"https://github.com/elastic/elasticsearch/pull/10523"},"issue":{"href":"https://api.github.com/repos/elastic/elasticsearch/issues/10523"},"comments":{"href":"https://api.github.com/repos/elastic/elasticsearch/issues/10523/comments"},"review_comments":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/10523/comments"},"review_comment":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/10523/commits"},"statuses":{"href":"https://api.github.com/repos/elastic/elasticsearch/statuses/d3809695f05b528506b49185bfabc8bd84d1592e"}}}}