Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing search queries in Java

Tags:

java

search

I have been trying to find an easy way to parse a search query and convert it to an SQL query for my DB.

I have found two solutions:

  1. Lucene: Powerful Java-based search engine, contains a query parser but it isn't very configurable and I could find a way to easily hack/adapt it to create SQL queries.
  2. ANTLR: A veteran text lexer-parser. Used for building anything from compilers to sky scrapers. ANTLR is highly configurable but everyone touching the code from now on will have to learn a new language...

Any other ideas?

like image 237
Barak Schiller Avatar asked Aug 17 '08 19:08

Barak Schiller


2 Answers

SQL-ORM is a very lightweight Java library which includes the ability to construct a (dynamic) SQL query in Java as a graph of objects

IMHO, this is a far better technique for building dynamic SQL queries than the usual String concatentation method.

Disclaimer: I have made some very minor contributions to this project

like image 51
Dónal Avatar answered Sep 26 '22 18:09

Dónal


What exactly do you have in mind? I've used Lucene for text-searching, but where it excels is building an index and searching that instead of hitting the database at all.

I recently set up an system where I index a table in Lucene by concatenating all the columns (separated by spaces) into one field, and popping that into Lucene, and then also adding the primary key in a separate column. Lucene does all the searching and returned a list of primary keys, which I used to pull up a populated set of results and display to the user.

Converting a search query into a SQL statement would seem to me to be a little messy.

Also, here's a great beginning tutorial explaining the basic structure of Lucene.

like image 22
pbh101 Avatar answered Sep 22 '22 18:09

pbh101