# Can the split “query” and “aggregations” achieve higher performance？

**URL:** <https://discuss.elastic.co/t/can-the-split-query-and-aggregations-achieve-higher-performance/278034>\
**Category:** Elasticsearch\
**Created:** [July 7, 2021, 7:32am UTC](https://discuss.elastic.co/t/can-the-split-query-and-aggregations-achieve-higher-performance/278034 "2021-07-07T07:32:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryze](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ryze/32/81828_2.png) [@Ryze](https://discuss.elastic.co/u/Ryze)\
**Post date:** [July 7, 2021, 7:32am UTC](https://discuss.elastic.co/t/can-the-split-query-and-aggregations-achieve-higher-performance/278034/1 "2021-07-07T07:32:48Z")

</div>

Imagine I have an index "article" with 100 million pieces of data. Its template data and mapping file are as follows. What I want to ask is, does splitting query and aggs (The following query1) and then using \_bulk api to execute them has higher performance than executing a dsl alone(The following query2)? （elasticsearch version 7.x）

mappings：  
`https://github.com/ZhangDi-d/elasticsearch-demo/blob/main/index_article_mappings.md`

template data:  
`https://github.com/ZhangDi-d/elasticsearch-demo/blob/main/index_article_template_data.md`

**query1:**  
include a "query" dsl and a "aggs" dsl

```auto
GET /article/_search
{
  "from": 0,
  "size": 1000,
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "hidden": {
                  "value": "false"
                }
              }
            },
            {
              "match": {
                "new_author_last_name": "Smith"
              }
            },
            {
              "range": {
                "view_cnt": {
                  "gte": 1
                }
              }
            },
            {
              "bool": {
                "should": [
                  {
                    "simple_query_string": {
                      "query": "i",
                      "fields": ["content"]
                    }
                  }
                ]
              }
            }
          ],
          "must_not": [
            {
              "match": {
                "author_first_name": "Danny"
              }
            }
          ]
        }
      },
      "functions": [
        {
          "filter": {
            "match_all": {
              "boost": 1
            }
          },
          "field_value_factor": {
            "field": "follower_num",
            "factor": 1,
            "missing": 22.5,
            "modifier": "none"
          }
        }
      ]
    }
  },
  "_source": false,
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    },
    {
      "follower_num": {
        "order": "desc",
        "missing": 0
      }
    },
    {
      "view_cnt": {
        "order": "desc",
        "missing": 0
      }
    }
  ],

  "collapse": {
    "field": "userID"
  }
}

GET /article/_search
{
  "size": 0,
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "hidden": {
                  "value": "false"
                }
              }
            },
            {
              "match": {
                "new_author_last_name": "Smith"
              }
            },
            {
              "range": {
                "view_cnt": {
                  "gte": 1
                }
              }
            },
            {
              "bool": {
                "should": [
                  {
                    "simple_query_string": {
                      "query": "i",
                      "fields": ["content"]
                    }
                  }
                ]
              }
            }
          ],
          "must_not": [
            {
              "match": {
                "author_first_name": "Danny"
              }
            }
          ]
        }
      },
      "functions": [
        {
          "filter": {
            "match_all": {
              "boost": 1
            }
          },
          "field_value_factor": {
            "field": "follower_num",
            "factor": 1,
            "missing": 22.5,
            "modifier": "none"
          }
        }
      ]
    }
  },
  "_source": false,
 
  "aggs": {
    "userID_agg": {
      "terms": {
        "field": "userID",
        "size": 100
      }
    }
  }
}

```

**query2：**

```auto
GET /article/_search
{
  "from": 0,
  "size": 1000,
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "hidden": {
                  "value": "false"
                }
              }
            },
            {
              "match": {
                "new_author_last_name": "Smith"
              }
            },
            {
              "range": {
                "view_cnt": {
                  "gte": 1
                }
              }
            },
            {
              "bool": {
                "should": [
                  {
                    "simple_query_string": {
                      "query": "i",
                      "fields": ["content"]
                    }
                  }
                ]
              }
            }
          ],
          "must_not": [
            {
              "match": {
                "author_first_name": "Danny"
              }
            }
          ]
        }
      },
      "functions": [
        {
          "filter": {
            "match_all": {
              "boost": 1
            }
          },
          "field_value_factor": {
            "field": "follower_num",
            "factor": 1,
            "missing": 22.5,
            "modifier": "none"
          }
        }
      ]
    }
  },
  "_source": false,
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    },
    {
      "follower_num": {
        "order": "desc",
        "missing": 0
      }
    },
    {
      "view_cnt": {
        "order": "desc",
        "missing": 0
      }
    }
  ],
  "aggs": {
    "userID_agg": {
      "terms": {
        "field": "userID",
        "size": 100
      }
    }
  }, 
  
  "collapse": {
    "field": "userID"
  }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 4, 2021, 7:33am UTC](https://discuss.elastic.co/t/can-the-split-query-and-aggregations-achieve-higher-performance/278034/2 "2021-08-04T07:33:14Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
