如何德兴REST风格的高级搜索

如何德兴REST风格的高级搜索

本文介绍了如何德兴REST风格的高级搜索/过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我已阅读和如何设计REST风格的搜索/过滤?问题。我试图设计出更高级的选项以简单和RESTful方式搜索。
对这些问题的答案给了我关于如何设计搜索/过滤功能,我的previous应用程序URL模式的一些见解和线索。

First of all, I have read RESTful URL design for search and How to design RESTful search/filtering? questions. I am trying to design more advanced options for searching in a simple and RESTful way.The answers to those questions have given me some insight and clues on how to design my previous application url pattern for search/filter functionality.

首先,我想出了基本的过滤选项相当不错的,简单的解决方案,使用方式:

First, I came up with quite nice and simple solution for basic filtering options, using pattern:

Equality search: key = val

IN search: key = val1 & key = val2

但是,随着应用越来越多,因此有搜索需求。我结束了与高级搜索选项的一些相当不愉快的和复杂的URL模式,其中包括:

But as application has grown, so were the search requirements. And I ended up with some rather unpleasant and complex url pattern for advanced searching options which included:

Negation search: key-N = val

Like search: key-L = val

OR search: key1-O = val1 & key2 = val2

Range search: key1-RS = val1 & key1-RE = val2

什么是更多,旁边过滤器,查询必须得到有关分页和ORDER BY信息,因此滤波器参数有F-后缀,为了用领域有着O-后缀,分页具有P-后缀。

Whats more, beside filters, query has to get information about pagination and order by, so the filter parameter has F- suffix, order by fields has O- suffix, and pagination has P- suffix.

我希望在这一点上我没有补充,解析这样的要求是相当恶意的任务,用含糊不清的可能性,如果键将包含 - 。我已经创造了一些正则表达式解析它,和它的作品相当不错的现在,但是......

I hope that at this point I do not have to add that parsing such request is rather malicious task, with the possibility of ambiguity if key will contain '-'. I have created some regexp to parse it, and it works quite well as for now, but...

现在我开始写一个新的Web应用程序,我必须从头开始重新设计这一块的机会。

Now I am starting to write a new web app and I have the chance to redesign this piece from scratch.

我想知道在浏览器中包含结构化和不言自明的方式,所有的信息,并将其发送到服务器的JSON字符串,如创建对象:

I am wondering about creating object in a browser containing all information in structured and self-explanatory way and send it to server as as JSON string, like:

filter = {{'type':'like','field':key,'value':val1,'operator':'and','negation':false},..}

但我得到的奇怪的感觉,这不是好主意 -​​ 我真的不知道为什么。

But I get strange feeling that this is not good idea - I really don't know why.

所以,这将是我的上下文的定义。现在的问题是:

So, this would be the definition of my context. Now the question:

我在寻找简单和更安全的模式实现高级​​搜索,包括我上面的RESTful GET参数提到的选项 - 你可以分享一些想法?
或者,也许一些见解上不这样做REST方式?
另外,如果你看到JSON方式一些陷阱,请分享。

I am searching for simpler and safer pattern for implementing advanced search including options I mentioned above as RESTful GET parameters - can you share some ideas?Or maybe some insights on not doing this in a RESTful way?Also, if you see some pitfalls in JSON way, please share them.

编辑:

我知道是什么让发送JSON作为GET参数,不是那么好主意。编码它 - 它使丑陋,难以阅读

I know what makes sending json as get parameter, not so good idea. Encoding it - it makes it ugly and hard to read.

信息,给了我一些思考,我设法设计出更consistient安全过滤器,GET参数处理。下面是语法的定义。

Info provided by links sended by thierry templier, gave me something to think about and I managed to design more consistient and safe filter handling in GET parameters. Below is definition of syntax.

有关过滤器 - 访问多个F参数(每个搜索绕圈):

For filters - multiple F parameters (one for each search criterium):

F = OPERATOR:NEGATION:TYPE:FIELD:VAL[:VAL1,:VAL2...]

允许值:

[AND|OR]:[T|F]:[EQ|LI|IN|RA]:FIELD_NAME:VALUE1:VALUE2...

有关借命令 - 多重O参数(每个有序域):

For order by - multiple O parameters (one for each ordered field):

O = ODINAL_NO:DIRECTION:FIELD

允许值:

[0-9]+:[ASC|DESC]:FIELD_NAME

分页 - 单P参数:

Pagination - single P parameter:

P = ITEMS_PER_PAGE:FROM_PAGE:TO_PAGE

我认为这将是很好的解决方案 - 这符合我的所有要求,很容易解析和写,这是可读的,我没有看到这种语法如何能变得模糊。

I think this will be good solution - it meets all my requirements, it is easy to parse and write, it is readable and I do not see how that syntax can become ambiguous.

我wloud AP preciate任何思考这个想法 - 你看到任何缺陷

I wloud appreciate any thoughts on that idea - do you see any pitfalls?

推荐答案

这里有几个选项。但很显然,如果你的查询往往是复杂与运营商,等等......你不能用一组查询参数。我看到两种方法:

There are several options here. But it's clear that if your queries tend to be complex with operators, and so on... you can't use a set of query parameters. I see two approaches:


  • 提供查询作为JSON内容到方法 POST

  • 提供与特定格式/语法查询参数查询的方法 GET

  • Provide the query as JSON content to a method POST
  • Provide the query in a query parameter with a specific format / grammar to a method GET

我认为你可以看看什么ElasticSearch他们的查询。它们能够描述使用JSON内容非常复杂的查询(使用几个级别)。这里是他们的查询DSL链接:.

I think that you could have a look at what ElasticSearch for their queries. They are able to describe very complex queries with JSON contents (using several levels). Here is a link to their query DSL: http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html.

您也可以看看什么的OData确实为查询。他们选择用一个查询参数的另一种方法 $过滤器。这里有一些链接,可以给你一些例子:和的。此选项需要对服务器端语法解析查询。

You can also have a look at what OData does for queries. They choose another approach with a single query parameter $filter. Here are some links that can give you some examples: https://msdn.microsoft.com/en-us/library/hh169248(v=nav.70) and http://www.odata.org/documentation/odata-version-3-0/url-conventions/. This option requires to have a grammar on the server side to parse your query.

在一般情况下,这个环节也可以给你在这个级别的一些提示在其部分过滤数据:的

In general, this link could also give you some hints at this level in its section "Filtering data": https://templth.wordpress.com/2014/12/15/designing-a-web-api/.

希望它给你一些有用的提示到您的RESTful服务中设计自己的查询; - )

Hope it gives you some helpful hints to design your queries within your RESTful services ;-)

蒂埃里

这篇关于如何德兴REST风格的高级搜索/过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 02:41