<commit_before>%% -------------------------------------------------------------------
%%
%% Copyright (c) 2007-2010 Basho Technologies, Inc.  All Rights Reserved.
%%
%% -------------------------------------------------------------------

-module(riak_search_cmd).
-include("riak_search.hrl").

-export([
    %% Used by bin/search-cmd
    command/1,

    %% Used by riak_search_test:test/N
    set_schema/2
]).

-import(riak_search_utils, [to_list/1]).

usage() ->
    Usage = "~nUsage:

    search-cmd set_schema [INDEX] SCHEMAFILE : Set schema for an index.
    search-cmd show_schema [INDEX]           : Display the schema for an index.
    search-cmd shell [INDEX]                 : Start the interactive Search shell.
    search-cmd search [INDEX] QUERY          : Perform a search.
    search-cmd search_doc [INDEX] QUERY      : Perform a document search.
    search-cmd explain [INDEX] QUERY         : Display an execution plan.
    search-cmd index [INDEX] PATH            : Index files in a path.
    search-cmd delete [INDEX] PATH           : De-index files in a path.
    search-cmd solr [INDEX] PATH             : Run the Solr file.
    search-cmd install BUCKET                : Install kv/search integration hook
    search-cmd uninstall BUCKET              : Uninstall kv/search integration hook
    search-cmd test PATH                     : Run a test package
    ",
    io:format(Usage).

%% Set Schema
command([CurDir, "set_schema", SchemaFile]) ->
    SchemaFile1 = filename:join(CurDir, SchemaFile),
    set_schema(?DEFAULT_INDEX, SchemaFile1);
command([CurDir, "set_schema", Index, SchemaFile]) ->
    SchemaFile1 = filename:join(CurDir, SchemaFile),
    set_schema(Index, SchemaFile1);

%% Show Schema
command([_CurDir, "show_schema"]) ->
    show_schema(?DEFAULT_INDEX);
command([_CurDir, "show_schema", Index]) ->
    show_schema(Index);

%% Shell
command([_CurDir, "shell"]) -> 
    shell(?DEFAULT_INDEX);
command([_CurDir, "shell", Index]) ->
    shell(Index);

%% Search
command([_CurDir, "search", Query]) ->
    search(?DEFAULT_INDEX, Query);
command([_CurDir, "search", Index, Query]) ->
    search(Index, Query);

%% Serach Doc
command([_CurDir, "search_doc", Query]) ->
    search_doc(?DEFAULT_INDEX, Query);
command([_CurDir, "search_doc", Index, Query]) ->
    search_doc(Index, Query);

%% Explain
command([_CurDir, "explain", Query]) ->
    explain(?DEFAULT_INDEX, Query);
command([_CurDir, "explain", Index, Query]) ->
    explain(Index, Query);

%% Index
command([CurDir, "index", Path]) ->
    Path1 = filename:join(CurDir, Path),
    index(?DEFAULT_INDEX, Path1);
command([CurDir, "index", Index, Path]) ->
    Path1 = filename:join(CurDir, Path),
    index(Index, Path1);

command([CurDir, "delete", Path]) ->
    Path1 = filename:join(CurDir, Path),
    delete(?DEFAULT_INDEX, Path1);
command([CurDir, "delete", Index, Path]) ->
    Path1 = filename:join(CurDir, Path),
    delete(Index, Path1);

command([CurDir, "solr", Path]) ->
    Path1 = filename:join(CurDir, Path),
    solr(?DEFAULT_INDEX, Path1);
command([CurDir, "solr", Index, Path]) ->
    Path1 = filename:join(CurDir, Path),
    solr(Index, Path1);

command([CurDir, "test", Path]) ->
    Path1 = filename:join(CurDir, Path),
    test(Path1);

command([_CurDir, "install", Bucket]) ->
    riak_search_kv_hook:install(riak_search_utils:to_binary(Bucket));
command([_CurDir, "uninstall", Bucket]) ->
    riak_search_kv_hook:uninstall(riak_search_utils:to_binary(Bucket));

command(_) ->
    usage().

set_schema(Index, SchemaFile) -> 
    io:format("~n :: Updating schema for '~s'...~n", [Index]),
    case file:read_file(SchemaFile) of
        {ok, B} ->
            case riak_search_config:put_raw_schema(Index, B) of
                ok ->
                    io:format(" :: Done.~n");
                Error ->
                    io:format(" :: ERROR: ~p~n", [Error]),
                    erlang:exit(-1)
            end;
        _Error ->
            io:format(" :: ERROR: Could not read '~s'.~n", [SchemaFile]),
            erlang:exit(-1)
    end.

show_schema(Index) -> 
    io:format("~n%% Schema for '~s'~n", [Index]),
    case riak_search_config:get_raw_schema(Index) of<commit_msg>Print messages when installing or removing Search/KV integration hook.<commit_after>%% -------------------------------------------------------------------
%%
%% Copyright (c) 2007-2010 Basho Technologies, Inc.  All Rights Reserved.
%%
%% -------------------------------------------------------------------

