Skip to content

Instantly share code, notes, and snippets.

@jorendorff
Forked from anonymous/searcher.ex
Last active July 27, 2016 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jorendorff/d907981408a47e7cebe6d8c46336a1e7 to your computer and use it in GitHub Desktop.
Save jorendorff/d907981408a47e7cebe6d8c46336a1e7 to your computer and use it in GitHub Desktop.
# 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