search_facet (zotonic_mod_search v1.0.0-rc.17)

Faceted search using a facet.tpl for definition and a postgresql table for searches.

Summary

Functions

Generate the table for the facet pivoting. The definition of the columns is derived from the facet.tpl template.

Ensure that the facet table is correct, if not then drop the existing table and request a pivot of all resources to fill the table. Check if there active modules that are modules not running. A not-running module might have a facet definition we need for building the facet table.

Generate the face table definition from the facets.

Return all found values (or min/max for all facets).

Check if the current table is compatible with the facets in pivot.tpl

Pivot all resources to fill the facet table. This runs after every change to the the facet.tpl blocks.

Batch for running the facet table updates. This updates the table with 5000 resources at a time.

Pivot a resource, fill the facet table.

Add an extra search argument to the given query. Called by the query builder in search_query.erl

Recreate the facet table by first dropping it.

Add facets to the result set using the query. The facets are calculated using the query without facets, per facet a subselect is done to show the variations for that facet with respect to the other facets.

Add facets to the result set using the query. The facets are calculated using the result ids. Facets can be used for a "drill down".

Fetch all facet definitions from the current facet template.

Fetch all facet definitions from the current facet template as a map.

Types

facet_def/0

-type facet_def() ::
          #facet_def{name :: binary(), block :: atom(), type :: facet_type(), is_range :: boolean()}.

facet_type/0

-type facet_type() :: text | fulltext | fts | integer | float | boolean | datetime | id | ids | list.

Functions

create_table(Context)

-spec create_table(z:context()) -> ok | {error, term()}.

Generate the table for the facet pivoting. The definition of the columns is derived from the facet.tpl template.

ensure_table(Context)

-spec ensure_table(z:context()) -> ok | {error, term()}.

Ensure that the facet table is correct, if not then drop the existing table and request a pivot of all resources to fill the table. Check if there active modules that are modules not running. A not-running module might have a facet definition we need for building the facet table.

facet_def(Field, Context)

-spec facet_def(Field, Context) ->
                   {ok,
                    #facet_def{name :: binary(),
                               block :: atom(),
                               type :: facet_type(),
                               is_range :: boolean()}} |
                   {error, term()}
                   when Field :: binary(), Context :: z:context().

facet_table(Context)

-spec facet_table(z:context()) -> {ok, term()} | {error, term()}.

Generate the face table definition from the facets.

facet_values(Context)

-spec facet_values(z:context()) -> {ok, map()} | {error, term()}.

Return all found values (or min/max for all facets).

is_table_ok(Context)

-spec is_table_ok(z:context()) -> boolean().

Check if the current table is compatible with the facets in pivot.tpl

pivot_all(Context)

-spec pivot_all(z:context()) -> ok.

Pivot all resources to fill the facet table. This runs after every change to the the facet.tpl blocks.

pivot_batch(ToId, Context0)

Batch for running the facet table updates. This updates the table with 5000 resources at a time.

pivot_rsc(Id, Context)

-spec pivot_rsc(m_rsc:resource_id(), z:context()) -> ok | {error, term()}.

Pivot a resource, fill the facet table.

qterm(Field, Op, Value, Context)

-spec qterm(Field, Op, Value, Context) -> {ok, Term} | {error, term()}
               when
                   Field :: binary(),
                   Op :: binary() | undefined,
                   Value :: term(),
                   Term ::
                       #search_sql_term{label :: term(),
                                        select :: term(),
                                        tables :: term(),
                                        join_inner :: term(),
                                        join_left :: term(),
                                        where :: term(),
                                        sort :: term(),
                                        asort :: term(),
                                        zsort :: term(),
                                        cats :: term(),
                                        cats_exclude :: term(),
                                        cats_exact :: term(),
                                        extra :: term(),
                                        args :: term()},
                   Context :: z:context().

Add an extra search argument to the given query. Called by the query builder in search_query.erl

recreate_table(Context)

-spec recreate_table(z:context()) -> ok | {error, term()}.

