-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Query customization #1
Comments
Hey @densefog, would the following work?: alias Haystack.{Query, Tokenizer, Transformer}
# I'm assuming you already have a %Haystack{} struct and the query string
# is the `q` variable
Haystack.index(haystack, :books, fn index ->
tokens = Tokenizer.tokenize(q)
tokens = Transformer.pipeline(tokens, Transformer.default())
expressions = [
Query.Expression.new(:match, field: "category", term: "elixir"),
Enum.map(tokens, &Query.Expression.new(:match, field: "title", term: &1))
]
Query.new()
|> Query.clause(Query.Clause.expressions(Query.Clause.new(:all), expressions))
|> Query.run(index)
end) You can build your own |
Hi @philipbrown, I think that's definitely on the right track. It gave me an error with the nested I was attempting to use the data from the live-view example with something like this but must have something off since nothing is returned. I'll see if I can dig further into it. alias Haystack.{Query, Tokenizer, Transformer}
Haystack.index(App.Articles.Search.haystack(), :articles, fn index ->
tokens = Tokenizer.tokenize("giant")
tokens = Transformer.pipeline(tokens, Transformer.default())
expressions = [
Query.Expression.new(:match, field: "name", term: "panda")
] ++ Enum.map(tokens, &Query.Expression.new(:match, field: "body", term: &1))
Query.new()
|> Query.clause(Query.Clause.expressions(Query.Clause.new(:all), expressions))
|> Query.run(index)
end) I also modified |> Stream.map(&Map.take(&1, ~w{id name body}a)) |
Ahh, Yes you're right. Sorry, that was my mistake. The perils of writing code without testing it! It should be: alias Haystack.{Query, Tokenizer, Transformer}
Haystack.index(App.Articles.Search.haystack(), :articles, fn index ->
tokens = Tokenizer.tokenize("giant")
tokens = Transformer.pipeline(tokens, Transformer.default())
expressions = [
Query.Expression.new(:match, field: "name", term: "panda")
] ++ Enum.map(tokens, &Query.Expression.new(:match, field: "body", term: &1.v))
Query.new()
|> Query.clause(Query.Clause.expressions(Query.Clause.new(:all), expressions))
|> Query.run(index)
end) -] ++ Enum.map(tokens, &Query.Expression.new(:match, field: "body", term: &1))
+] ++ Enum.map(tokens, &Query.Expression.new(:match, field: "body", term: &1.v)) Also, good spot on the Let me know how you get on! |
Yes, that does appear to be working better. In my testing though I did run into some results I thought were random but it appears to be something with the Stemmer timing. For example, I was trying to search for the word "company" and it wasn't finding any results when expected. I changed the search to the stemmed word "compani" and results were found. I'm sure that's a completely separate issue from this one. Thanks! |
@densefog That's great! 🙌 Yeah, that sounds like a separate issue. Feel free to open a new issue with your findings, or any suggestions you have for improvement! |
I'm enjoying Haystack and wondering if there is a way to customize how queries are run by field.
For example, say I have a list of books in a JSON document and each entry has the book title and a book category. Is there currently a way to specify the exact 'category' and have the search run across the title? Such as, give me all the books in the 'Elixir' category that have 'ecto' in them.
I tried creating a separate index and customizing tokens but didn't find an obvious way? Thanks!
The text was updated successfully, but these errors were encountered: