4

This is a spin-off question to query with filters

Say my application is managing objects called workload, with the following fields. I want to expose a REST interface for user to query workloads by labels.

"Workload": {"id":"test1", "labels":["A", "B", "C"]}
"Workload": {"id":"test2", "labels":["A", "C", "D"]}
"Workload": {"id":"test3", "labels":["A", "B", "D"]}

Question: How do I design the REST endpoint so that it would support query workload with basic logic operations?

Sample Query 2: I want to GET all the workloads with label "A" or "B" but no "C"

No clue how to do this sort of rest api at all, other than ask user to query by A, B, C separately then do proper set operations themselves? (What a great user experience...)

A similar question here touches upon query with boolean logic on different filters, but it doesn't seem applicable to repeated filter. (In this case, labels. It seems weird to do GET /workloads/labels:A/labels:B)

Community
  • 1
  • 1
cookieisaac
  • 1,357
  • 5
  • 18
  • 35

1 Answers1

0

Depending on the exact requirements, I would perhaps start with the "google" approach. Just present a query form, and create some primitive query language which might be just text (it's not necessary to use json if it's simple enough).

So the search page would look something like this:

{ "searchForm": {
    "target": "/workloads",
    "method": "GET",
    "components": [{ "name": "q" }]
  }
}

The media-type for the search page would define how to use the form, probably that it should make a request like:

GET /workloads?q=+A+B-C

For the query language I would go for the absolute minimum. Maybe just "+" and "-" signs, just like google. I would probably stay with a text query language even when more complex queries are needed, just to make it easy to read/test manually.

Or, if you don't want to be that RESTful, you can hardcode the query-uri into the application, that way you don't have to create the search page its media-type.

Robert Bräutigam
  • 7,514
  • 1
  • 20
  • 38