blogit v1.2.3 Blogit.Logic.Search View Source
Contains a function for filtering and searching posts.
Lists of Blogit.Models.Post
structures can be filtered by 'author',
'category', 'tags', 'year' and 'month'.
The content and the title of lists of Blogit.Models.Post
s can be searched
using a query.
Link to this section Summary
Functions
Filters a list of Blogit.Models.Post
structs using a map of meta fields
to filter by and/or search query to match to their contents and/or titles.
Link to this section Types
Link to this section Functions
Link to this function
filter_by_params(posts, params)
View Sourcefilter_by_params(posts(), %{optional(String.t()) => search_value()}) :: posts()
Filters a list of Blogit.Models.Post
structs using a map of meta fields
to filter by and/or search query to match to their contents and/or titles.
The params
parameter supports zero or more of the following keys:
- "author" - Used to filter
Blogit.Models.Post
structs by their.meta.author
field. - "category" - Used to filter
Blogit.Models.Post
structs by their.meta.category
field. - "tags" - Used to filter
Blogit.Models.Post
structs by their.meta.tags
field. The value for this key should a string of comma separated tags. - "year" - Used to filter
Blogit.Models.Post
by their.meta.year
field. - "month" - Used to filter
Blogit.Models.Post
by their.meta.month
field. - "q" - A query to filter
Blogit.Models.Post
structs by their content or title. Support text in double quotes in order to search for phrases.
If the map contains other keys, they'll be ignored.
Examples
iex> alias Blogit.Models.Post.Meta
iex> posts = [
...> %Blogit.Models.Post{
...> name: "one", raw: "", html: "",
...> meta: %Meta{author: "meddle", category: "Primary"}
...> },
...> %Blogit.Models.Post{
...> name: "two", raw: "", html: "",
...> meta: %Meta{author: "valo", category: "Secondary"}
...> },
...> %Blogit.Models.Post{
...> name: "three", raw: "", html: "",
...> meta: %Meta{author: "Reductions", category: "Other"}
...> },
...> %Blogit.Models.Post{
...> name: "four", raw: "", html: "",
...> meta: %Meta{author: "meddle", category: "Other"}
...> }
...> ]
iex> filter = %{"author" => "meddle"}
iex> filtered = Blogit.Logic.Search.filter_by_params(posts, filter)
iex> filtered |> Enum.map(fn (post) -> post.name end)
["one", "four"]
iex> filter = %{"category" => "Other"}
iex> filtered = Blogit.Logic.Search.filter_by_params(posts, filter)
iex> filtered |> Enum.map(fn (post) -> post.name end)
["three", "four"]
iex> alias Blogit.Models.Post.Meta
iex> posts = [
...> %Blogit.Models.Post{
...> name: "one", raw: "", html: "",
...> meta: %Meta{tags: ["one", "едно", "super"]}
...> },
...> %Blogit.Models.Post{
...> name: "two", raw: "", html: "",
...> meta: %Meta{tags: ["two", "две", "super"]}
...> }
...> ]
iex> filter = %{"tags" => "super, one"}
iex> filtered = Blogit.Logic.Search.filter_by_params(posts, filter)
iex> filtered |> Enum.map(fn (post) -> post.name end)
["one"]
iex> alias Blogit.Models.Post.Meta
iex> posts = [
...> %Blogit.Models.Post{
...> name: "one", raw: "", html: "",
...> meta: %Meta{year: "2017", month: "4"}
...> },
...> %Blogit.Models.Post{
...> name: "two", raw: "", html: "",
...> meta: %Meta{year: "2016", month: "4"}
...> },
...> %Blogit.Models.Post{
...> name: "three", raw: "", html: "",
...> meta: %Meta{year: "2017", month: "3"}
...> }
...> ]
iex> filter = %{"year" => "2017"}
iex> filtered = Blogit.Logic.Search.filter_by_params(posts, filter)
iex> filtered |> Enum.map(fn (post) -> post.name end)
["one", "three"]
iex> filter = %{"month" => "4"}
iex> filtered = Blogit.Logic.Search.filter_by_params(posts, filter)
iex> filtered |> Enum.map(fn (post) -> post.name end)
["one", "two"]
iex> filter = %{"month" => "4", "year" => "2017"}
iex> filtered = Blogit.Logic.Search.filter_by_params(posts, filter)
iex> filtered |> Enum.map(fn (post) -> post.name end)
["one"]
iex> alias Blogit.Models.Post.Meta
iex> posts = [
...> %Blogit.Models.Post{
...> name: "one", raw: "Something about Processes - stuff", html: "",
...> meta: %Meta{author: "meddle", title: "Processes"}
...> },
...> %Blogit.Models.Post{
...> name: "two", raw: "Modules contain functions!", html: "",
...> meta: %Meta{author: "valo", title: "Modules And Functions"}
...> },
...> %Blogit.Models.Post{
...> name: "three", raw: "Mix is one of the coolest things", html: "",
...> meta: %Meta{author: "Reductions", title: "Mix And Tests"}
...> },
...> %Blogit.Models.Post{
...> name: "four", raw: "Linked things make difference", html: "",
...> meta: %Meta{author: "meddle", title: "Processes And Links"}
...> }
...> ]
iex> filter = %{"q" => "Process"}
iex> filtered = Blogit.Logic.Search.filter_by_params(posts, filter)
iex> filtered |> Enum.map(fn (post) -> post.name end)
["one", "four"]
iex> filter = %{"q" => "Tests"}
iex> filtered = Blogit.Logic.Search.filter_by_params(posts, filter)
iex> filtered |> Enum.map(fn (post) -> post.name end)
["three"]
iex> filter = %{"q" => ~s("coolest things")}
iex> filtered = Blogit.Logic.Search.filter_by_params(posts, filter)
iex> filtered |> Enum.map(fn (post) -> post.name end)
["three"]
iex> filter = %{"q" => ~s("coolest things"), "author" => "meddle"}
iex> filtered = Blogit.Logic.Search.filter_by_params(posts, filter)
iex> filtered |> Enum.map(fn (post) -> post.name end)
[]