This is a document for PGroonga 2.X. See PGroonga 1.x document when you're using old PGroonga.

pgroonga_result_to_jsonb_objects function

Since 2.3.0.

Summary

pgroonga_result_to_jsonb_objects function is similar to pgroonga_result_to_recordset function.

pgroonga_result_to_jsonb_objects function converts pgroonga_command function's result JSON to a jsonb data that is an array of objects. You can convert the converted jsonb data to set of jsonb objects by PostgreSQL's jsonb_array_elements function. Generally, the converted jsonb data is is easier to handle rather than result JSON.

This will support all result JSON formats of Groonga command but the following commands are only supported for now:

Syntax

Here is the syntax of this function:

jsonb pgroonga_result_to_jsonb_objects(result)

result is a jsonb type value. You can cast pgroonga_command function's result text to jsonb to pass it to this function.

Usage

Here are sample schema and data:

CREATE TABLE memos (
  content text
);

CREATE INDEX pgroonga_memos_index
          ON memos
       USING pgroonga (content);

INSERT INTO memos VALUES ('PGroonga (PostgreSQL+Groonga) is great!');
INSERT INTO memos VALUES ('Groonga is an embeddable full text search engine.');

Here is an example to return jsonb data:

SELECT jsonb_pretty(
  pgroonga_result_to_jsonb_objects(
    pgroonga_command(
      'select',
      ARRAY[
        'table', pgroonga_table_name('pgroonga_memos_index')
      ]
    )::jsonb
  )
);
--                               jsonb_pretty                              
-- ------------------------------------------------------------------------
--  [                                                                     +
--      {                                                                 +
--          "_id": 1,                                                     +
--          "_key": 1,                                                    +
--          "content": "PGroonga (PostgreSQL+Groonga) is great!"          +
--      },                                                                +
--      {                                                                 +
--          "_id": 2,                                                     +
--          "_key": 2,                                                    +
--          "content": "Groonga is an embeddable full text search engine."+
--      }                                                                 +
--  ]
-- (1 row)

You can use PostgreSQL's jsonb_array_elements function to convert the converted jsonb data to set of jsonb objects:

SELECT jsonb_array_elements(
  pgroonga_result_to_jsonb_objects(
    pgroonga_command(
      'select',
      ARRAY[
        'table', pgroonga_table_name('pgroonga_memos_index')
      ]
    )::jsonb
  )
);
--                                  jsonb_array_elements                                  
-- ---------------------------------------------------------------------------------------
--  {"_id": 1, "_key": 1, "content": "PGroonga (PostgreSQL+Groonga) is great!"}
--  {"_id": 2, "_key": 2, "content": "Groonga is an embeddable full text search engine."}
-- (2 rows)

See also