[pylucene-dev] ConstantScoreQuery
tsuraan at tsuraan.net
tsuraan at tsuraan.net
Thu Aug 23 12:35:26 PDT 2007
The problem is that I have an automatic query builder that assembles
Lucene queries based off arbitrary user input. There are a few
different filters that are in use, so assembling them as parts of a
boolean query keeps my code simple; I used to have just one filtering
action that a user could do, and then I did have different code based
on whether the user used the filter or not, but once more filters were
added I found it a lot easier to convert them into queries to be added
to a boolean query tree.
On Aug 23, 2007, at 11:45 AM, Marc Weeber wrote:
> hi,
>
> The following code works for me:
>
> hope this helps?!
>
> best,
>
> Marc
>
>
> ----------------
>
>
> class AssociationFilter(object):
> """
> create a PyLucene Query filter to compute an association score
> between two items
>
> """
>
> def __init__(self, source, target):
> self._source = source
> self._target = target
>
> def bits(self, reader):
> bits = PyLucene.BitSet(reader.maxDoc())
> for id in [self._source, self._target]:
> termDocs = reader.termDocs(PyLucene.Term('uid', id))
> docs, freqs = termDocs.read(1)
> if len(docs) == 1:
> bits.set(docs[0])
> return bits
>
>
> filter = AssociationFilter(queryID, answerID)
> hits = searcher.search(query, filter)
>
>
>
> On 23-aug-2007, at 17:19, tsuraan at tsuraan.net wrote:
>
>> Is it possible to create a ConstantScoreQuery using a python-defined
>> filter? I'm trying the following, and I get the pasted error:
>>
>> >>> class MyFilter(object):
>> ... def bits(self, reader):
>> ... return BitSet()
>> ...
>> >>> f=MyFilter()
>> >>> q=ConstantScoreQuery(f)
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in ?
>> pybackend.thirdparty.PyLucene.lnxpyd.PyLucene.InvalidArgsError:
>> (<type 'PyLucene.ConstantScoreQuery'>, '__init__',
>> (<__main__.MyFilter object at 0xb7cda14c>,))
>>
>>
>> So, my other thought was maybe I need to inherit from the PyLucene
>> Filter object, but that doesn't work either:
>>
>> >>> class MyFilter(Filter):
>> ... def bits(self, reader):
>> ... return BitSet()
>> ...
>> >>> f=MyFilter()
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in ?
>> pybackend.thirdparty.PyLucene.lnxpyd.PyLucene.InvalidArgsError:
>> (<class '__main__.MyFilter'>, '__init__', ())
>>
>>
>> So, am I missing something, or is ConstantScoreQuery currently only
>> capable of using built-in filters?
>>
>> I'm using PyLucene 2.1.0-1 on Gentoo Linux with gcc 4.1.2; all tests
>> and sample code works for me.
>>
>> _______________________________________________
>> pylucene-dev mailing list
>> pylucene-dev at osafoundation.org
>> http://lists.osafoundation.org/mailman/listinfo/pylucene-dev
>
> _______________________________________________
> pylucene-dev mailing list
> pylucene-dev at osafoundation.org
> http://lists.osafoundation.org/mailman/listinfo/pylucene-dev
More information about the pylucene-dev
mailing list