2016-01-04 51 views
0

我一直在尝试很长一段时间,但看不到问题。如果有更多ES经验的人能够向我提出正确的方向,我会很高兴。我有一个父母类型(学院)和一个孩子类型(课程)。课程有3级嵌套聚合(Subjectgroup)。现在我试图说明有多少大学在大学查询中提供具有单个学科组的课程。嵌套的子集合返回“错误”父集doc_count

这里是我的映射:

indexes: 
    studiengaenge: 
     index_name: studiengaenge_dev 
     settings: 
      index: 
       analysis: 
        analyzer: 
         lc_term: 
          type: custom 
          tokenizer: keyword 
          filter: lowercase 
     types: 
      college: 
       mappings: 
        id: ~ 
      course: 
       mappings: 
        id: ~ 
        name: ~ 
        subjectgroups: 
         type: "nested" 
         properties: 
          name: { "type": "string", "index": "analyzed", "analyzer": "lc_term" } 
          area: 
           type: "nested" 
           properties: 
            name: { "type": "string", "index": "analyzed", "analyzer": "lc_term" } 
            field: 
             type: "nested" 
             properties: 
              name: { "type": "string", "index": "analyzed", "analyzer": "lc_term" } 
       _parent: 
        type: "college" 

查询:

GET college/_search?search_type=count 
 
{ 
 
    "query": { 
 
    "has_child": { 
 
     "type": "course", 
 
     "query": { 
 
     "filtered": { 
 
      "query": { 
 
      "match_all": {} 
 
      }, 
 
      "filter": { 
 
      "bool": { 
 
       "must": [ 
 
       { 
 
        "nested": { 
 
        "path": "subjectgroups", 
 
        "filter": { 
 
         "terms": { 
 
         "subjectgroups.name": [ 
 
          "lehramt" 
 
         ] 
 
         } 
 
        } 
 
        } 
 
       } 
 
       ] 
 
      } 
 
      } 
 
     } 
 
     } 
 
    } 
 
    }, 
 
    "aggs": { 
 
    "children": { 
 
     "children": { 
 
     "type": "course" 
 
     }, 
 
     "aggs": { 
 
     "fachgruppen": { 
 
      "nested": { 
 
      "path": "course.subjectgroups" 
 
      }, 
 
      "aggs": { 
 
      "filtered": { 
 
       "filter": { 
 
       "terms": { 
 
        "subjectgroups.name": [ 
 
        "lehramt" 
 
        ] 
 
       } 
 
       }, 
 
       "aggs": { 
 
       "fachgruppe": { 
 
        "terms": { 
 
        "field": "subjectgroups.name" 
 
        }, 
 
        "aggs": { 
 
        "reverse_nested": { 
 
         "reverse_nested": {}, 
 
         "aggs": { 
 
         "doc_count_college": { 
 
          "cardinality": { 
 
          "field": "_parent" 
 
          } 
 
         } 
 
         } 
 
        }, 
 
        "studienbereich": { 
 
         "nested": { 
 
         "path": "course.subjectgroups.area" 
 
         }, 
 
         "aggs": { 
 
         "studienbereich": { 
 
          "terms": { 
 
          "field": "subjectgroups.area.name" 
 
          }, 
 
          "aggs": { 
 
          "reverse_nested": { 
 
           "reverse_nested": {}, 
 
           "aggs": { 
 
           "doc_count_college": { 
 
            "cardinality": { 
 
            "field": "_parent" 
 
            } 
 
           } 
 
           } 
 
          } 
 
          } 
 
         } 
 
         } 
 
        } 
 
        } 
 
       } 
 
       } 
 
      } 
 
      } 
 
     } 
 
     } 
 
    } 
 
    } 
 
}

结果:

{ 
 
    "took": 7, 
 
    "timed_out": false, 
 
    "_shards": { 
 
     "total": 1, 
 
     "successful": 1, 
 
     "failed": 0 
 
    }, 
 
    "hits": { 
 
     "total": 123, 
 
     "max_score": 0, 
 
     "hits": [] 
 
    }, 
 
    "aggregations": { 
 
     "children": { 
 
     "doc_count": 12289, 
 
     "fachgruppen": { 
 
      "doc_count": 15029, 
 
      "filtered": { 
 
       "doc_count": 4582, 
 
       "fachgruppe": { 
 
        "doc_count_error_upper_bound": 0, 
 
        "sum_other_doc_count": 0, 
 
        "buckets": [ 
 
        { 
 
         "key": "lehramt", 
 
         "doc_count": 4582, 
 
         "reverse_nested": { 
 
          "doc_count": 3786, 
 
          "doc_count_college": { 
 
           "value": 124 
 
          } 
 
         }, 
 
         "studienbereich": { 
 
          "doc_count": 4582, 
 
          "studienbereich": { 
 
           "doc_count_error_upper_bound": 0, 
 
           "sum_other_doc_count": 0, 
 
           "buckets": [ 
 
           { 
 
            "key": "schulische fächer", 
 
            "doc_count": 3938, 
 
            "reverse_nested": { 
 
             "doc_count": 3399, 
 
             "doc_count_college": { 
 
              "value": 130 
 
             } 
 
            } 
 
           }, 
 
           { 
 
            "key": "berufliche fachrichtungen", 
 
            "doc_count": 357, 
 
            "reverse_nested": { 
 
             "doc_count": 315, 
 
             "doc_count_college": { 
 
              "value": 105 
 
             } 
 
            } 
 
           }, 
 
           { 
 
            "key": "sonderpädagogik, inklusive pädagogik", 
 
            "doc_count": 287, 
 
            "reverse_nested": { 
 
             "doc_count": 287, 
 
             "doc_count_college": { 
 
              "value": 32 
 
             } 
 
            } 
 
           } 
 
           ] 
 
          } 
 
         } 
 
        } 
 
        ] 
 
       } 
 
      } 
 
     } 
 
     } 
 
    } 
 
}

的问题是,即使有只有123结果(学院),第2级subjectgroup的聚集告诉我,有130所院校“钥匙”:“schulischefächer”。 任何帮助,不胜感激。谢谢,汉斯

回答

0

在这种情况下的问题是,elasticsearch不一定精确聚合计数。如果Elasticsearch认为聚合的结果不会受到该特定分片可能返回的结果的显着影响,那么Elasticsearch可能完全忽略它。

这只会导致聚合的近似结果。尝试设置索引参数的方式只有一个数据碎片。那么结果可能会有所不同,并可能是确切的。然而,这不是一个解决方案,因为如果你有很多数据,你需要将它分散在不同的分片上。

弹性搜索的这些属性在聚合和其他操作方面的缺点是无与伦比的速度。

+0

感谢您的快速响应。我将设置更改为仅使用一个分片,但不幸的是,它对结果没有任何影响。我读到ES在这些计算上不是100%正确的,但这看起来很不正确。我认为肯定还有其他问题。如果将所有文件都纳入计算并且不应用过滤器/查询,那么130将是正确的金额。这可能与它有什么关系? – HKandulla

+0

我编辑了问题(结果)到已更改的设置(一个分片)。 – HKandulla

+0

你说得对。这只是ES的准确性问题,而不是查询。我添加了一个500的“precision_threshold”,我现在得到了确切的数字。谢谢! – HKandulla