Code Search Results - Fetch Code Search Results

Provides a set of results for the search text.

POST https://almsearch.dev.azure.com/{organization}/{project}/_apis/search/codesearchresults?api-version=7.1-preview.1

URI Parameters

Name In Required Type Description
organization
path True

string

The name of the Azure DevOps organization.

project
path

string

Project ID or project name

api-version
query True

string

Version of the API to use. This should be set to '7.1-preview.1' to use this version of the api.

Request Body

Name Type Description
$orderBy

SortOption[]

Options for sorting search results. If set to null, the results will be returned sorted by relevance. If more than one sort option is provided, the results are sorted in the order specified in the OrderBy.

$skip

integer

Number of results to be skipped.

$top

integer

Number of results to be returned.

filters

object

Filters to be applied. Set it to null if there are no filters to be applied.

includeFacets

boolean

Flag to opt for faceting in the result. Default behavior is false.

includeSnippet

boolean

Flag to opt for including matched code snippet in the result. Default behavior is false.

searchText

string

The search text.

Responses

Name Type Description
200 OK

CodeSearchResponse

successful operation

Security

oauth2

Type: oauth2
Flow: accessCode
Authorization URL: https://app.vssps.visualstudio.com/oauth2/authorize&response_type=Assertion
Token URL: https://app.vssps.visualstudio.com/oauth2/token?client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer&grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer

Scopes

Name Description
vso.code Grants the ability to read source code and metadata about commits, changesets, branches, and other version control artifacts. Also grants the ability to search code and get notified about version control events via service hooks.

Examples

Sample Request

POST https://almsearch.dev.azure.com/fabrikam/_apis/search/codesearchresults?api-version=7.1-preview.1

{
  "searchText": "CodeSearchController",
  "$skip": 0,
  "$top": 1,
  "filters": {
    "Project": [
      "MyFirstProject"
    ],
    "Repository": [
      "MyFirstProject"
    ],
    "Path": [
      "/"
    ],
    "Branch": [
      "master"
    ],
    "CodeElement": [
      "def",
      "class"
    ]
  },
  "$orderBy": [
    {
      "field": "filename",
      "sortOrder": "ASC"
    }
  ],
  "includeFacets": true
}

Sample Response

{
  "count": 1,
  "results": [
    {
      "fileName": "CodeSearchController.cs",
      "path": "/CodeSearchController.cs",
      "matches": {
        "content": [
          {
            "charOffset": 1187,
            "length": 20
          },
          {
            "charOffset": 1395,
            "length": 20
          },
          {
            "charOffset": 1686,
            "length": 20
          }
        ],
        "fileName": [
          {
            "charOffset": 0,
            "length": -1
          }
        ]
      },
      "collection": {
        "name": "DefaultCollection"
      },
      "project": {
        "name": "MyFirstProject",
        "id": "00000000-0000-0000-0000-000000000000"
      },
      "repository": {
        "name": "MyFirstProject",
        "id": "c1548045-29f6-4354-8114-55ef058be1a3",
        "type": "git"
      },
      "versions": [
        {
          "branchName": "master",
          "changeId": "47e1cc8877baea4b7bb33af803d6cc697914f88b"
        }
      ],
      "contentId": "004898f1ad91c9c2a0f492f2d1174468bc3c84ef"
    }
  ],
  "infoCode": 0,
  "facets": {
    "Project": [
      {
        "name": "MyFirstProject",
        "id": "MyFirstProject",
        "resultCount": 1
      }
    ],
    "Repository": [
      {
        "name": "MyFirstProject",
        "id": "MyFirstProject",
        "resultCount": 1
      }
    ],
    "CodeElement": [
      {
        "name": "Class",
        "id": "class",
        "resultCount": 1
      },
      {
        "name": "Comment",
        "id": "comment",
        "resultCount": 1
      },
      {
        "name": "Definition",
        "id": "def",
        "resultCount": 1
      }
    ]
  }
}

Definitions

Name Description
CodeResult

Defines the code result containing information of the searched files and its metadata.

CodeSearchRequest

Defines a code search request.

CodeSearchResponse

Defines a code search response item.

Collection

Defines the details of the collection.

Project

Defines the details of the project.

Repository

Defines the details of the repository.

SortOption

Defines how to sort the result.

Version

Describes the details pertaining to a version of the result file.

VersionControlType

Version control type of the result file.

CodeResult

Defines the code result containing information of the searched files and its metadata.

Name Type Description
collection

Collection

Collection of the result file.

contentId

string

ContentId of the result file.

fileName

string

Name of the result file.

matches

object

Dictionary of field to hit offsets in the result file. Key identifies the area in which hits were found, for ex: file content/file name etc.

path

string

Path at which result file is present.

project

Project

Project of the result file.

repository

Repository

Repository of the result file.

versions

Version[]

Versions of the result file.

CodeSearchRequest

Defines a code search request.

Name Type Description
$orderBy

SortOption[]

Options for sorting search results. If set to null, the results will be returned sorted by relevance. If more than one sort option is provided, the results are sorted in the order specified in the OrderBy.

$skip

integer

Number of results to be skipped.

$top

integer

Number of results to be returned.

filters

object

Filters to be applied. Set it to null if there are no filters to be applied.

includeFacets

boolean

Flag to opt for faceting in the result. Default behavior is false.

includeSnippet

boolean

Flag to opt for including matched code snippet in the result. Default behavior is false.

searchText

string

The search text.

CodeSearchResponse

Defines a code search response item.

Name Type Description
count

integer

Total number of matched files.

facets

object

A dictionary storing an array of Filter object against each facet.

infoCode

integer

Numeric code indicating any additional information: 0 - Ok, 1 - Account is being reindexed, 2 - Account indexing has not started, 3 - Invalid Request, 4 - Prefix wildcard query not supported, 5 - MultiWords with code facet not supported, 6 - Account is being onboarded, 7 - Account is being onboarded or reindexed, 8 - Top value trimmed to maxresult allowed 9 - Branches are being indexed, 10 - Faceting not enabled, 11 - Work items not accessible, 19 - Phrase queries with code type filters not supported, 20 - Wildcard queries with code type filters not supported. Any other info code is used for internal purpose.

results

CodeResult[]

List of matched files.

Collection

Defines the details of the collection.

Name Type Description
name

string

Name of the collection.

Project

Defines the details of the project.

Name Type Description
id

string

Id of the project.

name

string

Name of the project.

Repository

Defines the details of the repository.

Name Type Description
id

string

Id of the repository.

name

string

Name of the repository.

type

VersionControlType

Version control type of the result file.

SortOption

Defines how to sort the result.

Name Type Description
field

string

Field name on which sorting should be done.

sortOrder

string

Order (ASC/DESC) in which the results should be sorted.

Version

Describes the details pertaining to a version of the result file.

Name Type Description
branchName

string

Name of the branch.

changeId

string

ChangeId in the given branch associated with this match.

VersionControlType

Version control type of the result file.

Name Type Description
custom

string

For internal use.

git

string

tfvc

string