为什么我与 Solr 的连接不起作用?

Why won't my connection to Solr work?

我正在使用 Scrapy 抓取网站,然后将该数据发送到 Solr 进行索引。数据通过使用 Solr Python 客户端之一的项目管道发送——mysolr。

蜘蛛工作正常,我的项目数组有两个项目具有正确的字段。该数组由管道中的 process_item 函数调用。

项目管道

from mysolr import Solr

class SolrPipeline(object):
    def __init__(self):
        self.client = Solr('http://localhost:8983/solr', version=4)
        response = self.client.search(q='Title')
        print response

    def process_item(self, item, spider):
        docs = [
            {'title' : item["title"],
             'subtitle' : item["subtitle"]   
            },
            {'title': item["title"],
             'subtitle': item["subtitle"]
            }
        ]
        print docs
        self.client.update(docs, 'json', commit=False)
        self.client.commit()

这是我遇到问题的地方。打印的响应是 < SolrResponse status=404 >。我使用了每当我启动 Solr 的 Admin UI 时出现的 SOLR_URL。

我得到的另一个错误如下。

2015-08-25 09:06:53 [urllib3.connectionpool] INFO: Starting new HTTP connection (1): localhost
2015-08-25 09:06:53 [urllib3.connectionpool] DEBUG: Setting read timeout to None
2015-08-25 09:06:53 [urllib3.connectionpool] DEBUG: "POST /update/json HTTP/1.1" 404 1278
2015-08-25 09:06:53 [urllib3.connectionpool] INFO: Starting new HTTP connection (1): localhost
2015-08-25 09:06:53 [urllib3.connectionpool] DEBUG: Setting read timeout to None
2015-08-25 09:06:53 [urllib3.connectionpool] DEBUG: "POST /update HTTP/1.1" 404 1273

这六行出现了两次(我想我想添加的每个项目一次)。

您想使用 JSON 数据执行 POST 请求,但实际上将 Python 字典列表传递给 self.client.update() 方法。

将 Python 字典列表转换为 JSON:

import json
from mysolr import Solr

class SolrPipeline(object):
    def __init__(self):
        self.client = Solr('http://localhost:8983/solr', version=4)
        response = self.client.search(q='Title')
        print response

    def process_item(self, item, spider):
        docs = [
            {'title' : item["title"],
             'subtitle' : item["subtitle"]   
            },
            {'title': item["title"],
             'subtitle': item["subtitle"]
            }
        ]

        docs = json.dumps(docs)  # convert to JSON
        self.client.update(docs, 'json', commit=False)
        self.client.commit()