Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query: Adds hybrid search query pipeline stage #4794

Merged
merged 15 commits into from
Oct 18, 2024

Conversation

neildsh
Copy link
Contributor

@neildsh neildsh commented Oct 10, 2024

Description

Adds hybrid search query pipeline stage. This requires the new Direct package and gateway to be available in order to light up.

Given an input SQL such as:

      SELECT TOP 100 c.text, c.abstract
      FROM c
      ORDER BY RANK RRF(FullTextScore(c.text, ['swim', 'run']), FullTextScore(c.abstract, ['energy']))

The new query plan (encoded below as XML instead of JSON to help readability) is as follows:

        <queryRanges>
          <Item>{"min":[],"max":"Infinity","isMinInclusive":true,"isMaxInclusive":false}</Item>
        </queryRanges>
        <hybridSearchQueryInfo>
          <globalStatisticsQuery><![CDATA[
SELECT 
    COUNT(1) AS documentCount,
    [
        {
            totalWordCount: SUM(_FullTextWordCount(c.text)),
            hitCounts: [
                COUNTIF(FullTextContains(c.text, "swim")),
                COUNTIF(FullTextContains(c.text, "run"))
            ]
        },
        {
            totalWordCount: SUM(_FullTextWordCount(c.abstract)),
            hitCounts: [
                COUNTIF(FullTextContains(c.abstract, "energy"))
            ]
        }
    ] AS fullTextStatistics
FROM c
]]></globalStatisticsQuery>
          <componentQueryInfos>
            <Item>
              <distinctType>None</distinctType>
              <top>200</top>
              <orderBy>
                <Item>Descending</Item>
              </orderBy>
              <orderByExpressions>
                <Item>_FullTextScore(c.text, ["swim", "run"], {documentdb-formattablehybridsearchquery-totaldocumentcount}, {documentdb-formattablehybridsearchquery-totalwordcount-0}, {documentdb-formattablehybridsearchquery-hitcountsarray-0})</Item>
              </orderByExpressions>
              <hasSelectValue>false</hasSelectValue>
              <rewrittenQuery><![CDATA[
SELECT TOP 200 
    c._rid,
    [
        {
            item: _FullTextScore(c.text, ["swim", "run"], {documentdb-formattablehybridsearchquery-totaldocumentcount}, {documentdb-formattablehybridsearchquery-totalwordcount-0}, {documentdb-formattablehybridsearchquery-hitcountsarray-0})
        }
    ] AS orderByItems,
    {
        payload: {
            text: c.text,
            abstract: c.abstract
        },
        componentScores: [
            _FullTextScore(c.text, ["swim", "run"], {documentdb-formattablehybridsearchquery-totaldocumentcount}, {documentdb-formattablehybridsearchquery-totalwordcount-0}, {documentdb-formattablehybridsearchquery-hitcountsarray-0}),
            _FullTextScore(c.abstract, ["energy"], {documentdb-formattablehybridsearchquery-totaldocumentcount}, {documentdb-formattablehybridsearchquery-totalwordcount-1}, {documentdb-formattablehybridsearchquery-hitcountsarray-1})
        ]
    } AS payload
FROM c
WHERE {documentdb-formattableorderbyquery-filter}
ORDER BY _FullTextScore(c.text, ["swim", "run"], {documentdb-formattablehybridsearchquery-totaldocumentcount}, {documentdb-formattablehybridsearchquery-totalwordcount-0}, {documentdb-formattablehybridsearchquery-hitcountsarray-0}) DESC
]]></rewrittenQuery>
              <hasNonStreamingOrderBy>true</hasNonStreamingOrderBy>
            </Item>
            <Item>
              <distinctType>None</distinctType>
              <top>200</top>
              <orderBy>
                <Item>Descending</Item>
              </orderBy>
              <orderByExpressions>
                <Item>_FullTextScore(c.abstract, ["energy"], {documentdb-formattablehybridsearchquery-totaldocumentcount}, {documentdb-formattablehybridsearchquery-totalwordcount-1}, {documentdb-formattablehybridsearchquery-hitcountsarray-1})</Item>
              </orderByExpressions>
              <hasSelectValue>false</hasSelectValue>
              <rewrittenQuery><![CDATA[
SELECT TOP 200 
    c._rid,
    [
        {
            item: _FullTextScore(c.abstract, ["energy"], {documentdb-formattablehybridsearchquery-totaldocumentcount}, {documentdb-formattablehybridsearchquery-totalwordcount-1}, {documentdb-formattablehybridsearchquery-hitcountsarray-1})
        }
    ] AS orderByItems,
    {
        payload: {
            text: c.text,
            abstract: c.abstract
        },
        componentScores: [
            _FullTextScore(c.text, ["swim", "run"], {documentdb-formattablehybridsearchquery-totaldocumentcount}, {documentdb-formattablehybridsearchquery-totalwordcount-0}, {documentdb-formattablehybridsearchquery-hitcountsarray-0}),
            _FullTextScore(c.abstract, ["energy"], {documentdb-formattablehybridsearchquery-totaldocumentcount}, {documentdb-formattablehybridsearchquery-totalwordcount-1}, {documentdb-formattablehybridsearchquery-hitcountsarray-1})
        ]
    } AS payload
FROM c
WHERE {documentdb-formattableorderbyquery-filter}
ORDER BY _FullTextScore(c.abstract, ["energy"], {documentdb-formattablehybridsearchquery-totaldocumentcount}, {documentdb-formattablehybridsearchquery-totalwordcount-1}, {documentdb-formattablehybridsearchquery-hitcountsarray-1}) DESC
]]></rewrittenQuery>
              <hasNonStreamingOrderBy>true</hasNonStreamingOrderBy>
            </Item>
          </componentQueryInfos>
          <take>100</take>
          <requiresGlobalStatistics>true</requiresGlobalStatistics>
        </hybridSearchQueryInfo>

