diff options
author | Robert Newson <rnewson@apache.org> | 2023-04-10 22:45:21 +0100 |
---|---|---|
committer | Robert Newson <rnewson@apache.org> | 2023-04-22 11:20:02 +0100 |
commit | 45d9dc84fd7b1ffb2db04014412b6a15f0e003df (patch) | |
tree | 584eaf5233f499e3b114c5717b691f5f7a2c4c62 | |
parent | 19c2764e25e33ec7e94732ea15e090b4969bcf2d (diff) | |
download | couchdb-45d9dc84fd7b1ffb2db04014412b6a15f0e003df.tar.gz |
tighten sort field type check and enhance docs
-rw-r--r-- | nouveau/src/main/java/org/apache/couchdb/nouveau/lucene9/Lucene9Index.java | 19 | ||||
-rw-r--r-- | src/docs/src/api/ddoc/nouveau.rst | 15 | ||||
-rw-r--r-- | src/nouveau/src/nouveau_httpd.erl | 2 |
3 files changed, 24 insertions, 12 deletions
diff --git a/nouveau/src/main/java/org/apache/couchdb/nouveau/lucene9/Lucene9Index.java b/nouveau/src/main/java/org/apache/couchdb/nouveau/lucene9/Lucene9Index.java index 64b55aa5c..27105d6b9 100644 --- a/nouveau/src/main/java/org/apache/couchdb/nouveau/lucene9/Lucene9Index.java +++ b/nouveau/src/main/java/org/apache/couchdb/nouveau/lucene9/Lucene9Index.java @@ -79,7 +79,7 @@ public class Lucene9Index extends Index { private static final Sort DEFAULT_SORT = new Sort(SortField.FIELD_SCORE, new SortField("_id", SortField.Type.STRING)); - private static final Pattern SORT_FIELD_RE = Pattern.compile("^([-+])?([\\.\\w]+)(?:<(\\w+)>)?$"); + private static final Pattern SORT_FIELD_RE = Pattern.compile("^([-+])?([\\.\\w]+)(?:<(\\w+)>)$"); private final Analyzer analyzer; private final IndexWriter writer; @@ -329,15 +329,26 @@ public class Lucene9Index extends Index { } private SortField convertSortField(final String sortString) { + if ("relevance".equals(sortString)) { + return SortField.FIELD_SCORE; + } final Matcher m = SORT_FIELD_RE.matcher(sortString); if (!m.matches()) { throw new WebApplicationException( sortString + " is not a valid sort parameter", Status.BAD_REQUEST); } final boolean reverse = "-".equals(m.group(1)); - SortField.Type type = SortField.Type.DOUBLE; - if ("string".equals(m.group(3))) { - type = SortField.Type.STRING; + SortField.Type type; + switch (m.group(3)) { + case "string": + type = SortField.Type.STRING; + break; + case "number": + type = SortField.Type.DOUBLE; + break; + default: + throw new WebApplicationException( + m.group(3) + " is not a valid sort type", Status.BAD_REQUEST); } return new SortField(m.group(2), type, reverse); } diff --git a/src/docs/src/api/ddoc/nouveau.rst b/src/docs/src/api/ddoc/nouveau.rst index e387bd1f8..d38d16e43 100644 --- a/src/docs/src/api/ddoc/nouveau.rst +++ b/src/docs/src/api/ddoc/nouveau.rst @@ -58,16 +58,15 @@ and optional ``min_inclusive`` and ``max_inclusive`` properties (defaulting to ``true`` if not specified). Example: ``{"bar":[{"label":"cheap","min":0,"max":100}]}`` - :query json sort: Specifies the sort order of the results. In a grouped search (when - ``group_field`` is used), this parameter specifies the sort order within a group. + :query json sort: Specifies the sort order of the results. The default sort order is relevance. A JSON string of the form ``"fieldname<type>"`` or ``"-fieldname<type>"`` for descending order, where - fieldname is the name of a string or number field, and ``type`` is either a - number, a string, or a JSON array of strings. The ``type`` part is optional, and - defaults to number. Some examples are ``"foo"``, ``"-foo"``, ``"bar<string>"``, - ``"-foo<number>"`` and [``"-foo<number>"``, ``"bar<string>"``]. String fields that - are used for sorting must not be analyzed fields. Fields that are used for sorting - must be indexed by the same indexer that is used for the search query. + fieldname is the name of a string or number field, and ``type`` is either + ``number`` or ``string``. You can use a single string to sort by one field + or an array of strings to sort by several fields in the same order as the + array. + Some examples are ``"relevance"``, ``"bar<string>"``, + ``"-foo<number>"`` and [``"-foo<number>"``, ``"bar<string>"``]. :query boolean update: Set to ``false`` to allow the use of an out-of-date index. :>header Content-Type: - :mimetype:`application/json` diff --git a/src/nouveau/src/nouveau_httpd.erl b/src/nouveau/src/nouveau_httpd.erl index 899b34f09..14360d940 100644 --- a/src/nouveau/src/nouveau_httpd.erl +++ b/src/nouveau/src/nouveau_httpd.erl @@ -186,6 +186,8 @@ validate_query_arg(limit, List) when is_list(List) -> end; validate_query_arg(sort, undefined) -> null; +validate_query_arg(sort, {json, Sort}) when is_binary(Sort) -> + [Sort]; validate_query_arg(sort, {json, Sort}) -> ok = is_list_of_strings(<<"counts">>, Sort), Sort; |