什么是使用逻辑操作进行查询的 RESTful 方式? [英] What's a RESTful way to query with logic operation?

查看:40
本文介绍了什么是使用逻辑操作进行查询的 RESTful 方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是使用过滤器查询

假设我的应用程序正在管理称为工作负载的对象,其中包含以下字段.我想公开一个 REST 接口供用户通过标签查询工作负载.

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"]}

问题:我如何设计 REST 端点,以便它支持具有基本逻辑操作的查询工作负载?

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

示例查询 2:我想获取标签为A"或B"但没有C"的所有工作负载

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

完全不知道怎么做这种rest api,除了让用户分别按A、B、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...)

这里有一个类似的问题涉及使用布尔逻辑的查询不同过滤器,但似乎不适用于重复过滤器.(在这种情况下,标签.执行 GET/workloads/labels:A/labels:B 似乎很奇怪)

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)

推荐答案

根据具体要求,我可能会从google"方法开始.只需呈现一个查询表单,并创建一些可能只是文本的原始查询语言(如果它足够简单,则不必使用 json).

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" }]
  }
}

搜索页面的 media-type 将定义如何使用表单,可能它应该发出如下请求:

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.

或者,如果您不想成为 那个 RESTful,您可以将查询 uri 硬编码到应用程序中,这样您就不必创建其媒体类型的搜索页面.

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.

这篇关于什么是使用逻辑操作进行查询的 RESTful 方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