From 7e034250eb6cce57018535a066da6d4b1408e089 Mon Sep 17 00:00:00 2001 From: Zeeshan Lakhani Date: Wed, 26 Nov 2014 18:18:37 -0500 Subject: [PATCH 1/4] add create and show fns for search schema cli updates --- include/yokozuna.hrl | 4 ++- src/yz_console.erl | 67 +++++++++++++++++++++++++++++++++++++++++++- src/yz_schema.erl | 1 - 3 files changed, 69 insertions(+), 3 deletions(-) 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..1984d220 100644 --- a/src/yz_console.erl +++ b/src/yz_console.erl @@ -20,7 +20,11 @@ -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]). %% @doc Print the Active Anti-Entropy status to stdout. -spec aae_status([]) -> ok. @@ -51,3 +55,64 @@ 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. +-spec create_schema([string()|string()]) -> ok | schema_err(). +create_schema([Name, Path]) -> + try + 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", [FMTName]), + error + end + catch fileReadError:exitError -> + exitError + end. + +%% @doc Shows solr schema for name passed in. +-spec show_schema([string()]) -> ok | schema_err(). +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", [FMTName, binary_to_list(R)]), + ok; + {error, notfound} -> + io:format("Schema ~p doesn't exist~n", [FMTName]), + error + end. + +%% @doc +-spec add_to_schema([string()|string()]) -> ok | schema_err(). +add_to_schema([Name|Opts]) -> + {Name, Opts}. + +%% @doc +-spec remove_from_schema([string()|string()]) -> ok | schema_err(). +remove_from_schema([Name, FieldName]) -> + {Name, FieldName}. + +%%%=================================================================== +%%% Private +%%%=================================================================== + +%% @doc Reads and returns `RawSchema` from file path. +-spec read_schema(string()) -> raw_schema() | schema_err(). +read_schema(Path) -> + AbsPath = filename:absname(Path), + case file:read_file(AbsPath) of + {ok, RawSchema} -> + RawSchema; + {error, enoent} -> + io:format("No such file or directory: ~s~n", [Path]), + throw({fileReadError, enoent}); + {error, Reason} -> + ?ERROR("Error reading file ~s:~p", [Path, Reason]), + io:format("Error reading file ~s, see log for details~n", [Path]), + throw({fileReadError, Reason}) + end. diff --git a/src/yz_schema.erl b/src/yz_schema.erl index 90c294cd..de754898 100644 --- a/src/yz_schema.erl +++ b/src/yz_schema.erl @@ -24,7 +24,6 @@ -compile(export_all). -define(SCHEMA_VSN, "1.5"). --type schema_err() :: {error, string()}. %%%=================================================================== %%% API From 2d114fb8d3d334f2ad831545a421c369f8d21081 Mon Sep 17 00:00:00 2001 From: Zeeshan Lakhani Date: Thu, 4 Dec 2014 11:24:47 -0500 Subject: [PATCH 2/4] finish add/remove and cleanup, just need to have a yz_schema:*update* fun to call into --- src/yz_console.erl | 100 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 81 insertions(+), 19 deletions(-) diff --git a/src/yz_console.erl b/src/yz_console.erl index 1984d220..5c94538d 100644 --- a/src/yz_console.erl +++ b/src/yz_console.erl @@ -26,6 +26,15 @@ add_to_schema/1, remove_from_schema/1]). +-type field_data() :: {dynamicfield | field, list(), 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. aae_status([]) -> @@ -45,7 +54,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; @@ -57,50 +67,96 @@ switch_to_new_search([]) -> end. %% @doc Creates (and overrides) schema for name and file path. --spec create_schema([string()|string()]) -> ok | schema_err(). +%% riak-admin search schema create +-spec create_schema([string()|string()]) -> ok | error. create_schema([Name, Path]) -> try RawSchema = read_schema(Path), - FMTName = list_to_atom(Name), - case yz_schema:store(list_to_binary(Name), RawSchema) of + 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", [FMTName]), + io:format("Error creating schema ~p~n", [FMTName]), error end - catch fileReadError:exitError -> - exitError + catch {fileReadError, _} -> + error end. %% @doc Shows solr schema for name passed in. --spec show_schema([string()]) -> ok | schema_err(). +%% 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 + FMTName = ?LIST_TO_ATOM(Name), + case yz_schema:get(?LIST_TO_BINARY(Name)) of {ok, R} -> - io:format("Schema ~p:~n~s", [FMTName, binary_to_list(R)]), + io:format("Schema ~p:~n~s~n", [FMTName, binary_to_list(R)]), ok; {error, notfound} -> - io:format("Schema ~p doesn't exist~n", [FMTName]), + io:format("Schema ~p not found~n", [FMTName]), + error + end. + +%% @doc Adds field of to schema contents. +%% riak-admin search schema add field|dynamicfield [