github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/website/content/api-docs/search.mdx (about)

     1  ---
     2  layout: api
     3  page_title: Search - HTTP API
     4  description: The /search endpoint is used to search for Nomad objects
     5  ---
     6  
     7  # Search HTTP API
     8  
     9  ## Prefix Searching
    10  
    11  The `/search` endpoint returns matches for a given prefix and context, where a
    12  context can be jobs, allocations, evaluations, nodes, deployments, plugins,
    13  namespaces, or volumes. When using Nomad Enterprise, the allowed contexts
    14  include quotas. Additionally, a prefix can be searched for within every
    15  context.
    16  
    17  | Method | Path         | Produces           |
    18  | ------ | ------------ | ------------------ |
    19  | `POST` | `/v1/search` | `application/json` |
    20  
    21  The table below shows this endpoint's support for
    22  [blocking queries](/api-docs#blocking-queries) and
    23  [required ACLs](/api-docs#acls).
    24  
    25  | Blocking Queries | ACL Required                     |
    26  | ---------------- | -------------------------------- |
    27  | `NO`             | `node:read, namespace:read-jobs` |
    28  
    29  When ACLs are enabled, requests must have a token valid for `node:read` or
    30  `namespace:read-jobs` roles. If the token is only valid for `node:read`, then
    31  job related results will not be returned. If the token is only valid for
    32  `namespace:read-jobs`, then node results will not be returned.
    33  
    34  ### Parameters
    35  
    36  - `Prefix` `(string: <required>)` - Specifies the identifier against which
    37    matches will be found. For example, if the given prefix were "a", potential
    38    matches might be "abcd", or "aabb".
    39  - `Context` `(string: <required>)` - Defines the scope in which a search for a
    40    prefix operates. Contexts can be: "jobs", "evals", "allocs", "nodes",
    41    "deployment", "plugins", "volumes" or "all", where "all" means every
    42    context will be searched.
    43  
    44  ### Sample Payload (for all contexts)
    45  
    46  ```json
    47  {
    48    "Prefix": "abc",
    49    "Context": "all"
    50  }
    51  ```
    52  
    53  ### Sample Request
    54  
    55  ```shell-session
    56  $ curl \
    57      --request POST \
    58      --data @payload.json \
    59      https://localhost:4646/v1/search
    60  ```
    61  
    62  ### Sample Response
    63  
    64  ```json
    65  {
    66    "Matches": {
    67      "allocs": null,
    68      "deployment": null,
    69      "evals": ["abc2fdc0-e1fd-2536-67d8-43af8ca798ac"],
    70      "jobs": ["abcde"],
    71      "nodes": null,
    72      "plugins": null,
    73      "volumes": null
    74    },
    75    "Truncations": {
    76      "allocs": "false",
    77      "deployment": "false",
    78      "evals": "false",
    79      "jobs": "false",
    80      "nodes": "false",
    81      "plugins": "false",
    82      "volumes": "false"
    83    }
    84  }
    85  ```
    86  
    87  #### Field Reference
    88  
    89  - `Matches` - A map of contexts to matching arrays of identifiers.
    90  
    91  - `Truncations` - Search results are capped at 20; if more matches were found for a particular context, it will be `true`.
    92  
    93  ### Sample Payload (for a specific context)
    94  
    95  ```json
    96  {
    97    "Prefix": "abc",
    98    "Context": "evals"
    99  }
   100  ```
   101  
   102  ### Sample Request
   103  
   104  ```shell-session
   105  $ curl \
   106      --request POST \
   107      --data @payload.json \
   108      https://localhost:4646/v1/search
   109  ```
   110  
   111  ### Sample Response
   112  
   113  ```json
   114  {
   115    "Matches": {
   116      "evals": ["abc2fdc0-e1fd-2536-67d8-43af8ca798ac"]
   117    },
   118    "Truncations": {
   119      "evals": "false"
   120    }
   121  }
   122  ```
   123  
   124  ## Fuzzy Searching
   125  
   126  The `/search/fuzzy` endpoint returns partial substring matches for a given search
   127  term and context, where a context can be jobs, allocations, nodes, plugins, or namespaces.
   128  Additionally, fuzzy searching can be done across all contexts. For better control
   129  over the performance implications of fuzzy searching on Nomad servers, aspects of
   130  fuzzy searching can be tuned through the <code>[search]</code> stanza in Nomad agent config.
   131  
   132  Fuzzy search results are ordered starting with closest matching terms. Items of
   133  a name that exactly matches the search term are listed first.
   134  
   135  | Method | Path               | Produces           |
   136  | ------ | ------------------ | ------------------ |
   137  | `POST` | `/v1/search/fuzzy` | `application/json` |
   138  
   139  The table below shows this endpoint's support for
   140  [blocking queries](/api-docs#blocking-queries) and
   141  [required ACLs](/api-docs#acls).
   142  
   143  | Blocking Queries | ACL Required                                                |
   144  | ---------------- | ----------------------------------------------------------- |
   145  | `NO`             | `node:read, namespace:read-jobs, namespace:csi-list-plugin` |
   146  
   147  When ACLs are enabled, requests must have a token valid for `node:read`, `plugin:read` or
   148  `namespace:read-jobs` roles. If the token is only valid for a portion of these
   149  capabilities, then results will include results including only data readable with
   150  the given token.
   151  
   152  ### Parameters
   153  
   154  - `Text` `(string: <required>)` - Specifies the identifier against which
   155    matches will be found. For example, if the given text were "py", potential
   156    fuzzy matches might be "python", "spying", or "happy".
   157  - `Context` `(string: <required>)` - Defines the scope in which a search for a
   158    prefix operates. Contexts can be: "jobs", "allocs", "nodes", "plugins", or
   159    "all", where "all" means every context will be searched. When "all" is selected,
   160    additional prefix matches will be included for the "deployments", "evals", and
   161    "volumes" types. When searching in the "jobs" context, results that fuzzy match
   162    "groups", "services", "tasks", "images", "commands", and "classes" are also
   163    included in the results.
   164  
   165  ### Scope
   166  
   167  Fuzzy match results are accompanied with a `Scope` field which is used to uniquely
   168  identify the matched object, in a way that the Nomad API can be queried again for
   169  additional information. The data provided by scope varies depending on the type
   170  of matched object, described below.
   171  
   172  ### Sample Payload (for jobs)
   173  
   174  ```json
   175  {
   176    "Text": "py",
   177    "Context": "jobs"
   178  }
   179  ```
   180  
   181  ### Sample Request
   182  
   183  ```shell-session
   184  $ curl \
   185      --request POST \
   186      --data @payload.json \
   187      https://localhost:4646/v1/search/fuzzy
   188  ```
   189  
   190  ### Sample Response
   191  
   192  ```json
   193  {
   194    "Index": 90,
   195    "KnownLeader": true,
   196    "LastContact": 0,
   197    "Matches": {
   198      "services": [
   199        {
   200          "ID": "python-logger",
   201          "Scope": [
   202            "default",
   203            "example-python",
   204            "my-spy-app",
   205            "my-python-task"
   206          ]
   207        },
   208        {
   209          "ID": "super-spy-service",
   210          "Scope": [
   211            "default",
   212            "example-python",
   213            "my-spy-app"
   214          ]
   215        }
   216      ],
   217      "tasks": [
   218        {
   219          "ID": "my-python-task",
   220          "Scope": [
   221            "default",
   222            "example-python",
   223            "my-spy-app"
   224          ]
   225        }
   226      ],
   227      "images": [
   228        {
   229          "ID": "python:3",
   230          "Scope": [
   231            "default",
   232            "example-python",
   233            "my-spy-app",
   234            "my-python-task"
   235          ]
   236        }
   237      ],
   238      "jobs": [
   239        {
   240          "ID": "example-python",
   241          "Scope": [
   242            "default"
   243          ]
   244        }
   245      ],
   246      "groups": [
   247        {
   248          "ID": "my-spy-app",
   249          "Scope": [
   250            "default",
   251            "example-python"
   252          ]
   253        }
   254      ]
   255    },
   256    "Truncations": {
   257      "jobs": false
   258    }
   259  }
   260  ```
   261  
   262  #### Scope (jobs)
   263  
   264  - `Scope[0]` : Namespace
   265  - `Scope[1]` : Job ID
   266  
   267  #### Scope (groups)
   268  
   269  - `Scope[0]` : Namespace
   270  - `Scope[1]` : Job ID
   271  
   272  #### Scope (tasks)
   273  
   274  - `Scope[0]` : Namespace
   275  - `Scope[1]` : Job ID
   276  - `Scope[2]` : Group
   277  
   278  #### Scope (group services)
   279  
   280  - `Scope[0]` : Namespace
   281  - `Scope[1]` : Group
   282  
   283  #### Scope (task services)
   284  
   285  - `Scope[0]` : Namespace
   286  - `Scope[1]` : Job ID
   287  - `Scope[2]` : Group
   288  - `Scope[3]` : Task
   289  
   290  #### Scope (commands/images/classes)
   291  
   292  - `Scope[0]` : Namespace
   293  - `Scope[1]` : Job ID
   294  - `Scope[2]` : Group
   295  - `Scope[3]` : Task
   296  
   297  ### Sample Payload (for nodes)
   298  
   299  ```json
   300  {
   301    "Text": "lab",
   302    "Context": "nodes"
   303  }
   304  ```
   305  
   306  ### Sample Request
   307  
   308  ```shell-session
   309  $ curl \
   310      --request POST \
   311      --data @payload.json \
   312      https://localhost:4646/v1/search/fuzzy
   313  ```
   314  
   315  ### Sample Response
   316  
   317  ```json
   318  {
   319    "Index": 9,
   320    "KnownLeader": true,
   321    "LastContact": 0,
   322    "Matches": {
   323      "nodes": [
   324        {
   325          "ID": "nomad-lab1",
   326          "Scope": [
   327            "c48cd39f-dfe1-9cc0-9c62-617d199854be"
   328          ]
   329        }
   330      ]
   331    },
   332    "Truncations": {
   333      "nodes": false
   334    }
   335  }
   336  ```
   337  
   338  ##### Scope (nodes)
   339  
   340  - `Scope[0]` : Node ID
   341  
   342  ### Sample Payload (for allocs)
   343  
   344  ```json
   345  {
   346     "Text":"py",
   347     "Context":"allocs"
   348  }
   349  ```
   350  
   351  ### Sample Request
   352  
   353  ```shell-session
   354  $ curl \
   355      --request POST \
   356      --data @payload.json \
   357      https://localhost:4646/v1/search/fuzzy
   358  ```
   359  
   360  ### Sample Response
   361  
   362  ```json
   363  {
   364    "Index": 136,
   365    "KnownLeader": true,
   366    "LastContact": 0,
   367    "Matches": {
   368      "allocs": [
   369        {
   370          "ID": "example-python.my-spy-app[0]",
   371          "Scope": [
   372            "default",
   373            "0fb703d1-ba4d-116f-13aa-27f31f046858"
   374          ]
   375        }
   376      ]
   377    },
   378    "Truncations": {
   379      "allocs": false
   380    }
   381  }
   382  ```
   383  
   384  #### Scope (allocs)
   385  
   386  - `Scope[0]` : Namespace
   387  - `Scope[1]` : Alloc ID
   388  
   389  
   390  ### Sample Payload (for plugins)
   391  
   392  ```json
   393  {
   394    "Text": "aws",
   395    "Context": "plugins"
   396  }
   397  ```
   398  
   399  ### Sample Request
   400  
   401  ```shell-session
   402  $ curl \
   403      --request POST \
   404      --data @payload.json \
   405      https://localhost:4646/v1/search/fuzzy
   406  ```
   407  
   408  ### Sample Response
   409  
   410  ```json
   411  {
   412    "Index": 0,
   413    "KnownLeader": true,
   414    "LastContact": 0,
   415    "Matches": {
   416      "plugins": [
   417        {
   418          "ID": "aws-efs0"
   419        }
   420      ]
   421    },
   422    "Truncations": {
   423      "plugins": false
   424    }
   425  }
   426  ```
   427  
   428  ### Sample Payload (for all)
   429  
   430  ```json
   431  {
   432    "Index": 260,
   433    "KnownLeader": true,
   434    "LastContact": 0,
   435    "Matches": {
   436      "services": [
   437        {
   438          "ID": "python-logger",
   439          "Scope": [
   440            "default",
   441            "example-python",
   442            "my-spy-app",
   443            "my-python-task"
   444          ]
   445        },
   446        {
   447          "ID": "super-spy-service",
   448          "Scope": [
   449            "default",
   450            "example-python",
   451            "my-spy-app"
   452          ]
   453        }
   454      ],
   455      "tasks": [
   456        {
   457          "ID": "my-python-task",
   458          "Scope": [
   459            "default",
   460            "example-python",
   461            "my-spy-app"
   462          ]
   463        }
   464      ],
   465      "jobs": [
   466        {
   467          "ID": "example-python",
   468          "Scope": [
   469            "default"
   470          ]
   471        }
   472      ],
   473      "evals": [],
   474      "scaling_policy": [],
   475      "groups": [
   476        {
   477          "ID": "my-spy-app",
   478          "Scope": [
   479            "default",
   480            "example-python"
   481          ]
   482        }
   483      ],
   484      "images": [
   485        {
   486          "ID": "python:3",
   487          "Scope": [
   488            "default",
   489            "example-python",
   490            "my-spy-app",
   491            "my-python-task"
   492          ]
   493        }
   494      ],
   495      "plugins": [
   496        {
   497          "ID": "aws-spy-plugin"
   498        }
   499      ],
   500      "deployment": [],
   501      "volumes": [],
   502      "allocs": [
   503        {
   504          "ID": "example-python.my-spy-app[0]",
   505          "Scope": [
   506            "default",
   507            "48608246-4c28-0446-f3d1-c67e3bc650ad"
   508          ]
   509        }
   510      ]
   511    },
   512    "Truncations": {
   513      "deployment": false,
   514      "volumes": false,
   515      "plugins": false,
   516      "namespaces": false,
   517      "scaling_policy": false,
   518      "evals": false,
   519      "allocs": false,
   520      "jobs": false,
   521      "nodes": false
   522    }
   523  }
   524  ```
   525  
   526  ### Sample Request
   527  
   528  ```shell-session
   529  $ curl \
   530      --request POST \
   531      --data @payload.json \
   532      https://localhost:4646/v1/search/fuzzy
   533  ```
   534  
   535  ### Prefix matching when fuzzy searching
   536  
   537  If the search Context is `all` when fuzzy searching, the object types that are
   538  identified only with UUIDs are also concurrently prefix-searched. Those types include
   539  deployments, evals, volumes, and quotas (Enterprise).
   540  
   541  ### Sample Payload (prefix match)
   542  
   543  ```json
   544  {
   545     "Text":"cc",
   546     "Context":"all"
   547  }
   548  ```
   549  
   550  ### Sample Request
   551  
   552  ```shell-session
   553  $ curl \
   554      --request POST \
   555      --data @payload.json \
   556      https://localhost:4646/v1/search/fuzzy
   557  ```
   558  
   559  ### Sample Result
   560  ```json
   561  {
   562    "Index": 267,
   563    "KnownLeader": true,
   564    "LastContact": 0,
   565    "Matches": {
   566      "scaling_policy": [],
   567      "evals": [],
   568      "deployment": [
   569        {
   570          "ID": "cc786388-e071-31ec-5821-b829839f9681"
   571        }
   572      ],
   573      "volumes": []
   574    },
   575    "Truncations": {
   576      "deployment": false,
   577      "volumes": false,
   578      "plugins": false,
   579      "namespaces": false,
   580      "scaling_policy": false,
   581      "evals": false,
   582      "allocs": false,
   583      "jobs": false,
   584      "nodes": false
   585    }
   586  }
   587  ```
   588  
   589  [search]: /docs/configuration/search