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

本文是小编为大家收集整理的关于什么是带有逻辑操作的RESTful查询方式?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

这是一个旋转问题,到使用过滤器查询

说我的应用程序正在管理名为workload的对象,使用以下字段.我想公开一个REST接口,让用户通过标签查询工作负载.

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

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

样本查询2 :我想使用标签"a"或"b"的所有工作负载,但没有"c"

没有Clue如何做这种休息API,除了询问用户通过A,B,C分别向用户进行查询,然后单独做正确的设置操作吗? (多么伟大的用户体验......)

这里的类似问题在查询时用布尔逻辑触摸不同的过滤器,但它似乎没有适用于重复过滤器. (在这种情况下,标签.似乎做到GET /workloads/labels:A/labels:B)

推荐答案

取决于确切要求,我可能会从"Google"方法开始.刚呈现一个查询表单,并创建一些可能只是文本的原始查询语言(如果它足够简单,则无需使用JSON).

所以搜索页面看起来像这样:

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

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

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

用于查询语言,我将用于绝对最小值.也许只是"+"和" - "标志,就像谷歌一样.即使需要更复杂的查询,我可能会留在文本查询语言中,只是为了使您可以轻松地手动读/测试.

或者,如果您不想成为 RESTFUL,则可以将查询URI硬介绍到应用程序中,这样您不必创建搜索页面其媒体类型.

本文地址:https://www.itbaoku.cn/post/1937846.html

问题描述

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)

推荐答案

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.