0

I have a complex query in Elasticsearch (below) and I need to sort by date_creation ascending, within the "Activité" (activity) bucket. The query works, but the basic sort I have for date_creation does not. I am looking for how I would sort the Activities by date_creation, in ascending order. I've seen some posts on nested queries here on stackoverflow, for example and here in the elastic search documentation but they don't seem to answer how to address the complexity of my query.

I am using ElasticSearch 2.3.5 with Lucene 5.5.0.

var searchQuery = {
  index: "resultats_" + env,
  body: {
    size: 0,
    sort: [{ date_creation: { order: "asc", mode: "min" } }],
    query: {
      filtered: {
        query: {
          match_all: {}
        },
        filter: {
          query: {
            bool: {
              should: [{}],
              must: [
                {
                  term: {
                    player_id: {
                      value: params.player_id
                    }
                  }
                },
                {
                  term: {
                    classes: {
                      value: params.grade
                    }
                  }
                }
              ],
              must_not: [{}]
            }
          }
        }
      }
    },
    aggs: {
      Matière: {
        terms: {
          field: "id_matiere",
          size: 10
        },
        aggs: {
          "Titre matière": {
            top_hits: {
              _source: {
                include: ["titre_matiere"]
              },
              size: 1
            }
          },
          PP: {
            terms: {
              field: "id_point_pedago",
              size: 10
            },
            aggs: {
              "Titre PP": {
                top_hits: {
                  _source: {
                    include: ["titre_point_pedago"]
                  },
                  size: 1
                }
              },
              Compétence: {
                terms: {
                  field: "id_competence",
                  size: 10
                },
                aggs: {
                  "Titre compétence": {
                    top_hits: {
                      _source: {
                        include: ["titre_competence"]
                      },
                      size: 1
                    }
                  },
                  Activité: {
                    terms: {
                      field: "id_activite",
                      size: 10
                    },
                    aggs: {
                      "Titre activité": {
                        top_hits: {
                          _source: {
                            include: [
                              "titre_activite",
                              "nombre_perimetre_occurrence"
                            ]
                          },
                          size: 1
                        }
                      },
                      Trimestres: {
                        filters: {
                          filters: {
                            T1: {
                              range: {
                                date_creation: {
                                  gte: params.t1_start,
                                  lte: params.t1_end
                                }
                              }
                            },
                            T2: {
                              range: {
                                date_creation: {
                                  gte: params.t2_start, 
                                  lte: params.t2_end 
                                }
                              }
                            },
                            T3: {
                              range: {
                                date_creation: {
                                  gte: params.t3_start, 
                                  lte: params.t3_end
                                }
                              }
                            }
                          }
                        },
                        aggs: {
                          Moyenne: {
                            avg: {
                              field: "resultat"
                            }
                          },
                          Occurrences: {
                            cardinality: {
                              field: "id_occurrence",
                              precision_threshold: 1000
                            }
                          },
                          Résultat: {
                            terms: {
                              field: "resultat",
                              size: 10,
                              min_doc_count: 0
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
};
Nathan W
  • 486
  • 1
  • 5
  • 16

1 Answers1

1

You can do it like this:

              Activité: {
                terms: {
                  field: "id_activite",
                  size: 10
                },
                aggs: {
                  "Titre activité": {
                    top_hits: {
                      _source: {
                        include: [
                          "titre_activite",
                          "nombre_perimetre_occurrence"
                        ]
                      },
                      size: 1,
add this line ->      sort: [{ date_creation: { order: "asc", mode: "min" } }],
                    }
                  },
Val
  • 207,596
  • 13
  • 358
  • 360
  • I can't confirm that it's working, yet, but I can confirm that it's not breaking my query - and your answer helps me get the general idea. – Nathan W Apr 11 '18 at 14:34