Can I use a painless script on a GET to update a counter? - elasticsearch

I have an index with documents that have an "access count" field which is intended to store the number of times that the document has been accessed. (Much like this web page.)
Of course I can use an update after each get to update the field, but is there a way to attach a Painless script to the get request to increment the field? Something like:
doc['access_count'] += 1;
I don't see an obvious answer out of the documentation, but if anyone has done this it would be helpful to know.

A GET call is supposed to be idempotent, i.e. calling the same URI multiple times doesn't change the underlying resource. So what you're asking goes against that principle and that's (luckily) not possible to do it.
You'll need to update a counter on that document separately.

Related

using entsp in GSA - making no difference to the results

I was hoping someone had some experience using the entsp flag in GSA and was able to point out what I am doing wrong. I've read the documentation but can't figure it out.
The query string I am passing through is:
?as_sitesearch=examplesite.com&callback=angular.callbacks._1&collection=hybrid_site_colection&entsp=a__di_site_biasing&frontend=jsonp&num=10&query=test&start=0
I've set up the biasing in the GSA, and when I apply it to the frontend itself, it behaves. But I was hoping to pass it in dynamically, as I believed you could (we are likely to want to dynamically pass through one of a few biasing, depending on where the user is coming from), it makes no difference.
Am I missing a tag, or can someone see anything else wrong?
Sorry, peoples, I've seen now that the problem is that we are going through a JSON bridge that isn't passing through my variables.
When I go directly to the GSA with a frontend, it works.

Fully update documents without creating if not existent

Is there any method on elasticsearch for fully (not partially) updating documents and not create new ones in case it doesn’t already exists?
Until now, I found that the _update method, while passing a doc attribute inside the json request body to partially updating documents, however, I would like to replace the entire document in this case, not only partially.
I have also found that, the index method, where sending a PUT request works fine, although creating a new document in case the id not yet indexed.
Setting the op_type parameter to create will enforce document creation instead update.
I was wondering if there is any way to always enforce update and never create a new one?
Or perhaps is there another method that would allow me to achieve such task?
If I understand correctly, you want to index a doc, but only if it already exists? Like an op_type option of update?
You can mostly do it with the update API, given that your mapping remains consistent. With an _update, if the document doesn't exist, you'll get back a 404. If it does exist, ES will merge the contents of doc with whatever document exists there. If you make sure you're sending over a new doc with all the fields in the mapping, then you're effectively replacing it outright.
Note, however, that you can do it without the document merge rather efficiently in two requests; the first one checking for doc existence with a HEAD request. If HEAD /idx/type/id is successful, then do a PUT. This is essentially what's happening internally anyway with the update API, with a little extra overhead. But HEAD is really cheap because it's not shuffling any payload around. It simply returns an HTTP 200/404.

Is it possible to check previous value of a key in beforeSave?

Let's say I want to perform custom logic only, say, when a user's verified field changes from false to true (in order to make sure they are allowed to be performing this operation). Is there a way in Cloud Code to see what the 'current', i.e. about-to-be-overwritten value of a field is?
I would look at changedAttributes(), previousAttributes() and previous("columnName") to see if these have been exposed in the beforeSave handler yet.
Update note: none of those methods help.
The only other option I've seen in some older questions is to check object.existed() and in that case do a get() request to load the original values before the save. Obviously this causes 2 API requests per save.
It would be great to hear back if the changed/previous methods work.
Update
I have since done some more thorough testing, and the only option is to get() the previous version of the record. Nothing else works. This of course requires that you do it in the before-save handler.

Passing values between cucumber statements

I'm running in to an issue in that I need to get one value in a cucumber statement, and then give that value to another statement.
Specifically I am getting a JSON object from one page (where that object gets sent to an api endpoint as a preference) and then using information out of that after I query the api, which happens in a completely separate step.
I am suspecting that I have to write the value somewhere, and then pull that value when the step that needs it comes up, but I'm kind of at a loss for doing that as well.
I can provide any further needed details, thanks for any help!
Definitely a rookie question - to which the answer is to use instance variables - #variable_name = get_method in the helper method your step calls.

Symfony2, doctrine2, session, best way to paginate search page

Let's imagine we have simple data and want to make pagination of it. It's not hard to do, simple _GET var with page number others doctrine with offset will allow us to do it in easy way, BUT How should it look like in search page? Let me explain.
For example we have simple route with /search url. Where we have form for our search. When use input string we user POST method on same page and will get result. Simple enough but if we add pagination here it become a problem with storing "inputed string".
If we store in session on search query it will be solution BUT... it's not. Why? User input search string - get result with pagination (here search string already in session) after that leave the page (or close browser, or left to another page). When he will return data from session will show him 'result of old query'...
So question is, what is the best practice for such situation? I want simple search query + pagination of it but if user left page - clear result.
Using POST instead of GET for search query is kinda unusual and not really safe. Since search query operations are read-only you should use GET to access/get the data. POST is used for updating or creating resources.
And how you will go back/forward in the pagination (using browser's buttons)? You always will be getting an alert box. AND you cannot share/bookmark the search query url.
BTW to answer your question, sessions and hidden input fields would be the way to go. You also can use a combination of get and post
When should I use GET or POST method? What's the difference between them?

Resources