elvanja
Need to build a “natural” language search query parser, for later conversion to appropriate Elasticsearch query.
Basically need something like https://github.com/Financial-Times/n-search-parser, which can do:
- Conjunction operators:
AND,OR,NOT - Quoted phrases:
"Modesty Blase" - Grouping with parentheses:
("Modesty Blase" OR "Willie Garvin")
I’m trying to decide which approach to take, e.g.:
- GitHub - dashbitco/nimble_parsec: A simple and fast library for text-based parser combinators · GitHub
- GitHub - OvermindDL1/ex_spirit · GitHub
- GitHub - bitwalker/combine: A parser combinator library for Elixir projects · GitHub
- Leex and Yecc
As per Best way to build a parser - #3 by david_ex it would seem that Nimble Parsec is the best candidate. There is even Pegasus — pegasus v1.0.0 which could generate Nimble Parsec parsers (can’t find any boolean search PEG definitions out there though!).
But, as far as I can tell, the Leex and Yecc solution would be more concise.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex











Showing Posts 11 to 20- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kip
You can get quite a long way just using the Elixir lexer. Especially now that in Elixir 1.16 its lexing errors are better for user experience.
Example
You would then need a parser to ensure you have a valid expression for your query language but I think thats really quite easy compared to the lexical part given your reasonably straight forward requirements.
elvanja
Hmmm, interesting idea! Unfortunately, it does not solve certain edge cases out of the box, e.g.:
Didn’t have time to inspect further since I ended up with a solution that just scans the input, see next answers.
elvanja
This idea to just implement binary pattern matching got me thinking. I couldn’t find a way to actually do that. But, during the study of the issue, decided to go with simple scan approach. E.g. just go over each character, split on spaces and count parenthesis and quotes. I have the working solution at Boolean query lexer (not AST unfortunately) · GitHub if you care to take a look. Not simple though and likely hard to debug too. But this bought time so I can try and implement this via some other mechanism.
elvanja
Good point about silently ignoring grammar errors! Haven’t considered that one and it does make sense. E.g. given search for
Modesty Blase OR Willie Garvin ANDit makes sense to drop the lastANDsince it does not serve any purpose.Would love to formalize requirements but unfortunately this will go the “let’s see how it works and then we’ll improve” way. Not that this is bad, just that it does not yield to formalization well
So for the time being I used a plain approach, scanning the input. See the gist link in reply above. This solution does that, ignores certain input errors (drops tailing operators, uses only the last operator if more than one are supplied, …). It does not say at which character e.g. unclosed parenthesis started, but it should be fairly simple to add that too (it is scanning so counting should not be an issue).
I do plan to try some/all of those solutions and see if I can get a more maintainable solution after all. Hope it will be possible to also ignore certain “invalid” input and have nice enough error reporting.
Thanks for the idea!
Adzz
That’s because the AND is uppercased, might be a simple fix with a String.replace ?
elvanja
Yep, replacing would work for operators. Mind you, this is user input so we could expect also things like
aNd,Andetc. and need to support another language too. But it could be done.Unfortunately, apart from quoted terms, it needs to support non-quoted multi-word ones, e.g.:
This case breaks it because those terms are indeed user input and not part of any dictionary, so have to take it in and parse as-is (no transformation/downcasing).
akash-akya
If strings can be unquoted then it will become ambiguous grammer right? For example how should
FOO AND BARbe parsed? It can be both a boolean expression and a single multi word string.th3mus1cman
@elvanja You can take a look at what I have done here predicated/lib/predicated/query/parser.ex at main · themusicman/predicated · GitHub.
Here are some examples in the limited tests: predicated/test/predicated/query/parser_test.exs at main · themusicman/predicated · GitHub
I am stuck at the moment on supporting parans and nesting.
th3mus1cman
Well, I had a break through last night and added support for grouping and nested grouping. Might be some bugs in there but I think it generally works.
https://github.com/themusicman/predicated/commit/cca03c4e15206cdb7d1bf409f4d9c322b8106c29#diff-2291a21e493bd663ef36b5e16bfdf8a6a09ae25a2fa5d26dbed6a970969db52cR1
elvanja
Well, we can make some assumptions here, I think. E.g. if user would enter “SAP BW OR Java” in search engine I would assume the user wanted someone with “SAP BW” OR “Java”. The way I decided to interpret unquoted searches basically boils down to considering everything between boolean operators (AND, OR, NOT, AND NOT, …) to be a “keyword” term which can be a single or multi word in nature. One needs to be careful with quoted terms of course (e.g. to take
"Bang AND Olufsen"- quoted - as is).