Method: Sphinx::Client#AddQuery

Defined in:
lib/sphinx/sphinx/client.rb

#AddQuery(query, index = '*', comment = '') ⇒ Object

Add query to batch.

Batch queries enable searchd to perform internal optimizations, if possible; and reduce network connection overheads in all cases.

For instance, running exactly the same query with different groupby settings will enable searched to perform expensive full-text search and ranking operation only once, but compute multiple groupby results from its output.

Parameters are exactly the same as in Query call. Returns index to results array returned by RunQueries call.



565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
# File 'lib/sphinx/sphinx/client.rb', line 565

def AddQuery(query, index = '*', comment = '')
  # build request
  
  # mode and limits
  request = Request.new
  request.put_int @offset, @limit, @mode, @ranker, @sort
  request.put_string @sortby
  # query itself
  request.put_string query
  # weights
  request.put_int_array @weights
  # indexes
  request.put_string index
  # id64 range marker
  request.put_int 1
  # id64 range
  request.put_int64 @min_id.to_i, @max_id.to_i 
  
  # filters
  request.put_int @filters.length
  @filters.each do |filter|
    request.put_string filter['attr']
    request.put_int filter['type']

    case filter['type']
      when SPH_FILTER_VALUES
        request.put_int64_array filter['values']
      when SPH_FILTER_RANGE
        request.put_int64 filter['min'], filter['max']
      when SPH_FILTER_FLOATRANGE
        request.put_float filter['min'], filter['max']
      else
        raise SphinxInternalError, 'Internal error: unhandled filter type'
    end
    request.put_int filter['exclude'] ? 1 : 0
  end
  
  # group-by clause, max-matches count, group-sort clause, cutoff count
  request.put_int @groupfunc
  request.put_string @groupby
  request.put_int @maxmatches
  request.put_string @groupsort
  request.put_int @cutoff, @retrycount, @retrydelay
  request.put_string @groupdistinct
  
  # anchor point
  if @anchor.empty?
    request.put_int 0
  else
    request.put_int 1
    request.put_string @anchor['attrlat'], @anchor['attrlong']
    request.put_float @anchor['lat'], @anchor['long']
  end
  
  # per-index weights
  request.put_int @indexweights.length
  @indexweights.each do |idx, weight|
    request.put_string idx
    request.put_int weight
  end
  
  # max query time
  request.put_int @maxquerytime
  
  # per-field weights
  request.put_int @fieldweights.length
  @fieldweights.each do |field, weight|
    request.put_string field
    request.put_int weight
  end
  
  # comment
  request.put_string comment
  
  # attribute overrides
  request.put_int @overrides.length
  for entry in @overrides do
    request.put_string entry['attr']
    request.put_int entry['type'], entry['values'].size
    entry['values'].each do |id, val|
      assert { id.instance_of?(Fixnum) || id.instance_of?(Bignum) }
      assert { val.instance_of?(Fixnum) || val.instance_of?(Bignum) || val.instance_of?(Float) }
      
      request.put_int64 id
      case entry['type']
        when SPH_ATTR_FLOAT
          request.put_float val
        when SPH_ATTR_BIGINT
          request.put_int64 val
        else
          request.put_int val
      end
    end
  end
  
  # select-list
  request.put_string @select
  
  # store request to requests array
  @reqs << request.to_s;
  return @reqs.length - 1
end