Skip to content

Commit

Permalink
api: fuzzy search results include job name with id in scope
Browse files Browse the repository at this point in the history
  • Loading branch information
shoenig committed Apr 16, 2021
1 parent ab92667 commit 068fd43
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion nomad/search_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func (*Search) fuzzyMatchesJob(j *structs.Job, text string) map[structs.Context]

// job.name
if idx := fuzzyIndex(j.Name, text); idx >= 0 {
sm[structs.Jobs] = append(sm[structs.Jobs], score(job, ns, idx))
sm[structs.Jobs] = append(sm[structs.Jobs], score(j.Name, ns, idx, job))
}

// job|group.name
Expand Down
22 changes: 11 additions & 11 deletions nomad/search_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1026,8 +1026,8 @@ func TestSearch_FuzzySearch_ACL(t *testing.T) {
require.NoError(t, msgpackrpc.CallWithCodec(codec, "Search.FuzzySearch", req, &resp))
require.Len(t, resp.Matches[structs.Jobs], 1)
require.Equal(t, structs.FuzzyMatch{
ID: job.ID,
Scope: []string{"default"},
ID: "my-job",
Scope: []string{"default", job.ID},
}, resp.Matches[structs.Jobs][0])

// Index of job - not node - because node context is filtered out
Expand All @@ -1046,7 +1046,7 @@ func TestSearch_FuzzySearch_ACL(t *testing.T) {
require.Equal(t, uint64(1001), resp.Index)
require.Len(t, resp.Matches[structs.Jobs], 1)
require.Equal(t, structs.FuzzyMatch{
ID: job.ID, Scope: []string{"default"},
ID: job.Name, Scope: []string{"default", job.ID},
}, resp.Matches[structs.Jobs][0])
require.Len(t, resp.Matches[structs.Nodes], 1)
require.Equal(t, structs.FuzzyMatch{
Expand Down Expand Up @@ -1559,7 +1559,7 @@ func TestSearch_FuzzySearch_Namespace_ACL(t *testing.T) {
var resp structs.FuzzySearchResponse
require.NoError(t, msgpackrpc.CallWithCodec(codec, "Search.FuzzySearch", req, &resp))
require.Len(t, resp.Matches[structs.Jobs], 1)
require.Equal(t, job2.ID, resp.Matches[structs.Jobs][0].ID)
require.Equal(t, job2.Name, resp.Matches[structs.Jobs][0].ID)

// Index of job - not node - because node context is filtered out
require.Equal(t, uint64(504), resp.Index)
Expand All @@ -1578,7 +1578,7 @@ func TestSearch_FuzzySearch_Namespace_ACL(t *testing.T) {
require.NoError(t, msgpackrpc.CallWithCodec(codec, "Search.FuzzySearch", req, &resp))
require.Equal(t, uint64(1001), resp.Index)
require.Len(t, resp.Matches[structs.Jobs], 1)
require.Equal(t, job1.ID, resp.Matches[structs.Jobs][0].ID)
require.Equal(t, job1.Name, resp.Matches[structs.Jobs][0].ID)
require.Len(t, resp.Matches[structs.Nodes], 1)
require.Len(t, resp.Matches[structs.Namespaces], 1) // matches "team-job-app"
}
Expand Down Expand Up @@ -1710,7 +1710,7 @@ func TestSearch_FuzzySearch_MultiNamespace_ACL(t *testing.T) {
var resp structs.FuzzySearchResponse
require.NoError(t, msgpackrpc.CallWithCodec(codec, "Search.FuzzySearch", req, &resp))
require.Len(t, resp.Matches[structs.Jobs], 1)
require.Equal(t, job2.ID, resp.Matches[structs.Jobs][0].ID)
require.Equal(t, job2.Name, resp.Matches[structs.Jobs][0].ID)

// Nodes filtered out since token only has access to namespace:read-job
require.Len(t, resp.Matches[structs.Nodes], 0)
Expand All @@ -1732,8 +1732,8 @@ func TestSearch_FuzzySearch_MultiNamespace_ACL(t *testing.T) {
var resp structs.FuzzySearchResponse
require.NoError(t, msgpackrpc.CallWithCodec(codec, "Search.FuzzySearch", req, &resp))
require.Len(t, resp.Matches[structs.Jobs], 2)
require.Equal(t, job2.ID, resp.Matches[structs.Jobs][0].ID)
require.Equal(t, job3.ID, resp.Matches[structs.Jobs][1].ID)
require.Equal(t, job2.Name, resp.Matches[structs.Jobs][0].ID)
require.Equal(t, job3.Name, resp.Matches[structs.Jobs][1].ID)
})

// Using a management token, we should get job results from all three namespaces
Expand All @@ -1744,9 +1744,9 @@ func TestSearch_FuzzySearch_MultiNamespace_ACL(t *testing.T) {
var resp structs.FuzzySearchResponse
require.NoError(t, msgpackrpc.CallWithCodec(codec, "Search.FuzzySearch", req, &resp))
require.Len(t, resp.Matches[structs.Jobs], 3)
require.Equal(t, job1.ID, resp.Matches[structs.Jobs][0].ID)
require.Equal(t, job2.ID, resp.Matches[structs.Jobs][1].ID)
require.Equal(t, job3.ID, resp.Matches[structs.Jobs][2].ID)
require.Equal(t, job1.Name, resp.Matches[structs.Jobs][0].ID)
require.Equal(t, job2.Name, resp.Matches[structs.Jobs][1].ID)
require.Equal(t, job3.Name, resp.Matches[structs.Jobs][2].ID)
})

// Using a token that can read nodes, we should get our 1 matching node when
Expand Down
9 changes: 5 additions & 4 deletions website/content/api-docs/search.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,17 @@ $ curl \
##### Scope (jobs)

- `Scope[0]` : Namespace
- `Scope[1]` : Job ID

##### Scope (groups)

- `Scope[0]` : Namespace
- `Scope[1]` : Job
- `Scope[1]` : Job ID

##### Scope (tasks)

- `Scope[0]` : Namespace
- `Scope[1]` : Job
- `Scope[1]` : Job ID
- `Scope[2]` : Group

##### Scope (group services)
Expand All @@ -282,14 +283,14 @@ $ curl \
#### Scope (task services)

- `Scope[0]` : Namespace
- `Scope[1]` : Job
- `Scope[1]` : Job ID
- `Scope[2]` : Group
- `Scope[3]` : Task

#### Scope (commands/images/classes)

- `Scope[0]` : Namespace
- `Scope[1]` : Job
- `Scope[1]` : Job ID
- `Scope[2]` : Group
- `Scope[3]` : Task

Expand Down

0 comments on commit 068fd43

Please sign in to comment.