Inist-CNRS / node-sphinxapi

Native javascript implementation of the standard Sphinx API
https://www.npmjs.com/package/sphinxapi
Other
76 stars 20 forks source link

SetFieldWeights broken, or am I using it incorrectly. #9

Open gyaaniguy opened 11 years ago

gyaaniguy commented 11 years ago
cl.SetFieldWeights({
        question : 20,
        tags : 50,
        body : 10
    });

This throws the error: AssertionError: "undefined" == "object" in line ../node_modules/sphinxapi/lib/sphinxapi.js:315:9)

This is the code. I think I notice a couple of mistakes there...

SphinxClient.prototype.SetFieldWeights = function (weights) {
  var self = this
    assert.equal(typeof item, 'object')
    forEach(weights, function (item, index) {
            assert.equal(typeof item, 'number')
    })
    self._fieldweights = weights
};

Please have a look. Thanks

gyaaniguy commented 11 years ago

Hey thanks for the quick response! But There are a few of more changes I had to make to make this work: BUT i am no expert! and wasn't exactly sure with every change.

  1. The following change is assuming that the value(in the passed object) should be a number.
forEach(weights, function (item, index) {
    assert.equal(typeof item, 'number')
})

Should be

forEach(weights, function (item, index) {
    assert.equal(typeof index, 'number')
})
  1. I just removed pb and it started working.
req.push(pb.pack('>L', [len(field)])) 
to
req.push(pack('>L', [len(field)]))
  1. This one I am sure of , obj should be o.
for(k in o) {
    l += Number( obj.hasOwnProperty(k) );
}

TO

for(k in o) { l += Number( o.hasOwnProperty(k) ); }

touv commented 11 years ago

you found an mistake in the foreach wrapper... I inverted the arguments of the callback.

I 'll fix all the bugs as soon as possible

touv commented 11 years ago
  1. fixed : c04cd575e562fb0a9c586edd3d4ebad19a6d9307
  2. fixed : c8f7440a7400f3651b5679d95092e04a824d5ceb
  3. fixed : adf486addaadf6236223fb3bd6db157c59c8481d
gyaaniguy commented 11 years ago

EDIT: SORRY! I was wrong , it is working. Just that the results are not in the order that I expected.

I hate being a bother, but I think it still could be broken. SetFieldWeights doesn't seem to have any effect. Could you please look into this .

Thanks

Here's my code btw: var searchQuery = '' + str; cl.SetServer('localhost', sphinxPort); cl.SetFieldWeights({ tagds: 15000, qdt: 30000, dqb: 1 }); cl.SetMatchMode(SphinxClient.SPH_MATCH_EXTENDED);

touv commented 11 years ago

Sorry, I forgot to change a line. it was fixed with this commit : b3ca39ea163649a57df5dbcf862eee60cce96c6e. I also wrote a little test to compare an search and the same search with different weights

https://github.com/touv/node-sphinxapi-test/blob/master/query.js https://github.com/touv/node-sphinxapi-test/blob/master/setfieldsweights.js

I compare the result with NodeJS's API and the official PHP's API. It looks the same.

https://github.com/touv/node-sphinxapi-test/blob/master/query.php https://github.com/touv/node-sphinxapi-test/blob/master/setfieldsweights.php

Tell me, if I can close this issue ?