Creating a dynamic sized OR query using Sunburnt+Solr

python, solr, sunburnt

Solution

What you really want to do is this:

query = si.query()

for word in words:
    query |= si.Q(word)

or, as a one-liner

query = reduce(operator.or_, [si.Q(word) for word in words])

Problem

I'm trying out the python Solr interface Sunburnt , and I've come across a little problem I can't seem to figure out. From my search field, I want to accept an arbitrary number of words which I put in an array (e.g. "Music 'Iron Maiden'" -> ['Music', 'Iron Maiden']. This I've figured out (using shlex). The problem is that Sunburnt syntax for ORing terms is ``` response = si.query(si.Q(tag = 'Music') | si.Q(tag = 'Iron Maiden')) ``` How can I iterate over my searchword list and end up with something like the above? Or is there any other way of doing it that I'm not aware of?

Original source