Alright, I finally managed to limit the resulting set of documents by adding
a size keyword :


{
   "fields":[

   ],
   "size":0,
   "from":0,
   "sort":[
      "_score"
   ],
   "query":{
      "filtered":{
         "query":{
            "bool":{
               "should":[
                  {
                     "multi_match":{
                        "query":"general",
                        "fields":[
                           "Titre.Valeur.original^4",
                           "Titre.Valeur.partial",
                           "Resume.Valeur.original^2",
                           "Resume.Valeur.partial",
                           "Corps.Valeur.original^2",
                           "Corps.Valeur.partial"
                        ]
                     }
                  }
               ],
               "minimum_should_match":1
            }
         }
      }
   },

    "aggs" : {

                "Count":{
                         "cardinality":{
                                "field":"IDDocument"
                         }
                  },
                  
                  "doc_limit_agg" :{
                  
                        "filter" : {
                                "script" : {
                                        "script" : "_doc.score <0.05"
                                }
                        },
                
                        "aggs" : {
                                "docagg" : {

                                        "terms" : {
                                                "field" : "IDDocument",
                                                "order" : { "version_max>score" 
: "desc" }
                                                ,"size":4
                                        },
                                        "aggs" : {
                                                "version_max" : {
                                                        "filter" : { "range" : 
{ "NumeroVersion" : { "gt" : 0 } }},
                                                        "aggs" : {
                                                                "vMax" : { 
"max" : { "field" : "NumeroVersion" }},
                                                                "score" : { 
"max" : { "script":"_doc.score"}}
                                                        }
                                                }
                                        }
                                }
                        }
        }
    }
}

Pagination could then be addressed based on highest score order and its
filtered value (see script above).

Last question, and yet maybe something not supported by ES : is there a way
to dynamically calculate a column (like a  ROW_NUMBER() in SQL) and use it
to sort and filter like above. Actually I'm seeking a better and more robust
way to filter/sort than the score column.

Again the SQL equivalent to illustrate what I'm aiming at :

with VersionMax as (
    select d.IDDocument, max(d.NumeroVersion) as NumeroVersion, max(d.Score)
as ScoreDocument,
    ROW_NUMBER() OVER(ORDER BY max(d.Score) DESC, d.IDDocument DESC) as
RowNumber
        from @Document d
        group by d.IDDocument
)
select top 2 *
from VersionMax v
where v.RowNumber < 4
order by v.RowNumber desc





--
View this message in context: 
http://elasticsearch-users.115913.n3.nabble.com/ES-aggregation-and-pagination-tp4052774p4052906.html
Sent from the ElasticSearch Users mailing list archive at Nabble.com.

-- 
You received this message because you are subscribed to the Google Groups 
"elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elasticsearch/1395929238409-4052906.post%40n3.nabble.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to