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
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
};
2
  • So you want to sort the top hits under the Activité aggregation? Commented Apr 11, 2018 at 14:08
  • Yes, @Val that sounds about right. Commented Apr 11, 2018 at 14:12

1 Answer 1

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" } }],
                    }
                  },
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.