I need to perform a calculation on a field of a specific document. As an example, I need to sum 50 to a price. I have tried the following options:
curl -X POST "localhost:9200/ex1/ex2/WPatZHgBEd7rI-6ZwNFC/_update?pretty" -H 'Content-Type: application/json' -d'{"doc": {"price": +50}}'
In this case it sets the price as 50. and if I try this:
curl -X POST "localhost:9200/ex1/ex2/WPatZHgBEd7rI-6ZwNFC/_update?pretty" -H 'Content-Type: application/json' -d'{"doc": {"price": "price"+50}}'
it gives the following error:
{
"error" : {
"root_cause" : [
{
"type" : "json_parse_exception",
"reason" : "Unexpected character ('-' (code 45)): was expecting comma to separate Object entries\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput#4c7cecda; line: 1, column: 29]"
}
],
"type" : "json_parse_exception",
"reason" : "Unexpected character ('-' (code 45)): was expecting comma to separate Object entries\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput#4c7cecda; line: 1, column: 29]"
},
"status" : 500
}
Use a script to increment a doc's attribute:
POST localhost:9200/ex1/ex2/WPatZHgBEd7rI-6ZwNFC/_update?pretty
{
"script": {
"source": "ctx._source.price += params.increment_by",
"params": {
"increment_by": 50
}
}
}
With cURL:
curl -XPOST "http://localhost:9200/localhost:9200/ex1/ex2/WPatZHgBEd7rI-6ZwNFC/_update?pretty" -H 'Content-Type: application/json' -d'{ "script": { "source": "ctx._source.price += params.increment_by", "params": { "increment_by": 50 } }}'
Related
Elasticsearch version: 8.3.3
Indexing was performed using the following Elasticsearch API.
curl -X POST "localhost:9200/bulk_meta/_doc/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"index": { "_id": "1"}}
{"mydoc": "index action, id 1 "}
{"index": {}}
{"mydoc": "index action, id 2"}
'
In this case, the following error occurred.
{
"error" : {
"root_cause" : [
{
"type" : "mapper_parsing_exception",
"reason" : "failed to parse"
}
],
"type" : "mapper_parsing_exception",
"reason" : "failed to parse",
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "Malformed content, found extra data after parsing: START_OBJECT"
}
},
"status" : 400
}
I've seen posts asking to add \n, but that didn't help.
You need to remove _doc from the requst.
curl -X POST "localhost:9200/bulk_meta/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"index":{"_id":"1"}}
{"mydoc":"index action, id 1 "}
{"index":{}}
{"mydoc":"index action, id 2"}
'
I created an index with a dense_vector:
curl -X PUT "localhost:9200/my_index?pretty" -H 'Content-Type: application/json' -d'
{
"mappings": {
"properties": {
"my_vector": {
"type": "dense_vector",
"dims": 3
}
}
}
}
'
When I index a document with a vector it works well:
curl -X PUT "localhost:9200/my_index/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
"my_vector" : [0.5, 10, 6]
}
'
BUT when I index a document with a null value for the vector it returns an error:
curl -X PUT "localhost:9200/my_index/_doc/2?pretty" -H 'Content-Type: application/json' -d'
{
"my_vector" : null
}
'
The error is:
{
"error" : {
"root_cause" : [
{
"type" : "parsing_exception",
"reason" : "Failed to parse object: expecting token of type [VALUE_NUMBER] but found [END_OBJECT]",
"line" : 5,
"col" : 1
}
],
"type" : "mapper_parsing_exception",
"reason" : "failed to parse",
"caused_by" : {
"type" : "parsing_exception",
"reason" : "Failed to parse object: expecting token of type [VALUE_NUMBER] but found [END_OBJECT]",
"line" : 5,
"col" : 1
}
},
"status" : 400
}
How can I handle null value for vector type in ES?
instead of setting it to null you can remove that field from that particular document which is equivalent to setting it as null using the followingrequest
curl --location --request POST 'http://{ip}:9200/my_index/_doc/{docId}/_update' \
--header 'Content-Type: application/json' \
--header 'Content-Type: application/json' \
--data-raw '{
"script" : "ctx._source.remove(\"my_vector\")"
}'
I'm using Curl to put data into ES. I have already created a customer index.
The following command is from ES document.
curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
"name": "John Doe"
}
'
When I do this, I get an error.
{
"error" : {
"root_cause" : [
{
"type" : "mapper_parsing_exception",
"reason" : "failed to parse"
}
],
"type" : "mapper_parsing_exception",
"reason" : "failed to parse",
"caused_by" : {
"type" : "json_parse_exception",
"reason" : "Unexpected character ('n' (code 110)): was expecting double-quote to start field name\n at [Source: org.elasticsearch.common.bytes.BytesReference$MarkSupportingStreamInputWrapper#1ec5236e; line: 3, column: 4]"
}
},
"status" : 400
}
I think, the below is the main reason of my error.
reason" : "Unexpected character ('n' (code 110)): was expecting double-quote to start field name
I have a feeling that I need to use (backslash) to escape. However, my attempt \' is not working great. Any advice?
I made it work like the below.
curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d '
{
\"name\": \"John Doe\" <==== I used "backslash" in front of all the "
}
'
Answer without my comment:
curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d '
{
\"name\": \"John Doe\"
}
'
I've looked at the docs but cannot get the following to work.
I have a search template that should look like this:
{
"template": {
"query": {
"bool": {
"must": [
{
"exists": {
"field": "on_sale"
}
}
{{#size}}
,
{
"term": {
"size": "{{size}}"
}
}
{{/#size}}
]
}
}
}
}
When I try to register the stringified version of the template with the api i get the following:
curl -XPOST 'localhost:9200/_search/template/test?pretty' -H 'Content-Type: application/json' -d '"{\"template\": {\"query\": {\"bool\": {\"must\": [{\"exists\": {\"field\": \"on_sale\"} } {{{#size}}} , {\"term\": {\"size\": \"{{size}}\"} } {{{/#size}}} ] } } } }"'
{
"error" : {
"root_cause" : [
{
"type" : "not_x_content_exception",
"reason" : "not_x_content_exception: Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"
}
],
"type" : "not_x_content_exception",
"reason" : "not_x_content_exception: Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"
},
"status" : 500
}
Update
Tried removing the single quotes and changing content type to plain/text
curl -XPOST 'localhost:9200/_search/template/test?pretty' -H 'Content-Type: text/plain' -d "{\"template\": {\"query\": {\"bool\": {\"must\": [{\"exists\": {\"field\": \"on_sale\"} } {{{#size}}} {\"term\": {\"size\": \"{{size}}\"} } {{{/#size}}} ] } } } }"
{
"error" : {
"root_cause" : [
{
"type" : "runtime_exception",
"reason" : "runtime_exception: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('{' (code 123)): was expecting comma to separate Array entries\n at [Source: org.elasticsearch.common.bytes.BytesReference$MarkSupportingStreamInputWrapper#1ea516ee; line: 1, column: 78]"
}
],
"type" : "runtime_exception",
"reason" : "runtime_exception: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('{' (code 123)): was expecting comma to separate Array entries\n at [Source: org.elasticsearch.common.bytes.BytesReference$MarkSupportingStreamInputWrapper#1ea516ee; line: 1, column: 78]",
"caused_by" : {
"type" : "i_o_exception",
"reason" : "Unexpected character ('{' (code 123)): was expecting comma to separate Array entries\n at [Source: org.elasticsearch.common.bytes.BytesReference$MarkSupportingStreamInputWrapper#1ea516ee; line: 1, column: 78]"
}
},
"status" : 500
}
I create a simple mapping:
curl -XPUT 'localhost:9200/ficherosindex?pretty=true' -d '{
"mappings": {
"items": {
"dynamic": "strict",
"properties" : {
"title" : { "type": "string" },
"body" : { "type": "string" },
"attachments" : { "type": "attachment" }
}}}}'
I make PUT the title and the body, leaving attachments empty.
curl -XPUT 'localhost:9200/ficherosindex/items/1' -d '{
"title": "This is a test title",
"body" : "This is the body of the java",
"attachments" : ""
}'
And then I make the following script to update the attachments fields with the content of the MY_PDF.pdf file, converting it to base64.
#!/bin/sh
coded=`cat MY_PDF.pdf | perl -MMIME::Base64 -ne 'print encode_base64($_)'`
curl -X POST 'localhost:9200/ficherosindex/items/1/_update?pretty=true' -d '{
"doc" : {
"attachments" : \"${coded}\"
}}'
When I run the script I'm getting the following error:
{
"error" : {
"root_cause" : [ {
"type" : "json_parse_exception",
"reason" : "Unexpected character ('\\' (code 92)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: [B#6c8caddf; line: 3, column: 30]"
} ],
"type" : "json_parse_exception",
"reason" : "Unexpected character ('\\' (code 92)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: [B#6c8caddf; line: 3, column: 30]"
},
"status" : 500
}
What I'm doing wrong? Maybe I've to change the following line?
{
"doc" : {
"attachments" : \"${coded}\"
}}'
I also tried this solution with no luck. I have to mantain the order I'm showing. First create the item without the attachments and then use the _update to append the content of the .PDF to it.
Thanks in advance
Something like this should do:
#!/bin/sh
coded=`cat MY_PDF.pdf | perl -MMIME::Base64 -ne 'print encode_base64($_)'`
curl -XPOST 'localhost:9200/ficherosindex/items/1/_update?pretty=true' -H "Content-Type: application/json" -d #- <<CURL_DATA
{ "doc": { "attachments": "$coded" }}
CURL_DATA