0

I have been trying to fetch sphinx data using Node.js and limestone. I got everything from sphinx instead the float value. In my Sphinx index Height is defined as float value ("sql_attr_float = Height") and but the Node.js returning some integer value.

Eg: if Height value in sphinx is 172.72, then i got "1127004242" from the Node.js

Please help me on this.

Below is the function used in limestone file which reads sphinx data,

proto.toReader = function toReader() {
  var offset = 0, length = this.length, buffer = this;
  return {
    empty: function empty() {
      return offset >= length;
    },
    int64: function shiftInt64() {
      var hi_low_pair = buffer.int64Read(offset);
      offset += 8;
      return hi_low_pair;
    },
    int32: function shiftInt32() {
      var number = buffer.int32Read(offset);
      offset += 4;
      return number;
    },
    int16: function shiftInt16() {
      var number = buffer.int16Read(offset);
      offset += 2;
      return number;
    },
    buffer: function shiftBuffer(length) {
      var b = buffer.slice(offset, offset + length);
      offset += length;
      return b;
    },
    string: function shiftString(length) {
      var string = buffer.toString('utf8', offset, offset + length);
      offset += length;
      return string;
    },
    cstring: function shiftCstring() {
      var end = offset;
      while (buffer[end] > 0 && end < length) { end++; }
      var string = buffer.toString('utf8', offset, end);
      offset = end + 1;
      return string;
    },
    lstring: function shiftLString() {
      var length = buffer.int32Read(offset);
      offset += 4;          

      if(!isNaN(length) && !isNaN(offset)){
        length = length;
        var string = buffer.toString('utf8', offset, offset + length);
      }else{
        var string = "";
      }   

      offset += length;
      return string;
    },  
    multicstring: function shiftMulticstring() {
      var fields = [];
      while (buffer[offset] > 0) {
        fields.push(this.cstring());
      }
      offset++;
      return fields;
    },
    hash: function shiftHash() {
      var hash = {};
      while (buffer[offset] > 0) {
        hash[this.cstring()] = this.cstring();
      }
      offset++;
      return hash;
    }
  };
};
Raja
  • 3,477
  • 12
  • 47
  • 89

1 Answers1

1

Hmm, line 715, of https://github.com/kurokikaze/limestone/blob/master/limestone.js has

            // FLOAT size attributes (32 bits)
            if (output.attributes[attribute].type == Sphinx.attribute.FLOAT) {
                attr_value = response.int32();
                match.attrs[output.attributes[attribute].name] = attr_value;
                continue;
            }

So its just reading the float as int32!

And as you say toReader/makeWriter is missing float methods.

So you need to figure out how to decode that value yourself, and either patch limestone to directly handle floats, or just do it yourself in the application.

Checking SphinxAPI code, this is how it done in php:

                                    // handle floats
                                    if ( $type==SPH_ATTR_FLOAT )
                                    {
                                            list(,$uval) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
                                            list(,$fval) = unpack ( "f*", pack ( "L", $uval ) );
                                            $attrvals[$attr] = $fval;
                                            continue;
                                    }

java can do it somewhat nativly

                                            /* handle floats */
                                            if ( type==SPH_ATTR_FLOAT )
                                            {
                                                    docInfo.attrValues.add ( attrNumber, new Float ( in.readFloat() ) );
                                                    continue;
                                            }

For ports of pack/unpack: pack / unpack functions for node.js

Community
  • 1
  • 1
barryhunter
  • 20,886
  • 3
  • 30
  • 43