Need to support the custom query DSL for Domain objects, so that user can create various queries in SQL like format for NoSQL database.
e.g. Select * from User where firstName = “vinayak” and age > 18 order by id limit 10.
The result will be the same irrespective of underlying storage either Elastic Search, MongoDB or MySql.
The solution to this problem is to create custom query language by providing SQL like syntax or grammar. The query defined using this grammar will be translated to the provided data source query language like elastic search query dsl. The tool evaluated for this purpose is ANTLR which is widely used for designing the custom domain specific languages. I highly appreciate the work done by Terence Parr in this area.
The language translation for target data source using ANTLR works in three different Steps.
To translate the parse tree into desired output or targeted data source query ANTLR also provided the parse tree traversal using Depth First Search. The Step I by default generate the listener interface and empty base implementation for this interface. The Step I also optionally generate the visitor for the parse tree. The listener methods will be called during parse tree traversal, in this callback methods one can perform respective actions as per the need.
This tool will help you to design your own custom language and its translation as per your need in very simple way. For more information one can explore the documentation and The Definitive ANTLR 4 Reference book.