使用Node.js在Sphinx中获取float属性时出现问题

时间:2012-03-13 10:00:39

标签: node.js sphinx

我一直在尝试使用Node.js和limestone来获取sphinx数据。我从sphinx获得了所有东西而不是浮点值。在我的Sphinx索引中,Height定义为float值(“sql_attr_float = Height”),但Node.js返回一些整数值。

例如:如果sphinx中的高度值是172.72,那么我从Node.js得到“1127004242”

请帮我解决这个问题。

以下是在石灰石文件中使用的函数,它读取sphinx数据,

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;
    }
  };
};

1 个答案:

答案 0 :(得分:1)

嗯,第715行, https://github.com/kurokikaze/limestone/blob/master/limestone.js 具有

            // 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;
            }

所以它只是将浮动读取为int32!

正如你所说toReader / makeWriter缺少float方法。

所以你需要弄清楚如何自己解码这个值,并修补limestone直接处理浮点数,或者只是在应用程序中自己完成。

检查SphinxAPI代码,这是在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可以做得有点天真

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

对于打包/解包的端口: pack / unpack functions for node.js