python - 使用 Sunburnt+Solr 创建动态大小的 OR 查询

标签 python solr sunburnt

我正在试用 python Solr 接口(interface) Sunburnt,我遇到了一个我似乎无法弄清楚的小问题。从我的搜索字段中,我想接受我放入数组中的任意数量的单词(例如“音乐'Iron Maiden'” -> ['Music','Iron Maiden']。我已经弄清楚了(使用shlex)。

问题是 ORing 项的 Sunburnt 语法是

    response = si.query(si.Q(tag = 'Music') | si.Q(tag = 'Iron Maiden'))

我怎样才能遍历我的搜索词列表并得到类似上面的结果?或者还有其他我不知道的方法吗?

最佳答案

你真正想做的是:

query = si.query()

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

或者,作为单行

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

关于python - 使用 Sunburnt+Solr 创建动态大小的 OR 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11016994/

相关文章:

python - Amazon ec2 上的 Gunicorn 连接错误

python - 使用 Python 进行捕食者-猎物模拟。 Python 读取等式错误的问题?

python - 在 Pandas 数据框中计算点之间最短(欧几里得)距离的最快方法

javascript - 使用 jquery 调用从 SOLR 检索结果

python - 在 python 中使用 getattr 的替代方法?

python - Pandas 线图抑制了一半的 xticks,如何阻止它?

solr - id 字段为长类型

php - Apache SOLR HttpTransportException

python - key 错误 : 'id' when trying to index documents to Solr using sunburnt