【问题标题】:ElasticSearch: Sorting a complex queryElasticSearch:对复杂查询进行排序
【发布时间】:2018-09-21 11:05:34
【问题描述】:

我在 Elasticsearch(如下)中有一个复杂的查询,我需要在“Activité”(活动)存储桶中按 date_creation 升序排序。查询有效,但我对 date_creation 的基本排序无效。我正在寻找如何按 date_creation 升序对活动进行排序。我看过一些关于嵌套查询here on stackoverflow 的帖子,例如和here in the elastic search documentation,但他们似乎没有回答如何解决我的查询的复杂性。

我正在使用 ElasticSearch 2.3.5 和 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
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
};

【问题讨论】:

  • 所以你想对Activité聚合下的热门排序?
  • 是的,@Val 听起来很对。

标签: sorting elasticsearch nested


【解决方案1】:

你可以这样做:

              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" } }],
                    }
                  },

【讨论】:

  • 我无法确认它是否正常工作,但我可以确认它没有破坏我的查询 - 您的回答有助于我了解总体思路。
  • 酷,很高兴它有帮助,如果您有更多问题,请随时回来。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-08
  • 2020-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-06
相关资源
最近更新 更多