diff --git a/include/yokozuna.hrl b/include/yokozuna.hrl index b8dd7021..9982f024 100644 --- a/include/yokozuna.hrl +++ b/include/yokozuna.hrl @@ -89,6 +89,9 @@ -type ring_event() :: {ring_event, riak_core_ring:riak_core_ring()}. -type event() :: ring_event(). +%% @doc +-type schema_err() :: {error, string()}. + %% @doc The `component()' type represents components that may be %% enabled or disabled at runtime. Typically a component is %% disabled in a live, production cluster in order to isolate @@ -105,7 +108,6 @@ %% action to manually index the missing data or wait for AAE to %% take care of it. -type component() :: search | index. - %%%=================================================================== %%% Macros %%%=================================================================== diff --git a/src/yz_console.erl b/src/yz_console.erl index b96e4d0d..49de3a9b 100644 --- a/src/yz_console.erl +++ b/src/yz_console.erl @@ -20,7 +20,24 @@ -module(yz_console). -include("yokozuna.hrl"). -export([aae_status/1, - switch_to_new_search/1]). + switch_to_new_search/1, + create_schema/1, + show_schema/1, + add_to_schema/1, + remove_from_schema/1]). + +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). +-endif. + +-type field_data() :: {dynamicfield | field, list()}. + +-define(LIST_TO_ATOM(L), list_to_atom(L)). +-define(LIST_TO_BINARY(L), list_to_binary(L)). +-define(FIELD_DEFAULTS, [{type, "text_general"}, + {indexed, "true"}, + {stored, "false"}, + {multiValued, "true"}]). %% @doc Print the Active Anti-Entropy status to stdout. -spec aae_status([]) -> ok. @@ -41,7 +58,8 @@ aae_status([]) -> %% back without restarting the cluster. -spec switch_to_new_search([]) -> ok | {error, {nodes_down, [node()]}}. switch_to_new_search([]) -> - {_Good, Down} = riak_core_util:rpc_every_member_ann(yokozuna, switch_to_yokozuna, [], 5000), + {_Good, Down} = riak_core_util:rpc_every_member_ann( + yokozuna, switch_to_yokozuna, [], 5000), case Down of [] -> ok; @@ -51,3 +69,164 @@ switch_to_new_search([]) -> io:format(standard_error, "The following nodes could not be reached: ~s", [DownStr]), {error, {nodes_down, Down}} end. + +%% @doc Creates (and overrides) schema for name and file path. +%% riak-admin search schema create +-spec create_schema([string()|string()]) -> ok | error. +create_schema([Name, Path]) -> + try + {ok, RawSchema} = read_schema(Path), + FMTName = ?LIST_TO_ATOM(Name), + case yz_schema:store(?LIST_TO_BINARY(Name), RawSchema) of + ok -> + io:format("~p schema created~n", [FMTName]), + ok; + {error, _} -> + io:format("Error creating schema ~p~n", [FMTName]), + error + end + catch {fileReadError, _} -> + error + end. + +%% @doc Shows solr schema for name passed in. +%% riak-admin search schema show +-spec show_schema([string()]) -> ok | error. +show_schema([Name]) -> + FMTName = ?LIST_TO_ATOM(Name), + case yz_schema:get(?LIST_TO_BINARY(Name)) of + {ok, R} -> + io:format("Schema ~p:~n~s~n", [FMTName, binary_to_list(R)]), + ok; + {error, notfound} -> + io:format("Schema ~p not found~n", [FMTName]), + error + end. + +%% @doc Adds field of to schema contents. +%% riak-admin search schema add field|dynamicfield [