Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple Publishing Frequency By Day Of The Week
HOST='http://localhost:9200'
INDEX='articles'
curl -s -X DELETE "$HOST/$INDEX" -o /dev/null
curl -s -X POST "$HOST/$INDEX/article" -d '{ "t" : "1", "published" : "2010-01-01T02:20:00" }' -o /dev/null # fri
curl -s -X POST "$HOST/$INDEX/article" -d '{ "t" : "2", "published" : "2010-01-02T02:20:00" }' -o /dev/null # sat
curl -s -X POST "$HOST/$INDEX/article" -d '{ "t" : "3", "published" : "2010-01-02T08:40:00" }' -o /dev/null # sat
curl -s -X POST "$HOST/$INDEX/article" -d '{ "t" : "4", "published" : "2010-01-03T08:10:00" }' -o /dev/null # sun
curl -s -X POST "$HOST/$INDEX/article" -d '{ "t" : "5", "published" : "2010-01-04T16:20:00" }' -o /dev/null # mon
curl -s -X POST "$HOST/$INDEX/article" -d '{ "t" : "6", "published" : "2010-01-04T16:40:00" }' -o /dev/null # mon
curl -s -X POST "$HOST/$INDEX/article" -d '{ "t" : "7", "published" : "2010-01-04T18:10:00" }' -o /dev/null # mon
curl -s -X POST "$HOST/$INDEX/article" -d '{ "t" : "8", "published" : "2010-01-04T18:10:00" }' -o /dev/null # mon
curl -s -X POST "$HOST/$INDEX/article" -d '{ "t" : "9", "published" : "2010-01-10T18:40:00" }' -o /dev/null # sun
curl -s -X POST "$HOST/$INDEX/article" -d '{ "t" : "10", "published" : "2010-01-12T10:00:00" }' -o /dev/null # tue
curl -s -X POST "$HOST/$INDEX/article" -d '{ "t" : "11", "published" : "2010-01-13T11:05:00" }' -o /dev/null # wed
curl -s -X POST "$HOST/$INDEX/article" -d '{ "t" : "12", "published" : "2010-01-14T12:10:00" }' -o /dev/null # thu
curl -s -X POST "$HOST/$INDEX/article" -d '{ "t" : "13", "published" : "2010-01-14T13:10:00" }' -o /dev/null # thu
curl -s -X POST "$HOST/$INDEX/article" -d '{ "t" : "14", "published" : "2010-01-15T20:00:00" }' -o /dev/null # fri
curl -s -X POST "$HOST/$INDEX/article" -d '{ "t" : "15", "published" : "2010-01-20T20:20:00" }' -o /dev/null # wed
curl -s -X POST "$HOST/$INDEX/article" -d '{ "t" : "16", "published" : "2010-01-20T20:25:00" }' -o /dev/null # wed
curl -s -X POST "$HOST/$INDEX/article" -d '{ "t" : "17", "published" : "2010-01-20T20:30:00" }' -o /dev/null # wed
curl -s -X POST "$HOST/$INDEX/article" -d '{ "t" : "18", "published" : "2010-01-20T15:40:00" }' -o /dev/null # wed
curl -s -X POST "$HOST/$INDEX/article" -d '{ "t" : "19", "published" : "2010-01-22T22:10:00" }' -o /dev/null # fri
curl -s -X POST "$HOST/$INDEX/article" -d '{ "t" : "20", "published" : "2010-01-23T23:05:00" }' -o /dev/null # sat
curl -s -X POST "$HOST/$INDEX/_refresh" -o /dev/null
curl -s -X GET "$HOST/$INDEX/_search?search_type=count&pretty=true" -d \
'{
"query" : {
"match_all" : {}
},
"facets" : {
"by_day_of_week" : {
"terms" : {
"script" : "var days = [null, \"mon\",\"tue\",\"wed\",\"thu\",\"fri\",\"sat\",\"sun\"]; days[doc.published.date.getDayOfWeek()];",
"lang" : "js"
}
}
}
}' | ruby -rubygems -e 'require "yajl"; json = Yajl::Parser.new.parse(STDIN); puts json["facets"]["by_day_of_week"]["terms"].map { |term| %Q|#{term["term"]},#{term["count"]}| }.join("\n")'
@kimchy

This comment has been minimized.

kimchy commented Sep 26, 2011

This oen should be a bit simpler and faster:

curl -X GET "$HOST/$INDEX/_search?search_type=count&pretty=true" -d \
'{
    "query" : {
        "match_all" : {}
    },
    "facets" : {
        "by_day_of_week" : {
            "terms" : {
              "script" : "doc.published.date.getDayOfWeek()",
              "lang"   : "js"
            }
        }
    }
}'
@karmi

This comment has been minimized.

Owner

karmi commented Sep 26, 2011

Thanks!, updated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment