3

In my web app, I want to allow my user to search for specific articles.

I need to support the following operators:

AND
OR
NOT
( )
" "

A search query could be something like:

aaa AND bbb AND (ccc or ddd) NOT "eee fff"

If possible, wildcards like * and ? should be supported too.

If possible, I would love to end up with a Linq expression, that will allow me to query EF, ElasticLINQ or Linq2SQL.

Do you know of any projects that allow me to accomplish this in a somewhat easy way?

I have been looking at Irony and Antlr, but they seem like overkill for this.

Any ideas?

EDIT:
If you have any suggestions as to how I could program this myself, I would be very interested too. But someone must have been in this position before.

Søren Lorentzen
  • 868
  • 1
  • 11
  • 26
  • 1
    Do the user have to write `AND`, `OR`, ...? Are they programmers? Are you not overengineering a little? You realize you would also need to do some syntax check(`AND AND AND AND(OR OR OR) NOT NOT NOT`) Dont see any _simple_ solution for this. If you really need to make such search functionality try to make it without the need for user to WRITE anything(especially if he is not a programmer) Make him click, select, drag, drop, but NOT write search queries. – Milan Halada May 05 '14 at 12:55
  • Thanks for your comment. The person entering the query is the administrator of the website and is familiar with the syntax. – Søren Lorentzen May 05 '14 at 12:57
  • I agree that this is almost certainly over-engineering. Is the purpose of this to find articles using syntax that the advanced find does not support, or are you trying to give a non-CRM user access to the articles? – Zach Mast May 05 '14 at 16:52

3 Answers3

1

I have recently implemented a similar parser myself, and it was rather tough task. I had to define the grammar, implement lexical and syntactic analysis, and also expression evaluation logic. Here it is: see Vb.Net code on Gist.

I do not provide much details here, as, IMHO, it is not the best way to go for you, unless you have experience of writing such code.

At the same time, I never heard of any tools that allowed to automate such syntactic and lexical analysis and were easy to use.

I know there were Lex, Yacc and Bison, but these all are for C++ and they are imho quite complex. Never found anything interesting for C#.

Hence, I would suggest you to consider it once again, whether you really need to implement it this way.

UPD: I found a nice SO question which could be useful for you, should you try to implement your own parser: How to write a Parser in C#?

Community
  • 1
  • 1
Rustam
  • 1,766
  • 18
  • 34
1

Solutions:

  1. use your own DSL (take a look @ http://www.antlr.org/)

  2. call a odata web service / wcf data service to use standarized queries see example @ http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/calling-an-odata-service-from-a-net-client

road242
  • 2,492
  • 22
  • 30
1

What you can do is handle this the way Microsoft Dynamics CRM handles this in their advanced find: instead of having the user enter their own search string manually, they have a graphical UI which gets turned into the proper string when you search for it.

enter image description here

I see that you are making this for someone familiar with the syntax. Even if the person knows how the query works, I still would go for this method:

  1. It's far easier to determine how a query works from a visual representation than from a text.
  2. It's easier to work with, even if you have experience with the syntax.
  3. Syntax Errors are harder to make, because the syntax is automatically generated.
Nzall
  • 3,439
  • 5
  • 29
  • 59