We have a custom implementation for the global statistics inside the HybridSearchCrossPartitionQueryPipelineStage because it uses nested aggregates. Each of the component queries in the hybrid search query plan is cross partition, and we run them using the existing cross partition query pipelines.

Note the use of placeholders such as {documentdb-formattablehybridsearchquery-totaldocumentcount} in the query plan. These need to be replaced by the global statistics.

Type of change

  • New feature (non-breaking change which adds functionality)

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good!

@neildsh neildsh changed the title Query: Add hybrid search query pipeline stage Query: Adds hybrid search query pipeline stage Oct 10, 2024
@neildsh neildsh marked this pull request as ready for review October 15, 2024 01:57
first draft of RRF implementation

add code for paginating results and respect skip/take
fix build errors

Add more integration tests with better validation

Add more integration tests with better validation

deleted gratuitous rewrite of sql query spec

fix up typo that causes build break

Fix up build break in OrderByPipelineStageBenchmark
@sboshra
Copy link
Contributor

sboshra commented Oct 16, 2024

        {

disable ODE for hybrid search queries #Pending


Refers to: Microsoft.Azure.Cosmos/src/Query/Core/Pipeline/CosmosQueryExecutionContextFactory.cs:282 in 458b10c. [](commit_id = 458b10c, deletion_comment = False)

sboshra
sboshra previously approved these changes Oct 17, 2024
Copy link
Contributor

@sboshra sboshra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

Rename a couple of Hybrid Search methods to conform to code review feedback
Update comment to be more helpful
Tiny bit of clean up
@neildsh neildsh added QUERY auto-merge Enables automation to merge PRs labels Oct 18, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot merged commit 4e1c033 into master Oct 18, 2024
24 checks passed
@microsoft-github-policy-service microsoft-github-policy-service bot deleted the users/ndeshpan/hybridSearch branch October 18, 2024 03:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-merge Enables automation to merge PRs QUERY
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

3 participants