Conditional update in Elasticsearch: "Invalid op [none]" - elasticsearch

Elasticsearch 2.3. I am firing this query to update every doc in an index based on a condition.
{
"query": {
"bool": {
"must": [
{
"match_all": {}
}
]
}
},
"script": {
"inline": "if (ctx._source.url.endsWith('a=6')) ctx.op = 'none' else ctx._source.url = ctx._source.url + '&b=3'"
}
}
As stated here, I am using ctx.op = 'none' to avoid updating the docs that do not match the condition.
I am getting
Invalid op [none]
Full error:
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "Invalid op [none]"
}
],
"type": "illegal_argument_exception",
"reason": "Invalid op [none]"
},
"status": 400
Seems to be so simple that I am pretty lost. Thanks for your help.

The correct operation is noop, not none.
From the documentation:
Just as in Update API you can set ctx.op = "noop" if your script decides that it doesn’t have to make any changes. That will cause _update_by_query to omit that document from its updates.
PR created: https://github.com/elastic/elasticsearch/pull/24613

Related

_update_by_query + script do not work correctly,error:Trying to create too many scroll contexts

Elasticsearch version: 7.6.2
JVM:13.0.2
OS version:centeros7
This is my code
POST recommend_index/_update_by_query
{
"script": {
"source": "ctx._source.rec_doctor_id = 1"
},
"query": {
"bool": {
"must": [{
"terms": {
"id": ["22222"]
}
}]
}
}
}
This code does not return the result correctly,The error message is
{
"error": {
"root_cause": [
{
"type": "exception",
"reason": "Trying to create too many scroll contexts. Must be less than or equal to: [5000]. This limit can be set by changing the [search.max_open_scroll_context] setting."
}
],
"type": "search_phase_execution_exception",
"reason": "Partial shards failure",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 1,
"index": "recommend_index",
"node": "XXX",
"reason": {
"type": "exception",
"reason": "Trying to create too many scroll contexts. Must be less than or equal to: [5000]. This limit can be set by changing the [search.max_open_scroll_context] setting."
}
}
]
},
"status": 500
}
I'm sure the current scroll is 0
When I replace _UPDATE_BY_QUERY with _UPDATE, it updates normally
No change has been made in ES since last Friday, and suddenly an error is reported
No configuration changes have been made to the ES server
follow-up:
I set the search.max_open_scroll_context parameter to 5000 and found nothing to do with it.
I looked up the 7.6.2 release and found that someone was having the same problem as me.Link on this
here #71354 #56202
I guess this is due to scrolling triggering the 7.6.2 bug.I restarted the cluster node without upgrading and found that it worked!!

update_by_query for multi field

I have added a new multi field (raw) of an existing field (response) in the index. And since the new multi field raw will have no data. I tried to add data from source as below.
POST /y_metrics/response/_update_by_query
{
"script":{
"inline": "ctx._source.response['raw'] = ctx._source.response;"
},
"query": {
"match_all": {}
}
}
fails :
"type": "missing_property_exception",
"reason": "No such property: raw for class: java.lang.String"
2nd try :
POST /y_metrics/response/_update_by_query
{
"script":{
"inline": "ctx._source['response.raw'] = ctx._source.response;"
},
"query": {
"match_all": {}
}
}
fails:
"type": "mapper_parsing_exception",
"reason": "Field name [response.raw] cannot contain '.'"
Apparently, the problem seems to be because of ".". But how else one would access multi field in this case ? I read about de_dot filter does it help in my case ?
if you add a field to an existing field (thus a multi field), there is no need to use a script, just reindex and Elasticsearch will handle the rest. You can just drop the script part of your update by query call.

ElasticSearch returns 404 while multi-index, multi-type search

