Question about scripting

Hi,
I want for a test indexing data with 2 fields and after that, i want
add fields with "update".

I've try :

for($i=1; $i < 4; $i++){
if($i == 1){
$result = $es->index(
index => "toto",
type => "tata",
id => 1,
data => {
name => "big",
surname => "muzzy"
},
);
}
else{
print "tag : "; #name of the new tag
chomp($tag = );

						$result = $es->update(
							index  => "toto",
							type   => "tata",
							id     => 1,

							script => "_fields[$tag].value+=[tag]",   #Doesn't work, don't

know how it's works
params => {$tag => 'ok'}
);
}
}

My main problem comes from the script, if you see other errors you can
tell me too.
So :

I'll send an SOS to the world
I hope that someone gets my
Message in a forum

Thanks !

I think in the update operation you should use ctx._source instead of
_fields.

On Wednesday, April 18, 2012 9:20:20 AM UTC-4, Jérome wrote:

Hi,
I want for a test indexing data with 2 fields and after that, i want
add fields with "update".

I've try :

for($i=1; $i < 4; $i++){
if($i == 1){
$result =
$es->index(
index =>
"toto",
type =>
"tata",
id =>
1,
data => {
name
=> "big",
surname
=> "muzzy"
},
);
}
else{
print "tag : ";
#name of the new tag
chomp($tag =
);

                                                    $result = 

$es->update(
index =>
"toto",
type =>
"tata",
id =>
1,

                                                            script => 

"_fields[$tag].value+=[tag]", #Doesn't work, don't
know how it's works
params =>
{$tag => 'ok'}
);
}
}

My main problem comes from the script, if you see other errors you can
tell me too.
So :

I'll send an SOS to the world
I hope that someone gets my
Message in a forum

Thanks !

On Wednesday, April 18, 2012 9:20:20 AM UTC-4, Jérome wrote:

Hi,
I want for a test indexing data with 2 fields and after that, i want
add fields with "update".

I've try :

for($i=1; $i < 4; $i++){
if($i == 1){
$result =
$es->index(
index =>
"toto",
type =>
"tata",
id =>
1,
data => {
name
=> "big",
surname
=> "muzzy"
},
);
}
else{
print "tag : ";
#name of the new tag
chomp($tag =
);

                                                    $result = 

$es->update(
index =>
"toto",
type =>
"tata",
id =>
1,

                                                            script => 

"_fields[$tag].value+=[tag]", #Doesn't work, don't
know how it's works
params =>
{$tag => 'ok'}
);
}
}

My main problem comes from the script, if you see other errors you can
tell me too.
So :

I'll send an SOS to the world
I hope that someone gets my
Message in a forum

Thanks !

Oh, i'm trying but i d'ont understand what is ctx._source, mainly
"ctx".

On 19 avr, 18:56, Igor Motov imo...@gmail.com wrote:

I think in the update operation you should use ctx._source instead of
_fields.

On Wednesday, April 18, 2012 9:20:20 AM UTC-4, Jérome wrote:

Hi,
I want for a test indexing data with 2 fields and after that, i want
add fields with "update".

I've try :

for($i=1; $i < 4; $i++){
if($i == 1){
$result =
$es->index(
index =>
"toto",
type =>
"tata",
id =>
1,
data => {
name
=> "big",
surname
=> "muzzy"
},
);
}
else{
print "tag : ";
#name of the new tag
chomp($tag =
);

                                                    $result =

$es->update(
index =>
"toto",
type =>
"tata",
id =>
1,

                                                            script =>

"_fields[$tag].value+=[tag]", #Doesn't work, don't
know how it's works
params =>
{$tag => 'ok'}
);
}
}

My main problem comes from the script, if you see other errors you can
tell me too.
So :

I'll send an SOS to the world
I hope that someone gets my
Message in a forum

Thanks !

On Wednesday, April 18, 2012 9:20:20 AM UTC-4, Jérome wrote:

Hi,
I want for a test indexing data with 2 fields and after that, i want
add fields with "update".

I've try :

