You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by va...@apache.org on 2024/10/11 01:48:51 UTC

(couchdb) 03/10: send all stored values for a field

This is an automated email from the ASF dual-hosted git repository.

vatamane pushed a commit to branch catch-up-3.4.x
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit eb5d0239be33737eff71b2df6f012373a8fa3975
Author: Robert Newson <rn...@apache.org>
AuthorDate: Tue Oct 8 17:14:49 2024 +0100

    send all stored values for a field
    
    closes https://github.com/apache/couchdb/issues/5285
---
 src/nouveau/src/nouveau_fabric_search.erl | 19 +++++++++++--------
 test/elixir/test/config/nouveau.elixir    |  1 +
 test/elixir/test/nouveau_test.exs         | 31 +++++++++++++++++++++++++++++++
 3 files changed, 43 insertions(+), 8 deletions(-)

diff --git a/src/nouveau/src/nouveau_fabric_search.erl b/src/nouveau/src/nouveau_fabric_search.erl
index 5008df9d6..4ced63e14 100644
--- a/src/nouveau/src/nouveau_fabric_search.erl
+++ b/src/nouveau/src/nouveau_fabric_search.erl
@@ -159,15 +159,18 @@ simplify_hit(#{} = Hit) ->
     Hit#{<<"fields">> => simplify_fields(Fields)}.
 
 simplify_fields(Fields) when is_list(Fields) ->
-    Fun = fun(Field, Acc) ->
-        {Key, Value} = simplify_field(Field),
-        Acc#{Key => Value}
+    Grouped = maps:groups_from_list(
+        fun(#{<<"name">> := Name}) -> Name end,
+        fun(#{<<"value">> := Value}) -> Value end,
+        Fields
+    ),
+    %% If stored field has only one value, return it directly,
+    %% otherwise, return an array of them.
+    Fun = fun
+        (_, [V]) -> V;
+        (_, V) -> V
     end,
-    lists:foldl(Fun, #{}, Fields).
-
-simplify_field(#{<<"@type">> := <<"stored">>} = Field) ->
-    #{<<"name">> := Key, <<"value">> := Value} = Field,
-    {Key, Value}.
+    maps:map(Fun, Grouped).
 
 merge_fun(Sort) ->
     fun(HitA, HitB) ->
diff --git a/test/elixir/test/config/nouveau.elixir b/test/elixir/test/config/nouveau.elixir
index bff40e84f..1ea4605c0 100644
--- a/test/elixir/test/config/nouveau.elixir
+++ b/test/elixir/test/config/nouveau.elixir
@@ -8,6 +8,7 @@
     "search returns all items (paginated)",
     "search for foo:bar",
     "search for numeric ranges with locales",
+    "multiple values for stored field",
     "sort by string field (asc)",
     "sort by string field (desc)",
     "sort by numeric field (asc)",
diff --git a/test/elixir/test/nouveau_test.exs b/test/elixir/test/nouveau_test.exs
index 0fb280efb..5780fa55c 100644
--- a/test/elixir/test/nouveau_test.exs
+++ b/test/elixir/test/nouveau_test.exs
@@ -251,6 +251,37 @@ defmodule NouveauTest do
     assert ids == ["doc2"]
   end
 
+  @tag :with_db
+  test "multiple values for stored field", context do
+    db_name = context[:db_name]
+    resp = Couch.post("/#{db_name}/_bulk_docs",
+      headers: ["Content-Type": "application/json"],
+      body: %{:docs => [
+        %{"_id" => "doc1", "colors" =>
+           ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]}]}
+    )
+    assert resp.status_code in [201]
+    create_ddoc(db_name,
+      %{
+        nouveau: %{
+          bar: %{
+            default_analyzer: "standard",
+            index: """
+            function (doc) {
+            doc.colors.forEach((color) =>
+              index('string', 'color', color, {'store': true}));
+            }
+            """
+          }
+        }
+      })
+    url = "/#{db_name}/_design/foo/_nouveau/bar"
+    resp = Couch.post(url, body: %{q: "*:*", include_docs: true})
+    assert_status_code(resp, 200)
+    assert Enum.at(resp.body["hits"], 0)["doc"]["colors"] ==
+      ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]
+  end
+
   @tag :with_db
   test "sort by string field (asc)", context do
     db_name = context[:db_name]