Increasing 'view' counter of a document in an index everytime it gets queried explicitly using _id via _search endpoint

39 views Asked by At

Say, I have an index called blog which has 10 documents called article. The article is a JSON with one of the property being views which is initialized to 0.

I was wondering if there's a good way of updating the views counter everytime the document gets explicitly called via _search endpoint using document id, so that I can sort it by view on my other queries.

Or would that be something that will have to be taken care of at the application layer?

My feeble attempt query dsl so far:

let options = {
    index: 'blog',
    body: {
        query: {
            function_score: {
                query: {
                    match: { _id: req.params.articleID }
                },
          "weight" : 2
            ,
            score_mode: "sum"
            ,
            script_score : {
                script : {
                  inline: "(2 + doc['view'].value)"
                }
            }
                }
            },
        }
    };

I have been trying inline script but that would require me to send two separate request. First search & then update if found. I was wondering if I could do it on a single query i.e trigger the views counter to increase by one automatically everytime I query via _search.

0

There are 0 answers