-module(riak_search_cmd).
-include("riak_search.hrl").

-export([
    %% Used by bin/search-cmd
    command/1,

    %% Used by riak_search_test:test/N
    set_schema/2
]).

-import(riak_search_utils, [to_list/1]).

usage() ->
    Usage = "~nUsage:

    search-cmd set_schema [INDEX] SCHEMAFILE : Set schema for an index.
    search-cmd show_schema [INDEX]           : Display the schema for an index.
    search-cmd shell [INDEX]                 : Start the interactive Search shell.
    search-cmd search [INDEX] QUERY          : Perform a search.
    search-cmd search_doc [INDEX] QUERY      : Perform a document search.
    search-cmd explain [INDEX] QUERY         : Display an execution plan.
    search-cmd index [INDEX] PATH            : Index files in a path.
    search-cmd delete [INDEX] PATH           : De-index files in a path.
    search-cmd solr [INDEX] PATH             : Run the Solr file.
    search-cmd install BUCKET                : Install kv/search integration hook
    search-cmd uninstall BUCKET              : Uninstall kv/search integration hook
    search-cmd test PATH                     : Run a test package
    ",
    io:format(Usage).

%% Set Schema
command([CurDir, "set_schema", SchemaFile]) ->
    SchemaFile1 = filename:join(CurDir, SchemaFile),
    set_schema(?DEFAULT_INDEX, SchemaFile1);
command([CurDir, "set_schema", Index, SchemaFile]) ->
    SchemaFile1 = filename:join(CurDir, SchemaFile),
    set_schema(Index, SchemaFile1);

%% Show Schema
command([_CurDir, "show_schema"]) ->
    show_schema(?DEFAULT_INDEX);
command([_CurDir, "show_schema", Index]) ->
    show_schema(Index);

%% Shell
command([_CurDir, "shell"]) -> 
    shell(?DEFAULT_INDEX);
command([_CurDir, "shell", Index]) ->
    shell(Index);

%% Search
command([_CurDir, "search", Query]) ->
    search(?DEFAULT_INDEX, Query);
command([_CurDir, "search", Index, Query]) ->
    search(Index, Query);

%% Serach Doc
command([_CurDir, "search_doc", Query]) ->
    search_doc(?DEFAULT_INDEX, Query);
command([_CurDir, "search_doc", Index, Query]) ->
    search_doc(Index, Query);

%% Explain
command([_CurDir, "explain", Query]) ->
    explain(?DEFAULT_INDEX, Query);
command([_CurDir, "explain", Index, Query]) ->
    explain(Index, Query);

%% Index
command([CurDir, "index", Path]) ->
    Path1 = filename:join(CurDir, Path),
    index(?DEFAULT_INDEX, Path1);
command([CurDir, "index", Index, Path]) ->
    Path1 = filename:join(CurDir, Path),
    index(Index, Path1);

command([CurDir, "delete", Path]) ->
    Path1 = filename:join(CurDir, Path),
    delete(?DEFAULT_INDEX, Path1);
command([CurDir, "delete", Index, Path]) ->
    Path1 = filename:join(CurDir, Path),
    delete(Index, Path1);

command([CurDir, "solr", Path]) ->
    Path1 = filename:join(CurDir, Path),
    solr(?DEFAULT_INDEX, Path1);
command([CurDir, "solr", Index, Path]) ->
    Path1 = filename:join(CurDir, Path),
    solr(Index, Path1);

command([CurDir, "test", Path]) ->
    Path1 = filename:join(CurDir, Path),
    test(Path1);

command([_CurDir, "install", Bucket]) ->
    io:format("~n :: Installing Riak Search <--> KV hook on bucket '~s'.~n", [Bucket]),
    riak_search_kv_hook:install(riak_search_utils:to_binary(Bucket));
command([_CurDir, "uninstall", Bucket]) ->
    io:format("~n :: Removing Riak Search <--> KV hook from bucket '~s'.~n", [Bucket]),
    riak_search_kv_hook:uninstall(riak_search_utils:to_binary(Bucket));

command(_) ->
    usage().

set_schema(Index, SchemaFile) -> 
    io:format("~n :: Updating schema for '~s'...~n", [Index]),
    case file:read_file(SchemaFile) of
        {ok, B} ->
            case riak_search_config:put_raw_schema(Index, B) of
                ok ->
                    io:format(" :: Done.~n");
                Error ->
                    io:format(" :: ERROR: ~p~n", [Error]),
                    erlang:exit(-1)
            end;
        _Error ->
            io:format(" :: ERROR: Could not read '~s'.~n", [SchemaFile]),
            erlang:exit(-1)
    end.

show_schema(Index) -> 
    io:format("~n%% Schema for '~s'~n", [Index]),
    case riak_search_config:get_raw_schema(Index) of
{'fraction_non_alphanumeric': 0.18063768115942028, 'fraction_numerical': 0.007188405797101449, 'mean_word_length': 2.748804867448935, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '398623', 'n_tokens_mistral': 2753, 'n_tokens_neox': 2675, 'n_words': 649}