Can I use comments in JSON request to ES?


#1

I created a quite complicated request to ES (I send it via curl), so I wanted to add a bit of documentation to it. I read that JSON does not allow comments, but I put a comment of type /* ... */ inside the request and it succeeded. Can I assume that ES allows such comments inside JSON request?

I'm currently using ES 2.4.2, but I'm more interested if current and future versions will allow comments.

Or maybe I can send requests in other languages, for example YAML, that allows comments?

I'm aware that I can use some tool to remove comments before I send it to ES, but I wanted to get sure if I really need to do this.

My request looks like this (it does not make much sense because you can use avg metric instead).

{
	"query" : 
	{
		"match_all" : {}
	},
	"aggregations": 
	{
		"avg_score": 
		{
			"scripted_metric": 
			{
				"init_script" : 
				/* comment */
					"_agg['scores'] = []; _agg['count'] = 0",
				"map_script" : 
					"_agg.scores.add(doc['score'].value); _agg.count += 1",
				"combine_script" : 
					"sum = 0; for (s in _agg.scores) { sum += s }; return [sum, _agg.count]",
				"reduce_script" : 
					"avg_sum = 0; avg_count = 0; for (a in _aggs) { avg_sum += a[0]; avg_count += a[1]; }; return avg_sum / avg_count"
			}
		}
	}
}

(Colin Goodheart-Smithe) #2

I would not assume that Elasticsearch allows comments inside JSON requests. As you mentioned, the JSON spec does not allow comments and I am surprised that it worked in your test to be honest. Its not something that is tested and not something we explicitly support so it could stop working at any time.


#3

Thank you for the quick answer and the explanation.


(system) #4

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.