Elasticsearch。使用术语汇总,返回文档计数小于某个值的值

时间:2018-07-09 08:13:06

标签: elasticsearch

我想使用术语汇总将字段(在我的情况下为帐户ID)按值分组,并且仅返回doc_count小于某个值的字段。

我可以指定min_doc_count参数,但是没有max_doc_count。因此,我正在寻找一种模拟此行为的方法。我的许多尝试之一是这样做,但它不起作用。

export default class ComponentIndex extends React.Component {
    render(){
        return(
            <ImageBackground
            source={require('./../../assets/images/background_placeholder.png')}
            style={{width: '100%', height: '100%'}}
            >
                <View style={styles.parentView}>
                    <View style={styles.elementSpacer}>
                        <Image
                            source={require('./../../assets/images/iview_learning_logo.png')}
                            style={styles.headerImage}
                        />
                    </View>
                    <View style={styles.elementContainer}>
                        <Text style={styles.subheadingText}>App for comprehensive tutorials</Text>
                    </View>
                    <View style={styles.elementContainer}>
                        <Button rounded style={styles.startButton}>
                            <Text style={styles.startButtonText}>LET'S GO</Text>
                        </Button>
                    </View>
                </View>
            </ImageBackground>
        );
    }
}

const styles = StyleSheet.create({
    parentView: {
        flex: 1,
        flexDirection: 'column',
        padding: 30,
        justifyContent: 'center',
    },
    headerImage: {
        resizeMode: 'contain',
        height: undefined,
        width: undefined,
    },
    elementSpacer: {
        flex: 1,
    },
    elementContainerHeader: {
        height: 60,
    },
    elementContainer: {
        margin: 10,
    },
    subheadingText: {
        fontSize: 18,
        textAlign: 'center',
        //fontFamily: 'Arial',
    },
    startButton: {
        alignItems: 'center',
        justifyContent: 'center',
        alignSelf: 'center',
        paddingRight: 25,
        paddingLeft: 25,
        backgroundColor: '#c00000',
    },
    startButtonText: {
        color: 'white',
        //fontWeight: 'bold',
        fontSize: 20,
        //fontFamily: 'Arial',
    },
});

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

bucket_selector聚合需要嵌套(因为它是 parent-type聚合),并且必须嵌套用于过滤存储桶的度量标准聚合。

因此,我们使用顶层 terms聚合,然后使用嵌套的value_count聚合将存储桶doc_count暴露给同级selector_bucket聚合

尝试:

{
  "size": 0,
  "aggs": {
    "by_account": {
      "terms": {
        "field": "accountId"
      },
      "aggs": {
        "by_account_number": {
          "value_count" : {
            "field" : "accountId"
          }
        },
        "by_account_filtered": {
          "bucket_selector": {
            "buckets_path": {
              "totalDocs": "by_account_number"
            },
            "script": "params.totalDocs < 10000"
          }
        }
      }
    }

  }
}

编辑:如果您想获得最低的帐户doc_count

{
      "size": 0,
      "aggs": {
        "by_account": {
          "terms": {
            "field": "accountId",
            "order" : { "_count" : "asc" },
            "size": 100
          },
          "aggs": {
            "by_account_number": {
              "value_count" : {
                "field" : "accountId"
              }
            },
            "by_account_filtered": {
              "bucket_selector": {
                "buckets_path": {
                  "totalDocs": "by_account_number"
                },
                "script": "params.totalDocs < 10000"
              }
            }
          }
        }

      }
    }