Representing the ElasticSearch query DSL in json-schema

1,547 views
Skip to first unread message

Adrian Rossouw

unread,
May 13, 2011, 12:13:08 PM5/13/11
to JSON Schema
Recently I have been making a lot of use of ElasticSearch (http://
elasticsearch.org) which is a very nice REST layer on top of Lucene.
In the interest of learning more about json-schema, and working more
concretely with it, I decided to try and build a json-schema of the
query DSL : http://www.elasticsearch.org/guide/reference/query-dsl/

One of the things that I found tricky was the lack of context a lot of
the examples have, because the components of the object can be re-used
in different contexts. The result of this experiment has lead me to
this point : https://gist.github.com/8887766ca0e7052814b0

As an example the basic query looks as follows :

{
query {
match_all: {}
}
}

Where match_all could be replaced with any of the query objects (the
full list on the guide). Now the tricky part here, is that there
may be only one 'command' inside the query object. Which means the
following should be invalid :


{
query {
match_all: {},
term: { fieldname: 'searchterm' }
}
}

I think I managed to represent that here : https://gist.github.com/8887766ca0e7052814b0#L143
, whereby it will only match to one of the items,
but it still feels a bit obtuse.

What really throws me however is the fact that the api requires the
use of a property name representing the field on the indexed object to
search
for. The term query above is an ideal example :
http://www.elasticsearch.org/guide/reference/query-dsl/term-query.html

It is one of the possible queries that can be run (of which there can
only be one), and it may only have a single property with a freeform
key, that
may be a string, or one of two different object representations.

The interaction between patternProperties and additionalProperties
alone confuses me, but I am not sure how to represent the limitation
of only
having a single property. Any help would be appreciated.


Gary Court

unread,
May 13, 2011, 7:20:02 PM5/13/11
to json-...@googlegroups.com
As of the latest published version (revision 3) of JSON Schema, there
is no way to limit the number of unknown properties on an object. As
you make a good use case for it, I'll mark this as a required feature
for the next JSON Schema revision.

-Gary

> --
> You received this message because you are subscribed to the Google Groups "JSON Schema" group.
> To post to this group, send email to json-...@googlegroups.com.
> To unsubscribe from this group, send email to json-schema...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/json-schema?hl=en.
>
>

Adrian Rossouw

unread,
May 17, 2011, 10:43:26 AM5/17/11
to JSON Schema
For my purposes, it would work to have additionalProperties possibly
be a positive integer, and
still enforce patternProperties up to a certain amount.

so :

{
type: 'object',
patternProperties: {
'.*' : { type: 'object' }
},
additionalProperties: 1
}

It's probably not the general case though.

Gary Court

unread,
May 18, 2011, 4:27:01 PM5/18/11
to json-...@googlegroups.com
That wouldn't work if you intended to use additionalProperties to
define the schema for all your properties, but still wanted to limit
the number of them. I think a better suggestion would be to add a
"minProperties" & "maxProperties" attribute to the specification
(behaves like "minItems" / "maxItems").

-Gary

On Tue, May 17, 2011 at 8:43 AM, Adrian Rossouw

Reply all
Reply to author
Forward
0 new messages