Send Zoekt query at top level instead of per node

What does this MR do and why?

Moves the Zoekt search query to the top level of the request payload (behind the zoekt_top_level_query feature flag) instead of duplicating it in every forward_to node entry.

Previously, CodeQueryBuilder was invoked once per node and the query embedded in each entry. This change:

  • Memoizes built_query so CodeQueryBuilder is called once regardless of node count
  • Hoists the query to the top-level payload when zoekt_top_level_query is enabled, with nodes receiving only their endpoint
  • Introduces top_level_query? which encapsulates both the traversal ID and feature flag conditions together

References

Screenshots or screen recordings

N/A — backend only

How to set up and validate locally

  1. Enable the feature flag: Feature.enable(:zoekt_top_level_query)
  2. Perform a Zoekt code search and verify the query appears at the top level of the request payload (visible via the performance bar)

image

zoekt query before

{
  "version": 2,
  "timeout": "120s",
  "num_context_lines": 1,
  "max_file_match_window": 0,
  "max_file_match_results": 5000,
  "max_line_match_window": 5000,
  "max_line_match_results": 0,
  "max_line_match_results_per_file": 50,
  "forward_to": [
    {
      "endpoint": "http://localhost:6091",
      "query": {
        "and": {
          "children": [
            {
              "query_string": {
                "query": "case:no po"
              }
            },
            {
              "meta": {
                "key": "archived",
                "value": "f"
              }
            },
            {
              "or": {
                "children": [
                  {
                    "meta": {
                      "key": "repository_access_level",
                      "value": "20|10"
                    },
                    "_context": {
                      "name": "admin_branch"
                    }
                  }
                ]
              },
              "_context": {
                "name": "access_branches"
              }
            }
          ]
        }
      }
    },
    {
      "endpoint": "http://localhost:6090",
      "query": {
        "and": {
          "children": [
            {
              "query_string": {
                "query": "case:no po"
              }
            },
            {
              "meta": {
                "key": "archived",
                "value": "f"
              }
            },
            {
              "or": {
                "children": [
                  {
                    "meta": {
                      "key": "repository_access_level",
                      "value": "20|10"
                    },
                    "_context": {
                      "name": "admin_branch"
                    }
                  }
                ]
              },
              "_context": {
                "name": "access_branches"
              }
            }
          ]
        }
      }
    }
  ]
}

zoekt query after

{
  "version": 2,
  "timeout": "120s",
  "num_context_lines": 1,
  "max_file_match_window": 0,
  "max_file_match_results": 5000,
  "max_line_match_window": 5000,
  "max_line_match_results": 0,
  "max_line_match_results_per_file": 50,
  "forward_to": [
    {
      "endpoint": "http://localhost:6091"
    },
    {
      "endpoint": "http://localhost:6090"
    }
  ],
  "query": {
    "and": {
      "children": [
        {
          "query_string": {
            "query": "case:no contribute"
          }
        },
        {
          "meta": {
            "key": "archived",
            "value": "f"
          }
        },
        {
          "or": {
            "children": [
              {
                "meta": {
                  "key": "repository_access_level",
                  "value": "20|10"
                },
                "_context": {
                  "name": "admin_branch"
                }
              }
            ]
          },
          "_context": {
            "name": "access_branches"
          }
        }
      ]
    }
  }
}

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Terri Chu

Merge request reports

Loading