Elasticsearch painless script update and get in a one POST call - elasticsearch

I am using Elasticsearch 7.4, and have a unique requirement to perform below operation in elasticsearch through painless script:-
Update the field through painless script
Get the updated value in response
#1 I can perform using painless script using below to update the counter value, but how can I get the updated value in response? I want the new value ctx._source.counter in response of the POST call I made, Is there any way?
curl -XPOST -H "Content-Type:application/json" localhost:9200/myindex/_update_by_query -d "{"query":{"bool":{"filter":[{"match":{"field1":"1"}}]}},"script":{"source":"ctx._source.counter = ctx._source.counter + params.incrementValue","lang":"painless","params":{"incrementValue":"2"}}}"

What you want is different from the specs of the _update_by_query API. In the specs you can see the response only contains stats & status of the update, and not the document body (neither initial or final).
One way to accomplish your goal is:
Query document to update ie. where field1==1
Get initial counter value
Update
With this, you can get compute the updated value at your end with the method of your choice. In any case it would be two API calls not just one.

Related

Need to Get data for an index for a timeframe using curl or using url

I need to Get data for an index for a timeframe using curl or using url.
I was able to get the data using below created url
http://localhost:9200/index_name/_search?size=10&q="* AND severity:major|critical"
but I am not sure where to provide the timeframe for example i only want data from last 15minutes.
Can anyone help me with a way it can be done
You can do it like this by adding #timestamp:[now-15m TO now] to your query:
http://localhost:9200/index_name/_search?size=10&q="* AND severity:major|critical AND #timestamp:[now-15m TO now]"

Elasticsearch in-place-update like solr

In SOLR I can use In-Place-Update to update the value of any fields. Here the value of popularity field will be incremented every time by 20 and add it to the the current value of popularity without considering any other things.
{
"id":"mydoc",
"price":{"set":99},
"popularity":{"inc":20}
}
For Elasticsearch, I can also use the _update api using script to In-Place-update.
POST /website/blog/1/_update
{
"script" : "ctx._source.popularity+=20"
}
But my problem is I want to use the _bulk api using python to in-place-update multiple documents at one with some incremental values. Here I've seen the documentation on how to use the _bulk endpoint to set different values with update action payload. I'm just having some difficulties how can I make the same POST JSON datasets for _bulk with python elasticsearch client for the script update way.

How do you allow a particular http method on elasticsearch6 for indexes with no types?

I am taking some courses on udemy on elastic search and trying to set up an elastic search project. I have gotten the bulk api to work and can successfully send batch data into an index on elastic search. But I am having trouble sending data without the bulk api. Because I have read on the elastic search docs that there is a 'false' analogy that a type is like a table in a database and an index is like a database. I decided for this project to create an index for each of the entities that I want to persist which is users and statistics. Therefore I have indices which are called statisitics and users. When I make the following request from postman
headers: Content-Type application/json
POST http://localhost:9200/users
with body:
{"id": 1, "name":"myname"}
I get an error
{"error":"Incorrect HTTP method for uri [/users] and method [POST],
allowed: [PUT, DELETE, GET, HEAD]","status":405}
How can I allow this http method?
Hello I had the same issue using PHP and JAVA, this happen simply because you must add the _type after the index name (described here) :
https://www.elastic.co/guide/en/elasticsearch/guide/current/index-doc.html
Try curl -XPOST http://localhost:9200/users/_admin or something similar ;)
See https://www.elastic.co/guide/en/elasticsearch/guide/current/mapping.html
Since version 6.2, we need to use _doc for the type.
https://discuss.elastic.co/t/cant-use-doc-as-type-despite-it-being-declared-the-preferred-method/113837
https://www.elastic.co/guide/en/elasticsearch/reference/master/removal-of-types.html#_schedule_for_removal_of_mapping_types
In fact, when you say there is "no types" allowed in ElasticSearch, it is false (at least for versions up to 8.x). What is true is that there is only 1 type allowed for each index.

Delete similarly named indexes from elasticsearch Head Plugin

I want to execute a curl request from the elasticsearch head plugin in order to delete all the similarly named indexes. I am using the following command but getting an error.
The _search endpoint doesn't support the HTTP DELETE method.
Do this instead:
In the location field, set parkingapi-farhad-*
Select the DELETE method
Click the "Request" button
You can also do it using a simple curl:
curl -XDELETE http://localhost:9500/parkingapi-farhad-*

Inserting an Element with a _parent field

I'm using ElasticSearch for an application in order to store and search for data.
Because it's also important to search for relationships in my particular case, I recently changed the structure of my data and I am using the _parent field now. (http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-parent-field.html)
I already designed the search query, which works perfectly fine. However, I now have a problem when inserting a new child entry in my database.
It should work this way:
Without the _parent field, when I wanted to achieve this
$ curl -XPUT 'http://localhost:9200/data/child/1' -d '
I inserted the data this way using the JEST API:
client.execute(new Index.Builder(payload).index("data").type("child").build());
When I want to achieve this:
$ curl -XPOST localhost:9200/data/child?parent=154121-d'
How am I supposed to implement this using the JEST-API?
It's possible to provide a parameter for the request. The correct line of code to achieve this:
$ curl -XPOST localhost:9200/data/child?parent=154121-d'
would be achieved with this:
client.execute(new Index.Builder(payload).index("data").type("child").setParameter("parent", "154121").build());

Resources