How to _update a non existing field

Using ElasticSearch as a DB I'm looking for a way to _update a field that
doesn't exist. I don't want to set the field to 0 when first indexed
because I don't know the field names for other reasons. I assume I can't
use null_value in mapping either, since I don't know the field name BEFORE
I index the document, and setting the null_value afterwards I assume won't
help.

curl 'http://localhost:9200/tests/test/test1?pretty' -d ' { }'

curl 'http://localhost:9200/tests/test/test1/_update?pretty' -d '
{"script": "ctx._source.b += v", "params": {"v": 2}}'

Returns ElasticSearchIllegalArgumentException[failed to execute script];
nested: PropertyAccessException[[Error: could not access: b; in class:
java.util.LinkedHashMap]\n[Near : {... ctx._source.b+v ....}]

I tried using isdef, but that seems to always return false, I'm probably
using it wrong.

curl 'http://localhost:9200/tests/test/test1/_update?pretty' -d '
{"script": "if (isdef ctx._source.b) {ctx._source.b += v} else
{ctx._source.b = v}", "params": {"v": 2}}'

Try and check if _ctx._source.b is null or not

On Mon, Apr 16, 2012 at 4:46 PM, Andy Wick andywick@gmail.com wrote:

Using Elasticsearch as a DB I'm looking for a way to _update a field that
doesn't exist. I don't want to set the field to 0 when first indexed
because I don't know the field names for other reasons. I assume I can't
use null_value in mapping either, since I don't know the field name BEFORE
I index the document, and setting the null_value afterwards I assume won't
help.

curl 'http://localhost:9200/tests/test/test1?pretty' -d ' { }'

curl 'http://localhost:9200/tests/test/test1/_update?pretty' -d '
{"script": "ctx._source.b += v", "params": {"v": 2}}'

Returns ElasticSearchIllegalArgumentException[failed to execute script];
nested: PropertyAccessException[[Error: could not access: b; in class:
java.util.LinkedHashMap]\n[Near : {... ctx._source.b+v ....}]

I tried using isdef, but that seems to always return false, I'm probably
using it wrong.

curl 'http://localhost:9200/tests/test/test1/_update?pretty' -d '
{"script": "if (isdef ctx._source.b) {ctx._source.b += v} else
{ctx._source.b = v}", "params": {"v": 2}}'

Like this? I tried both ctx._source.b and _ctx._source.b with the same
result.

curl 'http://localhost:9200/tests/test/test1/_update?pretty' -d '
{"script": "if (ctx._source.b == null) {ctx._source.b += v} else
{ctx._source.b = v}", "params": {"v": 2}}'

{
"error" :
"RemoteTransportException[[moloches-mtc04][inet[/172.29.127.44:9300]][update]];
nested: ElasticSearchIllegalArgumentException[failed to execute script];
nested: PropertyAccessException[[Error: could not access: b; in class:
java.util.LinkedHashMap]\n[Near : {... ctx._source.b+v ....}]\n
^\n[Line: 1, Column: 1]]; ",

Heya, this seems to work:

curl 'http://localhost:9200/tests/test/test1/_update?pretty' -d '{"script":
"if (ctx._source.containsKey("b")) {ctx._source.b += v} else
{ctx._source.b = v}", "params": {"v": 2}}'

(notes, I used " to escape it the "b" key, you won't need to do it if you
don't use curl).

On Wed, Apr 18, 2012 at 3:45 AM, Andy Wick andywick@gmail.com wrote:

Like this? I tried both ctx._source.b and _ctx._source.b with the same
result.

curl 'http://localhost:9200/tests/test/test1/_update?pretty' -d '
{"script": "if (ctx._source.b == null) {ctx._source.b += v} else
{ctx._source.b = v}", "params": {"v": 2}}'

{
"error" :
"RemoteTransportException[[moloches-mtc04][inet[/172.29.127.44:9300]][update]];
nested: ElasticSearchIllegalArgumentException[failed to execute script];
nested: PropertyAccessException[[Error: could not access: b; in class:
java.util.LinkedHashMap]\n[Near : {... ctx._source.b+v ....}]\n
^\n[Line: 1, Column: 1]]; ",