2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-08-10 19:24:11 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2017-08-14 13:34:31 +00:00
|
|
|
"github.com/hashicorp/nomad/api/contexts"
|
2017-08-10 19:24:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Search struct {
|
|
|
|
client *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search returns a handle on the Search endpoints
|
|
|
|
func (c *Client) Search() *Search {
|
|
|
|
return &Search{client: c}
|
|
|
|
}
|
|
|
|
|
api: implement fuzzy search API
This PR introduces the /v1/search/fuzzy API endpoint, used for fuzzy
searching objects in Nomad. The fuzzy search endpoint routes requests
to the Nomad Server leader, which implements the Search.FuzzySearch RPC
method.
Requests to the fuzzy search API are based on the api.FuzzySearchRequest
object, e.g.
{
"Text": "ed",
"Context": "all"
}
Responses from the fuzzy search API are based on the api.FuzzySearchResponse
object, e.g.
{
"Index": 27,
"KnownLeader": true,
"LastContact": 0,
"Matches": {
"tasks": [
{
"ID": "redis",
"Scope": [
"default",
"example",
"cache"
]
}
],
"evals": [],
"deployment": [],
"volumes": [],
"scaling_policy": [],
"images": [
{
"ID": "redis:3.2",
"Scope": [
"default",
"example",
"cache",
"redis"
]
}
]
},
"Truncations": {
"volumes": false,
"scaling_policy": false,
"evals": false,
"deployment": false
}
}
The API is tunable using the new server.search stanza, e.g.
server {
search {
fuzzy_enabled = true
limit_query = 200
limit_results = 1000
min_term_length = 5
}
}
These values can be increased or decreased, so as to provide more
search results or to reduce load on the Nomad Server. The fuzzy search
API can be disabled entirely by setting `fuzzy_enabled` to `false`.
2021-02-23 20:24:52 +00:00
|
|
|
// PrefixSearch returns a set of matches for a particular context and prefix.
|
2017-08-28 05:17:51 +00:00
|
|
|
func (s *Search) PrefixSearch(prefix string, context contexts.Context, q *QueryOptions) (*SearchResponse, *QueryMeta, error) {
|
2017-08-11 21:21:35 +00:00
|
|
|
var resp SearchResponse
|
|
|
|
req := &SearchRequest{Prefix: prefix, Context: context}
|
2017-08-10 19:24:11 +00:00
|
|
|
|
2017-08-28 05:17:51 +00:00
|
|
|
qm, err := s.client.putQuery("/v1/search", req, &resp, q)
|
2017-08-10 19:24:11 +00:00
|
|
|
if err != nil {
|
2017-08-28 05:17:51 +00:00
|
|
|
return nil, nil, err
|
2017-08-10 19:24:11 +00:00
|
|
|
}
|
|
|
|
|
2017-08-28 05:17:51 +00:00
|
|
|
return &resp, qm, nil
|
2017-08-10 19:24:11 +00:00
|
|
|
}
|
2017-08-11 21:21:35 +00:00
|
|
|
|
api: implement fuzzy search API
This PR introduces the /v1/search/fuzzy API endpoint, used for fuzzy
searching objects in Nomad. The fuzzy search endpoint routes requests
to the Nomad Server leader, which implements the Search.FuzzySearch RPC
method.
Requests to the fuzzy search API are based on the api.FuzzySearchRequest
object, e.g.
{
"Text": "ed",
"Context": "all"
}
Responses from the fuzzy search API are based on the api.FuzzySearchResponse
object, e.g.
{
"Index": 27,
"KnownLeader": true,
"LastContact": 0,
"Matches": {
"tasks": [
{
"ID": "redis",
"Scope": [
"default",
"example",
"cache"
]
}
],
"evals": [],
"deployment": [],
"volumes": [],
"scaling_policy": [],
"images": [
{
"ID": "redis:3.2",
"Scope": [
"default",
"example",
"cache",
"redis"
]
}
]
},
"Truncations": {
"volumes": false,
"scaling_policy": false,
"evals": false,
"deployment": false
}
}
The API is tunable using the new server.search stanza, e.g.
server {
search {
fuzzy_enabled = true
limit_query = 200
limit_results = 1000
min_term_length = 5
}
}
These values can be increased or decreased, so as to provide more
search results or to reduce load on the Nomad Server. The fuzzy search
API can be disabled entirely by setting `fuzzy_enabled` to `false`.
2021-02-23 20:24:52 +00:00
|
|
|
type SearchResponse struct {
|
|
|
|
Matches map[contexts.Context][]string
|
|
|
|
Truncations map[contexts.Context]bool
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
2017-08-11 21:21:35 +00:00
|
|
|
type SearchRequest struct {
|
|
|
|
Prefix string
|
2017-08-14 13:34:31 +00:00
|
|
|
Context contexts.Context
|
2017-08-28 05:17:51 +00:00
|
|
|
QueryOptions
|
2017-08-11 21:21:35 +00:00
|
|
|
}
|
|
|
|
|
api: implement fuzzy search API
This PR introduces the /v1/search/fuzzy API endpoint, used for fuzzy
searching objects in Nomad. The fuzzy search endpoint routes requests
to the Nomad Server leader, which implements the Search.FuzzySearch RPC
method.
Requests to the fuzzy search API are based on the api.FuzzySearchRequest
object, e.g.
{
"Text": "ed",
"Context": "all"
}
Responses from the fuzzy search API are based on the api.FuzzySearchResponse
object, e.g.
{
"Index": 27,
"KnownLeader": true,
"LastContact": 0,
"Matches": {
"tasks": [
{
"ID": "redis",
"Scope": [
"default",
"example",
"cache"
]
}
],
"evals": [],
"deployment": [],
"volumes": [],
"scaling_policy": [],
"images": [
{
"ID": "redis:3.2",
"Scope": [
"default",
"example",
"cache",
"redis"
]
}
]
},
"Truncations": {
"volumes": false,
"scaling_policy": false,
"evals": false,
"deployment": false
}
}
The API is tunable using the new server.search stanza, e.g.
server {
search {
fuzzy_enabled = true
limit_query = 200
limit_results = 1000
min_term_length = 5
}
}
These values can be increased or decreased, so as to provide more
search results or to reduce load on the Nomad Server. The fuzzy search
API can be disabled entirely by setting `fuzzy_enabled` to `false`.
2021-02-23 20:24:52 +00:00
|
|
|
// FuzzySearch returns a set of matches for a given context and string.
|
|
|
|
func (s *Search) FuzzySearch(text string, context contexts.Context, q *QueryOptions) (*FuzzySearchResponse, *QueryMeta, error) {
|
|
|
|
var resp FuzzySearchResponse
|
|
|
|
|
|
|
|
req := &FuzzySearchRequest{
|
|
|
|
Context: context,
|
|
|
|
Text: text,
|
|
|
|
}
|
|
|
|
|
|
|
|
qm, err := s.client.putQuery("/v1/search/fuzzy", req, &resp, q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &resp, qm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FuzzyMatch is used to describe the ID of an object which may be a machine
|
|
|
|
// readable UUID or a human readable Name. If the object is a component of a Job,
|
|
|
|
// the Scope is a list of IDs starting from Namespace down to the parent object of
|
|
|
|
// ID.
|
|
|
|
//
|
|
|
|
// e.g. A Task-level service would have scope like,
|
|
|
|
// ["<namespace>", "<job>", "<group>", "<task>"]
|
|
|
|
type FuzzyMatch struct {
|
|
|
|
ID string // ID is UUID or Name of object
|
|
|
|
Scope []string `json:",omitempty"` // IDs of parent objects
|
|
|
|
}
|
|
|
|
|
|
|
|
// FuzzySearchResponse is used to return fuzzy matches and information about
|
|
|
|
// whether the match list is truncated specific to each type of searchable Context.
|
|
|
|
type FuzzySearchResponse struct {
|
|
|
|
// Matches is a map of Context types to IDs which fuzzy match a specified query.
|
|
|
|
Matches map[contexts.Context][]FuzzyMatch
|
|
|
|
|
|
|
|
// Truncations indicates whether the matches for a particular Context have
|
|
|
|
// been truncated.
|
2017-08-14 13:34:31 +00:00
|
|
|
Truncations map[contexts.Context]bool
|
api: implement fuzzy search API
This PR introduces the /v1/search/fuzzy API endpoint, used for fuzzy
searching objects in Nomad. The fuzzy search endpoint routes requests
to the Nomad Server leader, which implements the Search.FuzzySearch RPC
method.
Requests to the fuzzy search API are based on the api.FuzzySearchRequest
object, e.g.
{
"Text": "ed",
"Context": "all"
}
Responses from the fuzzy search API are based on the api.FuzzySearchResponse
object, e.g.
{
"Index": 27,
"KnownLeader": true,
"LastContact": 0,
"Matches": {
"tasks": [
{
"ID": "redis",
"Scope": [
"default",
"example",
"cache"
]
}
],
"evals": [],
"deployment": [],
"volumes": [],
"scaling_policy": [],
"images": [
{
"ID": "redis:3.2",
"Scope": [
"default",
"example",
"cache",
"redis"
]
}
]
},
"Truncations": {
"volumes": false,
"scaling_policy": false,
"evals": false,
"deployment": false
}
}
The API is tunable using the new server.search stanza, e.g.
server {
search {
fuzzy_enabled = true
limit_query = 200
limit_results = 1000
min_term_length = 5
}
}
These values can be increased or decreased, so as to provide more
search results or to reduce load on the Nomad Server. The fuzzy search
API can be disabled entirely by setting `fuzzy_enabled` to `false`.
2021-02-23 20:24:52 +00:00
|
|
|
|
2017-08-11 21:21:35 +00:00
|
|
|
QueryMeta
|
|
|
|
}
|
api: implement fuzzy search API
This PR introduces the /v1/search/fuzzy API endpoint, used for fuzzy
searching objects in Nomad. The fuzzy search endpoint routes requests
to the Nomad Server leader, which implements the Search.FuzzySearch RPC
method.
Requests to the fuzzy search API are based on the api.FuzzySearchRequest
object, e.g.
{
"Text": "ed",
"Context": "all"
}
Responses from the fuzzy search API are based on the api.FuzzySearchResponse
object, e.g.
{
"Index": 27,
"KnownLeader": true,
"LastContact": 0,
"Matches": {
"tasks": [
{
"ID": "redis",
"Scope": [
"default",
"example",
"cache"
]
}
],
"evals": [],
"deployment": [],
"volumes": [],
"scaling_policy": [],
"images": [
{
"ID": "redis:3.2",
"Scope": [
"default",
"example",
"cache",
"redis"
]
}
]
},
"Truncations": {
"volumes": false,
"scaling_policy": false,
"evals": false,
"deployment": false
}
}
The API is tunable using the new server.search stanza, e.g.
server {
search {
fuzzy_enabled = true
limit_query = 200
limit_results = 1000
min_term_length = 5
}
}
These values can be increased or decreased, so as to provide more
search results or to reduce load on the Nomad Server. The fuzzy search
API can be disabled entirely by setting `fuzzy_enabled` to `false`.
2021-02-23 20:24:52 +00:00
|
|
|
|
|
|
|
// FuzzySearchRequest is used to parameterize a fuzzy search request, and returns
|
|
|
|
// a list of matches made up of jobs, allocations, evaluations, and/or nodes,
|
|
|
|
// along with whether or not the information returned is truncated.
|
|
|
|
type FuzzySearchRequest struct {
|
|
|
|
// Text is what names are fuzzy-matched to. E.g. if the given text were
|
|
|
|
// "py", potential matches might be "python", "mypy", etc. of jobs, nodes,
|
|
|
|
// allocs, groups, services, commands, images, classes.
|
|
|
|
Text string
|
|
|
|
|
|
|
|
// Context is the type that can be matched against. A Context of "all" indicates
|
|
|
|
// all Contexts types are queried for matching.
|
|
|
|
Context contexts.Context
|
|
|
|
|
|
|
|
QueryOptions
|
|
|
|
}
|