【问题标题】:Issue in fetching float attributes in Sphinx using Node.js使用 Node.js 在 Sphinx 中获取浮点属性的问题
【发布时间】:2012-03-13 10:00:38
【问题描述】:

我一直在尝试使用 Node.js 和石灰石获取 sphinx 数据。我从狮身人面像得到了一切,而不是浮点值。在我的 Sphinx 索引中,高度定义为浮点值(“sql_attr_float = Height”),但 Node.js 返回一些整数值。

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

请帮帮我。

以下是石灰石文件中使用的读取狮身人面像数据的函数,

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

【问题讨论】:

    标签: node.js sphinx


    【解决方案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 缺少浮点方法。

    因此,您需要弄清楚如何自己解码该值,并修补石灰石以直接处理浮点数,或者仅在应用程序中自己完成。

    检查 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-14
      • 1970-01-01
      • 2016-12-31
      • 1970-01-01
      • 1970-01-01
      • 2019-08-18
      • 1970-01-01
      • 2016-11-25
      相关资源
      最近更新 更多