for($i=1; $i < 4; $i++){
if($i == 1){
$result =
$es->index(
index =>
"toto",
type =>
"tata",
id =>
1,
data => {
name
=> "big",
surname
=> "muzzy"
},
);
}
else{
print "tag : ";
#name of the new tag
chomp($tag =
);

                                                    $result =

$es->update(
index =>
"toto",
type =>
"tata",
id =>
1,

                                                            script =>

"_fields[$tag].value+=[tag]", #Doesn't work, don't
know how it's works
params =>
{$tag => 'ok'}
);
}
}

My main problem comes from the script, if you see other errors you can
tell me too.
So :

I'll send an SOS to the world
I hope that someone gets my
Message in a forum

Thanks !

ctx is just a map that currently contain one value "_source", which is
another map containing all fileds in your document parsed into a map. So,

"script" : "ctx._source["tag"] = ["foo"]"

will create a new field called tag with value "foo" and

"script" : "ctx._source["tag"] += ["bar"]"

will create add another value "bar" to the filed.

You can combine these two operations together by checking if field already
exists first:

"script" : "if(ctx._source["tag"]==null) { ctx._source["tag"] =
["foo"]} else {ctx._source["tag"] += ["foo"]}"

On Friday, April 20, 2012 4:53:54 AM UTC-4, Jérome wrote:

Oh, i'm trying but i d'ont understand what is ctx._source, mainly
"ctx".

On 19 avr, 18:56, Igor Motov imo...@gmail.com wrote:

I think in the update operation you should use ctx._source instead of
_fields.

On Wednesday, April 18, 2012 9:20:20 AM UTC-4, Jérome wrote:

Hi,
I want for a test indexing data with 2 fields and after that, i want
add fields with "update".

I've try :

for($i=1; $i < 4; $i++){
if($i == 1){
$result =
$es->index(
index
=>
"toto",
type
=>
"tata",
id
=>
1,
data
=> {

name

=> "big",

surname

=> "muzzy"
},
);
}
else{
print "tag : ";
#name of the new tag
chomp($tag =
);

                                                    $result =

$es->update(
index
=>
"toto",
type
=>
"tata",
id
=>
1,

                                                            script 

=>

"_fields[$tag].value+=[tag]", #Doesn't work, don't
know how it's works
params
=>
{$tag => 'ok'}
);
}
}

My main problem comes from the script, if you see other errors you can
tell me too.
So :

I'll send an SOS to the world
I hope that someone gets my
Message in a forum

Thanks !

On Wednesday, April 18, 2012 9:20:20 AM UTC-4, Jérome wrote:

Hi,
I want for a test indexing data with 2 fields and after that, i want
add fields with "update".

I've try :

for($i=1; $i < 4; $i++){
if($i == 1){
$result =
$es->index(
index
=>
"toto",
type
=>
"tata",
id
=>
1,
data
=> {

name

=> "big",

surname

=> "muzzy"
},
);
}
else{
print "tag : ";
#name of the new tag
chomp($tag =
);

                                                    $result =

$es->update(
index
=>
"toto",
type
=>
"tata",
id
=>
1,

                                                            script 

=>

"_fields[$tag].value+=[tag]", #Doesn't work, don't
know how it's works
params
=>
{$tag => 'ok'}
);
}
}

My main problem comes from the script, if you see other errors you can
tell me too.
So :

I'll send an SOS to the world
I hope that someone gets my
Message in a forum

Thanks !

1 Like

Hi !
First, thanks for explain Igor.

I've try the ctx._source but doesn't works.

I show you the entire script (little script just to test update
function)


Script


#!/usr/bin/perl

use strict;
use warnings;
use ElasticSearch;

				my $es = ElasticSearch->new(
									        transport    =>  'http',

servers => 'localhost:9200',
trace_calls => 1,
timeout => 30,
max_requests => 10_000,

									        no_refresh   => 0
										);



				my ($result, $tag);
				my $i;



				for($i=1; $i < 5; $i++){
					if($i == 1){
						$result = $es->index(
							index => "toto",
							type  => "tata",
							id    => 1,
							data  => {
								name  => "big",
								surname => "muzzy"
							},
						);
					}
					else{
						print "tag : ";
						chomp($tag = <STDIN>);

						$result = $es->update(
							index  => "toto",
							type   => "tata",
							id     => 1,

							script => {"ctx._source[\"$tag\"].value=["foo"]"},
                                                            params

=> {$tag => 'ok'}
);
}
}


