Open MT-Jacobs opened 2 years ago
Per https://github.com/elastic/elasticsearch-java/blob/main/CONTRIBUTING.md#project-structure:
The
co.elastic.clients.elasticsearch
package and its children are all entirely generated, and the generator is not part of this repository. Because of this, PRs will not work for this part of the code. If you want to suggest changes to the generated code, open an issue describing how the code should look like, so that we can discuss on updating the generator.
The code I want a DSL for lives in that package, and I'm assuming the generator is closed source(?) so this doesn't appear to be something I can build myself.
If someone knows where the generator is though, I'd love to take a look at it.
Function literals with receiver make many parts of the DSL simple and elegant to construct:
fun SearchRequest.Builder.queryKt(commands: Query.Builder.() -> Unit): SearchRequest.Builder =
query(Query.Builder().apply(commands).build())
fun Query.Builder.boolKt(commands: BoolQuery.Builder.() -> Unit): ObjectBuilder<Query> =
bool(BoolQuery.Builder().apply(commands).build())
The problem is when you get to portions of functionality that use lists of maps such as SearchRequest.Builder#aggregations
and BoolQuery.Builder#filter
. That will require more complex code.
Again, I'm happy to help make this happen - it would make it way easier for my team to dive deep and iterate on Elasticsearch-driven code - I'm just not sure where to start with regards to the generator.
For what it's worth I still would very much be interested in contributing to something like this.
🚀 Feature Proposal
The Elasticsearch Specification could be used to generate a Kotlin DSL that can be used with the Elasticsearch Java client's builders.
Alternatively, one could do the same thing directly in Kotlin using fully Kotlin-based builders.
Motivation
I'm writing a lot of Elasticsearch client code in Kotlin and was getting tired of how hard it was to read with complex queries, even with the builder -
Kotlin is great at this kind of thing, so I gave a try at manually implementing it and started to wonder if it could be automated. The easier it is to describe a query in Elasticsearch, the more I can do with the technology.
Example
The DSL could vastly improve ease of use in Kotlin projects. Here's one sample of some easy to work with DSL code that I successfully implemented as a proof of concept:
We can use an inline reified function for searches to 1) avoid having to indicate the class twice and 2) invoke the SearchKt object and build our
SearchRequest
.All one needs to do to make the above functionality possible is for to auto-generate various Kotlin builders on top of the existing Java-based ones (or, alternatively, set them up without Java at all) - something that might look like this: