Elasticsearch Nested Match Last Value

We have article & edited authors as array. Need to check How many article last edited by particular author.

Data:

{
    "Article" : [
      {
        "id" : 12
        "title" : "An article title",
        "categories" : [1,3,5,7],
        "tag" : ["elasticsearch", "symfony",'Obtao'],
        "author" : [
            {
                "firstname" : "Francois",
                "surname": "francoisg",
                "id" : 18
            },
            {
                "firstname" : "Gregory",
                "surname" : "gregquat"
                "id" : "2"
            }
        ]
      }
    },
    {
        "id" : 13
        "title" : "A second article title",
        "categories" : [1,7],
        "tag" : ["elasticsearch", "symfony",'Obtao'],
        "author" : [
            {
                "firstname" : "Gregory",
                "surname" : "gregquat",
                "id" : "2"
            },
            {
                "firstname" : "Francois",
                "surname": "francoisg",
                "id" : 18
            }
        ]
      }
}

Request Data:
URL: http://localhost:9200/book/articles/_count/

For example `Gregory` & `Francois` should get one article as count.

Please let me know how to compare first object only & get results.

Hello,

I think you should add a property in your model in order to distinguish the first object of author's array.

As is, you can't make a request which target only the first element of an array.

If you don't need author's array mapped as "nested", you can eventually make a scripted request like this

GET /_search
{
    "query": {
        "bool" : {
            "must" : {
                "script" : {
                    "script" : {
                        "inline": "doc['author.firstname'][0] == params.searchedFirstName",
                        "lang": "painless",
                        "params":{
                                                 "searchedFirstName" : "Francois"
                          }
                     }
                }
            }
        }
    }
}

But without nested, you will not be able to search articles with author "Francois Francoisg"..
(look a the doc about nested type)

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.