Error message


Odd number of elements in anonymous hash at ./testupdate.pl line 42,
line 1.

[Mon Apr 23 14:49:55 2012] Protocol: http, Server:

193.51.160.124:9200
curl -XPOST 'http://127.0.0.1:9200/toto/tata/1/_update?pretty=1' -d '
{
"params" : {
"plop" : "ok"
},
"script" : {
"ctx._source["plop"].value=["tags"]" : null
}
}
'

[Mon Apr 23 14:49:55 2012] Response:

{

"status" : 400,

"error" : "ElasticSearchIllegalArgumentException[failed to exe

> cute script]; nested: CompileException[[Error: unterminated

> collection element]\n[Near : {... {ctx._source["plop"].val

> ue=["tags"]=null} ....}]\n

> ^\n[Line: 1, Column: 36]]; "

}

[ERROR] ** ElasticSearch::Error::Request at /Library/Perl/5.10.0/
ElasticSearch/Transport/HTTP.pm line 60 :
ElasticSearchIllegalArgumentException[failed to execute script];
nested: CompileException[[Error: unterminated collection element]
[Near : {... {ctx._source["plop"].value=["tags"]=null} ....}]
^
[Line: 1, Column: 36]];

With vars:{
'request' => {
'post_process' => undef,
'qs' => {},
'cmd' => '/toto/tata/1/_update',
'as_json' => undef,
'data' => {
'params' => {
'plop' => 'ok'
},
'script' => {
'ctx._source["plop"].value=["tags"]' => undef
}
},
'method' => 'POST'
},
'status_code' => 400,
'server' => '193.51.160.124:9200',
'status_msg' => 'Bad Request'
}

I don't understand the problem...

Jerome

I think the problem is caused by { } around script text. It should be

script => "ctx._source["$tag"].value=["foo"]"

On Monday, April 23, 2012 8:53:38 AM UTC-4, Jérome wrote:

Hi !
First, thanks for explain Igor.

I've try the ctx._source but doesn't works.

I show you the entire script (little script just to test update
function)


Script


#!/usr/bin/perl

use strict;
use warnings;
use Elasticsearch;

                                    my $es = ElasticSearch->new( 
                                                                             
   transport    =>  'http', 

servers => 'localhost:9200',

   trace_calls  => 1, 
                                                                             
   timeout      => 30, 
                                                                             
   max_requests => 10_000, 


                                                                             
   no_refresh   => 0 
                                                                                    ); 




                                    my ($result, $tag); 
                                    my $i; 



                                    for($i=1; $i < 5; $i++){ 
                                            if($i == 1){ 
                                                    $result = 

$es->index(
index =>
"toto",
type =>
"tata",
id =>
1,
data => {
name
=> "big",
surname
=> "muzzy"
},
);
}
else{
print "tag : ";
chomp($tag =
);

                                                    $result = 

$es->update(
index =>
"toto",
type =>
"tata",
id =>
1,

                                                            script => 

{"ctx._source["$tag"].value=["foo"]"},
params
=> {$tag => 'ok'}
);
}
}


Error message


Odd number of elements in anonymous hash at ./testupdate.pl line 42,
line 1.

[Mon Apr 23 14:49:55 2012] Protocol: http, Server:

193.51.160.124:9200
curl -XPOST 'http://127.0.0.1:9200/toto/tata/1/_update?pretty=1' -d '
{
"params" : {
"plop" : "ok"
},
"script" : {
"ctx._source["plop"].value=["tags"]" : null
}
}
'

[Mon Apr 23 14:49:55 2012] Response:

{

"status" : 400,

"error" : "ElasticSearchIllegalArgumentException[failed to exe

> cute script]; nested: CompileException[[Error: unterminated

> collection element]\n[Near : {... {ctx._source["plop"].val

> ue=["tags"]=null} ....}]\n

> ^\n[Line: 1, Column: 36]]; "

}