Recreate the facet table by first dropping it.

search_query_facets(Result, Query, Context)

-spec search_query_facets(Result, Query, Context) -> NewResult
                             when
                                 Result ::
                                     #search_result{search_name :: binary() | atom(),
                                                    search_args :: map() | proplists:proplist(),
                                                    result :: list(),
                                                    page :: pos_integer(),
                                                    pagelen :: pos_integer() | undefined,
                                                    options :: z_search:search_options(),
                                                    total :: non_neg_integer() | undefined,
                                                    pages :: non_neg_integer() | undefined,
                                                    is_total_estimated :: boolean(),
                                                    next :: pos_integer() | false,
                                                    prev :: pos_integer(),
                                                    facets :: #{binary() => map()} | undefined},
                                 NewResult ::
                                     #search_result{search_name :: binary() | atom(),
                                                    search_args :: map() | proplists:proplist(),
                                                    result :: list(),
                                                    page :: pos_integer(),
                                                    pagelen :: pos_integer() | undefined,
                                                    options :: z_search:search_options(),
                                                    total :: non_neg_integer() | undefined,
                                                    pages :: non_neg_integer() | undefined,
                                                    is_total_estimated :: boolean(),
                                                    next :: pos_integer() | false,
                                                    prev :: pos_integer(),
                                                    facets :: #{binary() => map()} | undefined},
                                 Query ::
                                     #search_sql{select :: iodata(),
                                                 from :: iodata(),
                                                 where :: iodata(),
                                                 order :: iodata(),
                                                 group_by :: iodata(),
                                                 limit :: term(),
                                                 tables :: list(),
                                                 args :: list(),
                                                 cats :: list(),
                                                 cats_exclude :: list(),
                                                 cats_exact :: list(),
                                                 run_func :: function() | undefined,
                                                 post_func ::
                                                     fun((#search_result{search_name ::
                                                                             binary() | atom(),
                                                                         search_args ::
                                                                             map() |
                                                                             proplists:proplist(),
                                                                         result :: list(),
                                                                         page :: pos_integer(),
                                                                         pagelen ::
                                                                             pos_integer() | undefined,
                                                                         options ::
                                                                             z_search:search_options(),
                                                                         total ::
                                                                             non_neg_integer() |
                                                                             undefined,
                                                                         pages ::
                                                                             non_neg_integer() |
                                                                             undefined,
                                                                         is_total_estimated :: boolean(),
                                                                         next :: pos_integer() | false,
                                                                         prev :: pos_integer(),
                                                                         facets ::
                                                                             #{binary() => map()} |
                                                                             undefined},
                                                          #search_sql{},
                                                          z:context()) ->
                                                             #search_result{search_name ::
                                                                                binary() | atom(),
                                                                            search_args ::
                                                                                map() |
                                                                                proplists:proplist(),
                                                                            result :: list(),
                                                                            page :: pos_integer(),
                                                                            pagelen ::
                                                                                pos_integer() |
                                                                                undefined,
                                                                            options ::
                                                                                z_search:search_options(),
                                                                            total ::
                                                                                non_neg_integer() |
                                                                                undefined,
                                                                            pages ::
                                                                                non_neg_integer() |
                                                                                undefined,
                                                                            is_total_estimated ::
                                                                                boolean(),
                                                                            next ::
                                                                                pos_integer() | false,
                                                                            prev :: pos_integer(),
                                                                            facets ::
                                                                                #{binary() => map()} |
                                                                                undefined}) |
                                                     undefined,
                                                 extra :: list(),
                                                 assoc :: boolean(),
                                                 search_sql_terms :: list() | undefined},
                                 Context :: z:context().

Add facets to the result set using the query. The facets are calculated using the query without facets, per facet a subselect is done to show the variations for that facet with respect to the other facets.

search_query_subfacets(Result, Query, Context)

