Revision 774

Date:
2019/04/22 15:18:57
Author:
ahitrov
Revision Log:
Search through go-based JSON proxy

Files:

Legend:

 
Added
 
Removed
 
Modified
  • utf8/plugins/sphinx/lib/sphinx/Keeper.pm

     
    4 4 use warnings 'all';
    5 5 use base qw(Contenido::Keeper);
    6 6 use Contenido::Globals;
    7 use LWP::UserAgent;
    8 use JSON::XS;
    7 9 use Data::Dumper;
    8 10
    11 my $ua = LWP::UserAgent->new;
    12 $ua->timeout(1);
    13
    14
    9 15 ######################
    10 16 # Отправить объект в поиск:
    11 17 # Вызов: $keeper->set_search( $object );
     
    65 71
    66 72 # Методы поиска
    67 73 ####################################################################
    74 sub proxy {
    75 my $self = shift;
    76 my $text = shift;
    77 return unless $text;
    78 my $opts = shift // {};
    79
    80 my $db_table = delete $opts->{db_table} || $self->state->table_name;
    81 my $uri = URI->new( 'http://'.$self->state->db_host.':'.$self->state->db_port.'/search/'.$db_table );
    82 $uri->query_param( search => $text );
    83 if ( exists $opts->{object_class} ) {
    84 if ( ref $opts->{object_class} eq 'ARRAY' ) {
    85 $uri->query_param( class => $opts->{object_class} );
    86 } else {
    87 $uri->query_param( class => $opts->{object_class} );
    88 }
    89 }
    90 if ( exists $opts->{order_by} ) {
    91 my @Orders;
    92 if ( ref $opts->{order_by} eq 'ARRAY' ) {
    93 foreach my $order ( @{$opts->{order_by}} ) {
    94 if ( ref $order eq 'ARRAY' && scalar @$order > 0 && scalar @$order <= 2 ) {
    95 if ( scalar @$order == 2 && lc($order->[1]) eq 'desc' ) {
    96 push @Orders, { field => $order->[0], reverse => 'desc' };
    97 } else {
    98 push @Orders, { field => $order->[0] };
    99 }
    100 } elsif ( $order && !ref $order ) {
    101 my @parts = split /\s*,\s+/, $order;
    102 if ( scalar @parts == 2 && lc($parts[1]) eq 'desc' ) {
    103 push @Orders, { field => $parts[0], reverse => 'desc' };
    104 } else {
    105 push @Orders, { field => $parts[0] };
    106 }
    107 }
    108 }
    109 } else {
    110 my @orders = split /\s*,\s+/, $opts->{order_by};
    111 foreach my $order ( @orders ) {
    112 if ( $order =~ /^([\w\.]+)\s+(asc|desc)/i ) {
    113 push @Orders, { field => $1, reverse => $2 };
    114 } else {
    115 push @Orders, { field => $order };
    116 }
    117 }
    118 }
    119 my $i = 1;
    120 foreach my $order ( @Orders ) {
    121 $uri->query_param( "order_$i" => $order->{field} );
    122 if ( exists $order->{reverse} && lc($order->{reverse}) eq 'desc' ) {
    123 $uri->query_param( "sort_$i" => 'desc' );
    124 }
    125 last if ++$i > 5;
    126 }
    127 }
    128 if ( exists $opts->{limit} ) {
    129 $uri->query_param( limit => $opts->{limit} );
    130 }
    131 if ( exists $opts->{offset} ) {
    132 $uri->query_param( offset => $opts->{offset} );
    133 }
    134 warn "Ask sphinx: $uri\n" if $DEBUG;
    135 my $res = $ua->get($uri);
    136 if ($res->is_success) {
    137 my $body = $res->decoded_content;
    138 warn "Response: [$body]\n" if $DEBUG;
    139 my $result = decode_json $body;
    140 return $result;
    141 } else {
    142 warn "SPHINX PROXY error! Code: ".$res->code.". Status: ".$res->status_line."\n";
    143 }
    144 }
    145
    68 146 sub search {
    69 147 my $self = shift;
    70 148 my $text = shift;
  • utf8/plugins/sphinx/lib/sphinx/State.pm.proto

     
    18 18 $self->{tab_name} = 'sphinx';
    19 19
    20 20 # зашитая конфигурация плагина
    21 $self->{db_type} = 'remote'; ### For REAL database use 'remote'
    21 $self->{db_type} = 'proxy'; ### For REAL database use 'remote'
    22 22 $self->{db_keepalive} = 0;
    23 23 $self->{db_host} = '@SPHINX_HOST@';
    24 24 $self->{db_name} = '';