How to use Search Contexts on Sourcegraph
Tags: codesearch, sourcegraph, search, webdev
Published: 2021-11-19 12:02:50

Sourcegraph prides itself in the ability to search millions of open source and private repositories. 

Many features enrich the developer's experience of using Sourcegraph code search. One of such features is **Search Contexts**.

In this article, you'll learn how to use search contexts on [Sourcegraph Cloud](https://sourcegraph.com)

## What are Search Contexts?

Search contexts are a set of repositories at specific revisions that search queries on Sourcegraph will target. In other words, search contexts enable you to narrow, group and search only the code repositories you care about. 

By default, Sourcegraph uses a global search context (namely, `context:global`) for all search queries, when no search context is specified by the actor (me, you, or a programmable robot) performing the search.


## Use cases for Search Contexts


### _First_

- In a day, I search many repositories for code. I have identified about 15 code repos that often come up repeatedly and return the desired search results. I want to group them so that my search query targets only this set of repos.

### _Second_

- Sequel to the first case, Sourcegraph allows me to search code repo branches. Instead of specifying the branch names in my query every time, I want to group specific branches of certain repos once to focus more on what I need to find than the semantics of finding it.

### _Third_

- My company has hundreds of repos, thousands of developers and several teams. Sourcegraph allows us to search all of these repos. However, we'd also like each team to be able to search only the code repos they actively manage.


## How to use Search Contexts

The first step is to go to [sourcegraph.com](https://sourcegraph.com). 

You’ll see a **context:global** selector at the left side of the search textbox. 

Every open-source repository that is added to [Sourcegraph Cloud](https://sourcegraph.com) is automatically assigned to the global search context.

![Global context](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9zybq8h07ny2fjsicpuj.png)

You can explore other available public search contexts by clicking the search contexts selector. A dropdown menu should appear as shown below:

![Dropdown of other contexts](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m9r5c2yb0d9grwnaumav.png)

Yaaaay!! More public search contexts. Here we have: [chakraui](https://sourcegraph.com/contexts/chakraui), [cncf](https://sourcegraph.com/contexts/cncf), [kubernetes](https://sourcegraph.com/contexts/kubernetes), [o3de](https://sourcegraph.com/contexts/o3de), [stackstorm](https://sourcegraph.com/contexts/stackstorm), [stanford](https://sourcegraph.com/contexts/stanford), and [temporalio](https://sourcegraph.com/contexts/temporalio) search contexts that anyone can use to explore public code.

Given a scenario where you want to search for code within the Kubernetes community, you can select the **kubernetes** search context and input your query syntax in the search textbox like so:

![Public search contexts](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ilj7biikt1klj43rqofu.png)

It only searches for code within the **Kubernetes** search context. One major benefit of search contexts is the swift response time you get while searching for anything.

**My Recommendation:** Explore the Stanford search context. A treasure of open source code from Stanford students, academics and professors!

You’re probably wondering how to create your search context. So let’s get into it!

**Note:** By the way, if you have added repositories to [Sourcegraph Cloud](https://sourcegraph.com), you already have a search context in this format: **context:@username**.

## How to create Search Contexts 

Follow these steps:

### Step 1

Sign up for a free account on [Sourcegraph.com](https://sourcegraph.com/sign-up). You can’t create a search context without an account.

### Step 2

Go to _Code Search_ in the top left navigation bar. On Hover, you’ll see a dropdown.

![Search Context drop down](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/36vptwehge9jkbkrgr49.png)

Click on the _Contexts_ menu item. It'll direct you to a page that shows all your existing search contexts (if you have others apart from the global search context).

![All search contexts](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9n3plwxy4ef7w4jtb7lw.png)

In the image above, you can see that I already have a **context:@unicodeveloper** search context because I added some private repositories to Sourcegraph a few months ago.

### Step 3

Click on the _Create search context_ button by the right hand side.
It will direct you to [https://sourcegraph.com/contexts/new](https://sourcegraph.com/contexts/new)

![Create new search context](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oipbdue52pum25dh64j9.png)

- Add a **name** and **description**.
- Choose the **visibility** of the context.

![Context visibility](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/64o7x5t2frrbgghnue87.png)

Now, add the repositories you want to be part of this context in the format above.

An example is this:

![Add repository to the context](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/flklkrbkoj1wh4lugutr.png)

**Note:** Remove the “https://” when adding the repository URL. 

- Test the configuration
- Go ahead and create the search context.

You will see your recently created search context. Mine is below:

![Eden life backend context](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vsly935o1pryw2pcly7s.png)

![All contexts](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/51un79afcpyndft1kma7.png)

Now, you can explore only the code you care about in this newly minted search context like so:

![Explore new search context](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/agbznm50vsx5ijo8sd3k.png)

 

**Note:** Don’t forget to press the context selector to switch to the global search context if you need to explore all the public code on Sourcegraph.


## Conclusion

As developers, our knowledge of refactoring code and identifying patterns come in handy in automating repeatable processes. Leverage your search context knowledge in taking your code search prowess to the next level.

Create as many contexts as possible to match your use case. If you are part of a team, invite them to enjoy the benefits of code search.

[Sourcegraph.com](https://sourcegraph.com) is not the only option to [add, explore private code](https://learn.sourcegraph.com/how-to-add-private-code-repositories-to-sourcegraph) and use search contexts. Sourcegraph can be installed in different ways to set up a deployment for your private code. 

You can run [Sourcegraph locally](https://docs.sourcegraph.com/admin/install#self-hosted) or opt-in for us to set up a [managed Sourcegraph instance](https://docs.sourcegraph.com/admin/install/managed) for your team. You'll be able to use a self-hosted Sourcegraph instance for free (up to 10-users).

---
Have suggestions or questions? Leave a comment, or join our [Community Slack Space](https://about.sourcegraph.com/community/?utm_medium=social&utm_source=devto&utm_campaign=slacklaunch) where our team will be happy to answer any questions you may have about Sourcegraph.