We have a requirement where in we need to query across multiple indices as follows
We are using ElasticSearch 5.1.1.
http://localhost:9200/index1,index2,index3/type1,type2/_search
query:
{
"query": {
"multi_match": {
"query": "data",
"fields": ["status"]
}
}
}
However we may not know in advance if the index is present or not , we get following error if either of above indices is not present.
{
"error": {
"root_cause": [
{
"type": "index_not_found_exception",
"reason": "no such index",
"resource.type": "index_or_alias",
"resource.id": "index3",
"index_uuid": "_na_",
"index": "index3"
}
],
"type": "index_not_found_exception",
"reason": "no such index",
"resource.type": "index_or_alias",
"resource.id": "index3",
"index_uuid": "_na_",
"index": "index3"
},
"status": 404
}
One obvious way is to check if index is already present or not but I would like to avoid that extra call.
Note: At least 1 index will always be present
Is it possible to avoid this Exception ?
Thanks in advance !!
"ignore_unavailable" is the solution for this. Pass this as a query parameter in search url.
Exa. http://localhost:9200/index1,index2/type/_search?ignore_unavailable
This will not give 404 even if either of the indices are not present

ElasticSearch script query & geo distance query

ElasticSearch version is 2.3.4
How to query geo_distance_query and script_query combination ?
I have an index, which has a lot of store information, there are location and distance fields.
Given a geo_point, the index based on the top of the query based on geo_distance_query this point to all store the distance, and calculated the distance should be less than the current store in the distance field value.
I think the script_query and geo_distance_query combination, do not know how to achieve.
Try the following code:
query: {
bool: {
must: [
{
script: {
script: {
inline: "doc['location'].arcDistance(" + _.lat + "," + _.lon + ") < doc['distance'].value"
, lang: "painless"
}
}
}
]
}
}
Results Elasticsearch error:
[illegal_argument_exception] script_lang not supported [painless]
Whether someone has encountered and solved this problem, using what method to implement the query ?
change code :
{
bool: {
must: [
{
script: {
script: {
inline: "doc['location'].arcDistance(" + _.lat + "," + _.lon + ") < doc['distance'].value"
, lang: "groovy"
}
}
}
]
}
}
also error :
message: '[script_exception] failed to run inline script [doc[\'location\'].arcDistance(31.89484,120.287825) < doc[\'distance\'].value] using lang [groovy]',
detail image :
{
"error": {
"root_cause": [{
"type": "script_exception",
"reason": "failed to run inline script [doc[\'location\'].arcDistance(31.89484,120.287825) < 3000] using lang [groovy]"
}],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query_fetch",
"grouped": true,
"failed_shards": [{
"shard": 0,
"index": "business_stores_v1",
"node": "Z_65eOYXT6u8aDf7mp2ZRg",
"reason": {
"type": "script_exception",
"reason": "failed to run inline script [doc[\'location\'].arcDistance(31.89484,120.287825) < 3000] using lang [groovy]",
"caused_by": {"type": "null_pointer_exception", "reason": null}
}
}]
}, "status": 500
}
The error you're getting, i.e. null_pointer_exception is because one of the documents in your business_stores_v1 index has a null location field and thus the formula fails
doc['location'].arcDistance(...)
^
|
null_pointer_exception here

_search CJK term in elasticsearch

Does elasticsearch not support query cjk character in url ?
I need to query term 北京(Beijing in chinese) for field(name) in index (old_merge_result). The following query seems not working. ES would retur
GET /old_merge_result/tempid/_search?q=name:北京
ES would return :
{
"statusCode": 400,
"error": "Bad Request",
"message": "child \"uri\" fails because [\"uri\" must be a valid uri]",
"validation": {
"source": "query",
"keys": [
"uri"
]
}
}
Instead, query through the following would return exactly what i want.
GET /old_merge_result/tempid/_search
{
"query": {
"term": {
"name": {
"value": "北京"
}
}
}
}
So is there any way query through url like old_merge_result/tempid/_search?q=name:北京 ?
One needs to use percent-encoding/URL-encoding to pass cjk characters as query parameters
For the above example it would be :
GET /old_merge_result/tempid/_search?q=name:%E5%8C%97%E4%BA%AC

Resources