# Inconsistent hybrid search hits total results lead to incorrect aggregations

**URL:** <https://discuss.elastic.co/t/inconsistent-hybrid-search-hits-total-results-lead-to-incorrect-aggregations/370196>\
**Category:** Elasticsearch\
**Tags:** vector-search\
**Created:** [November 8, 2024, 4:59am UTC](https://discuss.elastic.co/t/inconsistent-hybrid-search-hits-total-results-lead-to-incorrect-aggregations/370196 "2024-11-08T04:59:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrewquang512](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrewquang512/32/139031_2.png) [@andrewquang512](https://discuss.elastic.co/u/andrewquang512)\
**Post date:** [November 8, 2024, 4:59am UTC](https://discuss.elastic.co/t/inconsistent-hybrid-search-hits-total-results-lead-to-incorrect-aggregations/370196/1 "2024-11-08T04:59:32Z")

</div>

Hi All,

My team is working on system using hybrid search combining knn search and full-text queries. There is a usecase that we need to count a tag field data which we are using [Term Aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html) and display these counts on UI and support filtering result by tag.

And then we found that in this guide [Aggregation with kNN](https://www.elastic.co/guide/en/elasticsearch/reference/current/knn-search.html#_combine_approximate_knn_with_other_features) which state that for approximate kNN search, aggregations are calculated on the top `k` nearest documents - this may messed up the aggregation counting results as the kNN search will make sure that `k` matching documents with tag-filter are returned - which make the aggregation of first search request without filtering incorrect

We do some research and try to apply [kNN query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-knn-query.html#knn-query-aggregations) and although it may sounds matching our needs, but the hits total results are inconsistent between search requests with the same request body - which lead to aggregation results are incorrect too.

 ![search count results](https://us1.discourse-cdn.com/elastic/original/3X/3/2/32ec91bc58e0995f5c6b6a119d5d154f9dcc98e3.png)

We don't understand this behavior, we find that when we keep only one vector knn search or small size , this will not happen again.

Can your guys help me explain what is happening in background here and any solutions/workarounds to implement term aggregation and filter by it with hybrid search. Any suggestions or opinions are appreciated. Thank you.

Below are my queries;

This is hybrid search with kNN

```auto
GET product_books/_search
{
  "size": 200,
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "match_phrase": {
                  "product_title": {
                    "query": "Hello World",
                    "boost": 3.27
                  }
                }
              },
              {
                "match_phrase": {
                  "product_title_kana": {
                    "query": "Hello World",
                    "boost": 2.84
                  }
                }
              },
              {
                "match_phrase": {
                  "author_1": {
                    "query": "Hello World",
                    "boost": 2.44
                  }
                }
              },
              {
                "match_phrase": {
                  "author_1_kana": {
                    "query": "Hello World",
                    "boost": 2.3
                  }
                }
              },
              {
                "match_phrase": {
                  "author_2": {
                    "query": "Hello World",
                    "boost": 2.44
                  }
                }
              },
              {
                "match_phrase": {
                  "author_2_kana": {
                    "query": "Hello World",
                    "boost": 2.3
                  }
                }
              },
              {
                "match_phrase": {
                  "search_text.search_text": {
                    "query": "Hello World",
                    "boost": 1.04
                  }
                }
              },
              {
                "match_phrase": {
                  "search_text.search_ngram": {
                    "query": "Hello World",
                    "boost": 0.24
                  }
                }
              },
              {
                "match_phrase": {
                  "search_text.search_ngram_norm": {
                    "query": "Hello World",
                    "boost": 0.17
                  }
                }
              },
              {
                "match_phrase_prefix": {
                  "product_title": {
                    "query": "Hello World",
                    "boost": 1.64
                  }
                }
              },
              {
                "match_phrase_prefix": {
                  "product_title_kana": {
                    "query": "Hello World",
                    "boost": 1.44
                  }
                }
              },
              {
                "match_phrase_prefix": {
                  "author_1": {
                    "query": "Hello World",
                    "boost": 1.2
                  }
                }
              },
              {
                "match_phrase_prefix": {
                  "author_1_kana": {
                    "query": "Hello World",
                    "boost": 1.14
                  }
                }
              },
              {
                "match_phrase_prefix": {
                  "author_2": {
                    "query": "Hello World",
                    "boost": 1.2
                  }
                }
              },
              {
                "match_phrase_prefix": {
                  "author_2_kana": {
                    "query": "Hello World",
                    "boost": 1.14
                  }
                }
              },
              {
                "match_phrase_prefix": {
                  "search_text.search_text": {
                    "query": "Hello World",
                    "boost": 0.5
                  }
                }
              },
              {
                "match_phrase_prefix": {
                  "search_text.search_ngram": {
                    "query": "Hello World",
                    "boost": 0.14
                  }
                }
              },
              {
                "match_phrase_prefix": {
                  "search_text.search_ngram_norm": {
                    "query": "Hello World",
                    "boost": 0.1
                  }
                }
              },
              {
                "match": {
                  "product_title": {
                    "query": "Hello World",
                    "boost": 0.67
                  }
                }
              },
              {
                "match": {
                  "product_title_kana": {
                    "query": "Hello World",
                    "boost": 0.57
                  }
                }
              },
              {
                "match": {
                  "author_1": {
                    "query": "Hello World",
                    "boost": 0.5
                  }
                }
              },
              {
                "match": {
                  "author_1_kana": {
                    "query": "Hello World",
                    "boost": 0.47
                  }
                }
              },
              {
                "match": {
                  "author_2": {
                    "query": "Hello World",
                    "boost": 0.5
                  }
                }
              },
              {
                "match": {
                  "author_2_kana": {
                    "query": "Hello World",
                    "boost": 0.47
                  }
                }
              },
              {
                "match": {
                  "search_text.search_text": {
                    "query": "Hello World",
                    "boost": 0.2
                  }
                }
              },
              {
                "match": {
                  "search_text.search_ngram": {
                    "query": "Hello World",
                    "boost": 0.04
                  }
                }
              },
              {
                "match": {
                  "search_text.search_ngram_norm": {
                    "query": "Hello World",
                    "boost": 0.03
                  }
                }
              },
              {
                "term": {
                  "jan": {
                    "value": "Hello World",
                    "boost": 3.27
                  }
                }
              },
              {
                "term": {
                  "isbn_10": {
                    "value": "Hello World",
                    "boost": 3.27
                  }
                }
              }
            ],
            "minimum_should_match": 6
          }
        }
      ],
      "filter": [
        {
          "term": {
            "tags": "コミック"
          }
        }
      ]
    }
  },
  "knn": [
    {
      "field": "semantic_description_vector",
      "query_vector_builder": {
        "text_embedding": {
          "model_id": "intfloat__multilingual-e5-base_query",
          "model_text": "Hello World"
        }
      },
      "k": 15,
      "num_candidates": 50,
      "boost": 28,
      "filter": [
        {
          "term": {
            "tags": "コミック"
          }
        }
      ]
    },
    {
      "field": "semantic_description_vector",
      "query_vector_builder": {
        "text_embedding": {
          "model_id": "intfloat__multilingual-e5-base_query",
          "model_text": "Hello World"
        }
      },
      "k": 15,
      "num_candidates": 50,
      "boost": 28,
      "filter": [
        {
          "term": {
            "tags": "コミック"
          }
        }
      ]
    },
    {
      "field": "semantic_metadata_vector",
      "query_vector_builder": {
        "text_embedding": {
          "model_id": "intfloat__multilingual-e5-base_query",
          "model_text": "Hello World"
        }
      },
      "k": 15,
      "num_candidates": 50,
      "boost": 28,
      "filter": [
        {
          "term": {
            "tags": "コミック"
          }
        }
      ]
    }
  ]
}

```

This is hybrid search with query DSL kNN query :

```auto
GET product_books/_search
{
  "size": 200,
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "bool": {
                  "should": [
                    {
                      "match_phrase": {
                        "product_title": {
                          "query": "Hello World",
                          "boost": 3.27
                        }
                      }
                    },
                    {
                      "match_phrase": {
                        "product_title_kana": {
                          "query": "Hello World",
                          "boost": 2.84
                        }
                      }
                    },
                    {
                      "match_phrase": {
                        "author_1": {
                          "query": "Hello World",
                          "boost": 2.44
                        }
                      }
                    },
                    {
                      "match_phrase": {
                        "author_1_kana": {
                          "query": "Hello World",
                          "boost": 2.3
                        }
                      }
                    },
                    {
                      "match_phrase": {
                        "author_2": {
                          "query": "Hello World",
                          "boost": 2.44
                        }
                      }
                    },
                    {
                      "match_phrase": {
                        "author_2_kana": {
                          "query": "Hello World",
                          "boost": 2.3
                        }
                      }
                    },
                    {
                      "match_phrase": {
                        "search_text.search_text": {
                          "query": "Hello World",
                          "boost": 1.04
                        }
                      }
                    },
                    {
                      "match_phrase": {
                        "search_text.search_ngram": {
                          "query": "Hello World",
                          "boost": 0.24
                        }
                      }
                    },
                    {
                      "match_phrase": {
                        "search_text.search_ngram_norm": {
                          "query": "Hello World",
                          "boost": 0.17
                        }
                      }
                    },
                    {
                      "match_phrase_prefix": {
                        "product_title": {
                          "query": "Hello World",
                          "boost": 1.64
                        }
                      }
                    },
                    {
                      "match_phrase_prefix": {
                        "product_title_kana": {
                          "query": "Hello World",
                          "boost": 1.44
                        }
                      }
                    },
                    {
                      "match_phrase_prefix": {
                        "author_1": {
                          "query": "Hello World",
                          "boost": 1.2
                        }
                      }
                    },
                    {
                      "match_phrase_prefix": {
                        "author_1_kana": {
                          "query": "Hello World",
                          "boost": 1.14
                        }
                      }
                    },
                    {
                      "match_phrase_prefix": {
                        "author_2": {
                          "query": "Hello World",
                          "boost": 1.2
                        }
                      }
                    },
                    {
                      "match_phrase_prefix": {
                        "author_2_kana": {
                          "query": "Hello World",
                          "boost": 1.14
                        }
                      }
                    },
                    {
                      "match_phrase_prefix": {
                        "search_text.search_text": {
                          "query": "Hello World",
                          "boost": 0.5
                        }
                      }
                    },
                    {
                      "match_phrase_prefix": {
                        "search_text.search_ngram": {
                          "query": "Hello World",
                          "boost": 0.14
                        }
                      }
                    },
                    {
                      "match_phrase_prefix": {
                        "search_text.search_ngram_norm": {
                          "query": "Hello World",
                          "boost": 0.1
                        }
                      }
                    },
                    {
                      "match": {
                        "product_title": {
                          "query": "Hello World",
                          "boost": 0.67
                        }
                      }
                    },
                    {
                      "match": {
                        "product_title_kana": {
                          "query": "Hello World",
                          "boost": 0.57
                        }
                      }
                    },
                    {
                      "match": {
                        "author_1": {
                          "query": "Hello World",
                          "boost": 0.5
                        }
                      }
                    },
                    {
                      "match": {
                        "author_1_kana": {
                          "query": "Hello World",
                          "boost": 0.47
                        }
                      }
                    },
                    {
                      "match": {
                        "author_2": {
                          "query": "Hello World",
                          "boost": 0.5
                        }
                      }
                    },
                    {
                      "match": {
                        "author_2_kana": {
                          "query": "Hello World",
                          "boost": 0.47
                        }
                      }
                    },
                    {
                      "match": {
                        "search_text.search_text": {
                          "query": "Hello World",
                          "boost": 0.2
                        }
                      }
                    },
                    {
                      "match": {
                        "search_text.search_ngram": {
                          "query": "Hello World",
                          "boost": 0.04
                        }
                      }
                    },
                    {
                      "match": {
                        "search_text.search_ngram_norm": {
                          "query": "Hello World",
                          "boost": 0.03
                        }
                      }
                    },
                    {
                      "term": {
                        "jan": {
                          "value": "Hello World",
                          "boost": 3.27
                        }
                      }
                    },
                    {
                      "term": {
                        "isbn_10": {
                          "value": "Hello World",
                          "boost": 3.27
                        }
                      }
                    }
                  ],
                  "minimum_should_match": 6
                }
              }
            ]
          }
        },
        {
          "knn": {
            "field": "semantic_title_vector",
            "query_vector_builder": {
              "text_embedding": {
                "model_id": "intfloat__multilingual-e5-base_query",
                "model_text": "query: Hello World"
              }
            },
            "_name": "knn_query"
          }
        },
        {
          "knn": {
            "field": "semantic_description_vector",
            "query_vector_builder": {
              "text_embedding": {
                "model_id": "intfloat__multilingual-e5-base_query",
                "model_text": "query: Hello World"
              }
            },
            "_name": "knn_query"
          }
        },
        {
          "knn": {
            "field": "semantic_metadata_vector",
            "query_vector_builder": {
              "text_embedding": {
                "model_id": "intfloat__multilingual-e5-base_query",
                "model_text": "query: Hello World"
              }
            },
            "_name": "knn_query"
          }
        }
      ]
    }
  },
  "aggs": {
    "tags": {
      "terms": {
        "field": "tags",
        "size": 100
      }
    }
  }
}

```

This pretty much the same to this topic [Inconsistency in No of Total Hits](https://discuss.elastic.co/t/inconsistency-in-no-of-total-hits-in-results/258023) but this is hybrid search to our usecase

For Elasticsearch version, we are using ElasticCloud with version 8.14.3 of Elasticsearch

---

<div class="post-metadata">

**Author:** ![Fabrizio\_Fortino](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fabrizio_fortino/32/1719_2.png) [@Fabrizio\_Fortino](https://discuss.elastic.co/u/Fabrizio_Fortino)\
**Post date:** [November 14, 2024, 5:50pm UTC](https://discuss.elastic.co/t/inconsistent-hybrid-search-hits-total-results-lead-to-incorrect-aggregations/370196/2 "2024-11-14T17:50:57Z")

</div>

I am experiencing the same behaviour with hybrid queries and ES 8.15 (using ElasticCloud as well). The knn query clause specifies numCandidates=100. Query executions range between 99 and 101 hits.

---

<div class="post-metadata">

**Author:** ![andrewquang512](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrewquang512/32/139031_2.png) [@andrewquang512](https://discuss.elastic.co/u/andrewquang512)\
**Post date:** [November 18, 2024, 7:38am UTC](https://discuss.elastic.co/t/inconsistent-hybrid-search-hits-total-results-lead-to-incorrect-aggregations/370196/3 "2024-11-18T07:38:45Z")

</div>

Yes, this is a bit confusing behavior as far as i know knn will make ES choosing `num_candidates` from each shard during the search and choose the top `k` results. For the same keyword, the `num_candidates` should remain the same and thus the results are consistent.

I have just find out that the knn top-level section results will be also inconsistent when putting high `num_candidates` and `k`. But not as much as the knn query, the result from knn top-level section fluctuates around 1-2 documents.

For the aggregation, we figure out the way to manage to do this is that we are using [Post Filter](https://www.elastic.co/guide/en/elasticsearch/reference/current/filter-search-results.html) - it took us a while to figure this docs - I think this section should be included in [KNN search filter](https://www.elastic.co/guide/en/elasticsearch/reference/current/knn-search.html#knn-search-filter-example) in case any ones are struggling with the same problem we have met.
