ElasticSearch:过滤复杂查询

时间:2018-04-11 15:30:56

标签: elasticsearch filter

我在Elasticsearch(下面)中有一个复杂的查询,我需要一个非常基本的过滤器来过滤掉所有" resultat" (结果)其中value = 1.(resultat是一个数字字段)。

我尝试过使用" must_not"以下代码中的术语which was found here虽然它不会导致错误,但它似乎仍无法正确过滤结果。

我正在使用ElasticSearch 2.3.5和Lucene 5.5.0。

var searchQuery = {
  index: "resultats_" + env,
  body: {
    size: 0,
    query: {
      filtered: {
        query: {
          match_all: {}
        },
        filter: {
          query: {
            bool: {
              should: [{}],
              must: [
                {
                  term: {
                    player_id: {
                      value: params.player_id
                    }
                  }
                },
                {
                  term: {
                    classes: {
                      value: params.grade
                    }
                  }
                }
              ],
              must_not: [
                {
                  term: {
                    resultat: {
                      value: 1
                    }
                  }
                }
              ]
            }
          }
        }
      }
    },
    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,
                          sort: [
                            { date_creation: { order: "asc", mode: "min" } }
                          ]
                        }
                      },
                      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
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
};

0 个答案:

没有答案