complex objects in body templates?

70 views
Skip to first unread message

Alex

unread,
Dec 2, 2011, 5:18:34 PM12/2/11
to ql.io
Very cool looking project. I'm monkeying around with ql.io to create
some ElasticSearch tables.

Is it possible to pass a rich parameter structure into a body
template's rendering context?

See the "Non empty lists" section in the mustache docs for an example
of what I'm trying to do. http://mustache.github.com/mustache.5.html

Thanks.

Alex

Subbu Allamaraju

unread,
Dec 2, 2011, 5:32:56 PM12/2/11
to ql.io
Hi Alex,

Can you give a rough skeleton of the structure that you want to pass?
I think there is quick way, but I want to understand the use case
before I suggest it.

Subbu

Alex Wolfe

unread,
Dec 2, 2011, 5:40:37 PM12/2/11
to ql...@googlegroups.com
Sure.

Basically, I want to pass ElasticSearch query parameters into the template. Take the term query for example:
http://www.elasticsearch.org/guide/reference/query-dsl/term-query.html

I'd like to pass the "term" parameter is as { "user" : "kimchy" } or { "user" : { "value" : "kimchy", "boost" : 2.0 }. The same applies to a bunch of the other parameters that can be provided in an ElasticSearch query doc, such as "prefix", "text" and especially "range".

I'd prefer not to flatten these parameters out, if I can help it. Let me know if that doesn't make sense, or if there's a better way.

Alex

Subbu Allamaraju

unread,
Dec 4, 2011, 11:37:45 PM12/4/11
to ql...@googlegroups.com
select * from foo where user = kimchy

can be mapped to template

{
{{#params}}
"term": { "user": "{{user}}" }
{{/params}}
}

A snippet like

> { "user" : { "value" : "kimchy", "boost" : 2.0 }

can also be mapped similarly.

Mustache does have some limitations as it requires the data to be shaped to fit the template. If the data is not shaped, an alternative is to write a UDF to shape the data. For this, add a monkey patch (http://ql.io/docs/monkey-patch) that exports a function like the one below.

exports['udf'] = {
'user' : function(arg) {
return {
...some json here...
};
}
}

This can be triggered with

select * from foo where user('kimchy').

Even better alternative would be to support an alternative to Mustache like EJS. Thoughts?

Subbu

Alex

unread,
Dec 6, 2011, 2:21:13 PM12/6/11
to ql...@googlegroups.com
Gotcha. It seems to me that having more robust templating would only be a good thing. Though, I'm not really using ql.io for anything "real" so I couldn't make a better case for it than that. :)

Subbu Allamaraju

unread,
Dec 6, 2011, 3:14:21 PM12/6/11
to ql...@googlegroups.com

On Dec 6, 2011, at 11:21 AM, Alex wrote:

> Gotcha. It seems to me that having more robust templating would only be a good thing. Though, I'm not really using ql.io for anything "real" so I couldn't make a better case for it than that. :)

That's true - https://github.com/ql-io/ql.io/issues/130

Subbu

NIkhil

unread,
Nov 26, 2012, 12:54:35 AM11/26/12
to ql...@googlegroups.com
Hi,

  I have to pass a complex json object to a post method. Creating a mustache template of the json has consumed quite a time and it is still not done.
  Is there any other alternative to mustache, can't we pass the json directly to the create table statement in ql.io ?

Sample json;
{
"a":
   "c" : "c",
   "d"  : [ 1, 2, 3 ],
   "e" : {
        "user" : "user1",
        "token": "token"
        }
}

One more complication while creating a mustache in my case is that the key - value pairs under "a" key are optional.

Any help is appreciated.

Thanks,
Nikhil

shimonchayim

unread,
Nov 27, 2012, 12:52:11 AM11/27/12
to ql...@googlegroups.com
Nikil, you can use ejs template if mustache is tricky.

Reply all
Reply to author
Forward
0 new messages