-spec search_query_subfacets(Result, Query, Context) -> NewResult
                                when
                                    Result ::
                                        #search_result{search_name :: binary() | atom(),
                                                       search_args :: map() | proplists:proplist(),
                                                       result :: list(),
                                                       page :: pos_integer(),
                                                       pagelen :: pos_integer() | undefined,
                                                       options :: z_search:search_options(),
                                                       total :: non_neg_integer() | undefined,
                                                       pages :: non_neg_integer() | undefined,
                                                       is_total_estimated :: boolean(),
                                                       next :: pos_integer() | false,
                                                       prev :: pos_integer(),
                                                       facets :: #{binary() => map()} | undefined},
                                    NewResult ::
                                        #search_result{search_name :: binary() | atom(),
                                                       search_args :: map() | proplists:proplist(),
                                                       result :: list(),
                                                       page :: pos_integer(),
                                                       pagelen :: pos_integer() | undefined,
                                                       options :: z_search:search_options(),
                                                       total :: non_neg_integer() | undefined,
                                                       pages :: non_neg_integer() | undefined,
                                                       is_total_estimated :: boolean(),
                                                       next :: pos_integer() | false,
                                                       prev :: pos_integer(),
                                                       facets :: #{binary() => map()} | undefined},
                                    Query ::
                                        #search_sql{select :: iodata(),
                                                    from :: iodata(),
                                                    where :: iodata(),
                                                    order :: iodata(),
                                                    group_by :: iodata(),
                                                    limit :: term(),
                                                    tables :: list(),
                                                    args :: list(),
                                                    cats :: list(),
                                                    cats_exclude :: list(),
                                                    cats_exact :: list(),
                                                    run_func :: function() | undefined,
                                                    post_func ::
                                                        fun((#search_result{search_name ::
                                                                                binary() | atom(),
                                                                            search_args ::
                                                                                map() |
                                                                                proplists:proplist(),
                                                                            result :: list(),
                                                                            page :: pos_integer(),
                                                                            pagelen ::
                                                                                pos_integer() |
                                                                                undefined,
                                                                            options ::
                                                                                z_search:search_options(),
                                                                            total ::
                                                                                non_neg_integer() |
                                                                                undefined,
                                                                            pages ::
                                                                                non_neg_integer() |
                                                                                undefined,
                                                                            is_total_estimated ::
                                                                                boolean(),
                                                                            next ::
                                                                                pos_integer() | false,
                                                                            prev :: pos_integer(),
                                                                            facets ::
                                                                                #{binary() => map()} |
                                                                                undefined},
                                                             #search_sql{},
                                                             z:context()) ->
                                                                #search_result{search_name ::
                                                                                   binary() | atom(),
                                                                               search_args ::
                                                                                   map() |
                                                                                   proplists:proplist(),
                                                                               result :: list(),
                                                                               page :: pos_integer(),
                                                                               pagelen ::
                                                                                   pos_integer() |
                                                                                   undefined,
                                                                               options ::
                                                                                   z_search:search_options(),
                                                                               total ::
                                                                                   non_neg_integer() |
                                                                                   undefined,
                                                                               pages ::
                                                                                   non_neg_integer() |
                                                                                   undefined,
                                                                               is_total_estimated ::
                                                                                   boolean(),
                                                                               next ::
                                                                                   pos_integer() | false,
                                                                               prev :: pos_integer(),
                                                                               facets ::
                                                                                   #{binary() => map()} |
                                                                                   undefined}) |
                                                        undefined,
                                                    extra :: list(),
                                                    assoc :: boolean(),
                                                    search_sql_terms :: list() | undefined},
                                    Context :: z:context().

Add facets to the result set using the query. The facets are calculated using the result ids. Facets can be used for a "drill down".

template_facets(Context)

-spec template_facets(z:context()) -> {ok, [facet_def()]} | {error, term()}.

Fetch all facet definitions from the current facet template.

template_facets_map(Context)

-spec template_facets_map(z:context()) -> {ok, [map()]} | {error, term()}.

Fetch all facet definitions from the current facet template as a map.