[ERROR] ** Elasticsearch::Error::Request at /Library/Perl/5.10.0/
Elasticsearch/Transport/HTTP.pm line 60 :
ElasticSearchIllegalArgumentException[failed to execute script];
nested: CompileException[[Error: unterminated collection element]
[Near : {... {ctx._source["plop"].value=["tags"]=null} ....}]
^
[Line: 1, Column: 36]];

With vars:{
'request' => {
'post_process' => undef,
'qs' => {},
'cmd' => '/toto/tata/1/_update',
'as_json' => undef,
'data' => {
'params' => {
'plop' => 'ok'
},
'script' => {
'ctx._source["plop"].value=["tags"]' => undef
}
},
'method' => 'POST'
},
'status_code' => 400,
'server' => '193.51.160.124:9200',
'status_msg' => 'Bad Request'
}

I don't understand the problem...

Jerome

Yes it works !
Really really thank you !

Jerome

On 23 avr, 15:04, Igor Motov imo...@gmail.com wrote:

I think the problem is caused by { } around script text. It should be

script => "ctx._source["$tag"].value=["foo"]"

On Monday, April 23, 2012 8:53:38 AM UTC-4, Jérome wrote:

Hi !
First, thanks for explain Igor.

I've try the ctx._source but doesn't works.

I show you the entire script (little script just to test update
function)


Script


#!/usr/bin/perl

use strict;
use warnings;
use Elasticsearch;

                                    my $es = ElasticSearch->new(
   transport    =>  'http',

servers => 'localhost:9200',

   trace_calls  => 1,
   timeout      => 30,
   max_requests => 10_000,
   no_refresh   => 0
                                                                                    );
                                    my ($result, $tag);
                                    my $i;
                                    for($i=1; $i < 5; $i++){
                                            if($i == 1){
                                                    $result =

$es->index(
index =>
"toto",
type =>
"tata",
id =>
1,
data => {
name
=> "big",
surname
=> "muzzy"
},
);
}
else{
print "tag : ";
chomp($tag =
);

                                                    $result =

$es->update(
index =>
"toto",
type =>
"tata",
id =>
1,

                                                            script =>

{"ctx._source["$tag"].value=["foo"]"},
params
=> {$tag => 'ok'}
);
}
}


Error message


Odd number of elements in anonymous hash at ./testupdate.pl line 42,
line 1.

[Mon Apr 23 14:49:55 2012] Protocol: http, Server:

193.51.160.124:9200
curl -XPOST 'http://127.0.0.1:9200/toto/tata/1/_update?pretty=1' -d '
{
"params" : {
"plop" : "ok"
},
"script" : {
"ctx._source["plop"].value=["tags"]" : null
}
}
'

[Mon Apr 23 14:49:55 2012] Response:

{

"status" : 400,

"error" : "ElasticSearchIllegalArgumentException[failed to exe

> cute script]; nested: CompileException[[Error: unterminated

> collection element]\n[Near : {... {ctx._source["plop"].val

> ue=["tags"]=null} ....}]\n

> ^\n[Line: 1, Column: 36]]; "

}

[ERROR] ** Elasticsearch::Error::Request at /Library/Perl/5.10.0/
Elasticsearch/Transport/HTTP.pm line 60 :
ElasticSearchIllegalArgumentException[failed to execute script];
nested: CompileException[[Error: unterminated collection element]
[Near : {... {ctx._source["plop"].value=["tags"]=null} ....}]
^
[Line: 1, Column: 36]];

With vars:{
'request' => {
'post_process' => undef,
'qs' => {},
'cmd' => '/toto/tata/1/_update',
'as_json' => undef,
'data' => {
'params' => {
'plop' => 'ok'
},
'script' => {
'ctx._source["plop"].value=["tags"]' => undef
}
},
'method' => 'POST'
},
'status_code' => 400,
'server' => '193.51.160.124:9200',
'status_msg' => 'Bad Request'
}

I don't understand the problem...

Jerome

SOLVED