-
-
Save jorendorff/d907981408a47e7cebe6d8c46336a1e7 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
# To run this code, you'll need the sample data (133 MB download, unzips to 492 MB): | |
# http://bit.ly/2avfASU | |
# tar xjf sample.tar.bz2 | |
defmodule Elindex.Searcher do | |
def search(word) do | |
File.ls!("sample") | |
|> Stream.map(fn(filename) -> | |
fn -> | |
Path.join("sample", filename) | |
|> File.read! | |
|> hit_info(word) | |
end | |
end) | |
|> Stream.map(&Task.async/1) | |
|> Stream.map(&Task.await/1) | |
|> Stream.filter(fn({count, _}) -> count > 0 end) | |
|> Enum.sort(&>=/2) | |
|> Enum.take(10) | |
end | |
def hit_info(blob, word) do | |
count = Enum.count(Regex.scan(~r(\b#{word}\b), blob)) | |
title = String.split(blob, "\n") |> List.first | |
{count, title} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment