Python Solr与solrpy的索引问题
刚开始学习solr。我正在尝试使用solrpy作为客户机。我的python代码是:Python Solr与solrpy的索引问题,python,solr,Python,Solr,刚开始学习solr。我正在尝试使用solrpy作为客户机。我的python代码是: import solr # create a connection to a solr server s = solr.SolrConnection('http://localhost:8983/solr') # add a document to the index doc = dict( id='testid123', title='Lucene in Action', autho
import solr
# create a connection to a solr server
s = solr.SolrConnection('http://localhost:8983/solr')
# add a document to the index
doc = dict(
id='testid123',
title='Lucene in Action',
author=['Erik Hatcher', 'Otis Gospodneti'],
)
s.add(doc, commit=True)
# do a search
response = s.query('title:lucene')
for hit in response.results:
print hit['title']
这是从给出的例子
My solr schema.xml是solr发行版附带的默认模式。我没有对此做任何更改。它有一个唯一的关键字字段“id”
schema.xml文件位于solr-4.4.0/example/solr/collection1/conf中
我只是在示例目录中运行start.jar来运行solr
知道我哪里出错了吗?我没有使用Solr,所以我可能完全错了,但在示例中,您链接到
id
的是int
。尝试将您的id设置为int
,将id从'testid123'
更改为其他类似123
,看看会发生什么。我没有太多地使用solrpy(并且还没有安装它),但从最初的示例来看,它似乎希望使用属性=值对而不是字典来调用。(我知道您发布的示例来自在线0.9.2文档!但github上当前的源代码在注释中有此内容):
所以试试这个:
s.add(commit=True, **doc)
这可能会奏效。我不知道,您可能需要退出提交并单独执行
我不是一个solr专家,只是玩了一点,但我也比solrpy更幸运。也许值得一试
edit:github指向该文件的指针在这里:问题确实在于参数的传递方式。“提交”过程仍然存在一些问题。但是根据你的建议,我打算花些时间和sunburnt在一起。略读一下,文档看起来更详细。谢谢:)
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
Traceback (most recent call last):
File "/Users/user1/Documents/workspace/PyDelight/src/Test.py", line 12, in <module>
s.add(doc, commit=True)
File "/Library/Python/2.7/site-packages/solrpy-0.9.5-py2.7.egg/solr/core.py", line 678, in add
return Solr.add_many(self, [fields], commit=_commit)
File "/Library/Python/2.7/site-packages/solrpy-0.9.5-py2.7.egg/solr/core.py", line 326, in wrapper
return self._update(content, query)
File "/Library/Python/2.7/site-packages/solrpy-0.9.5-py2.7.egg/solr/core.py", line 550, in _update
rsp = self._post(selector, request, self.xmlheaders)
File "/Library/Python/2.7/site-packages/solrpy-0.9.5-py2.7.egg/solr/core.py", line 639, in _post
return check_response_status(self.conn.getresponse())
File "/Library/Python/2.7/site-packages/solrpy-0.9.5-py2.7.egg/solr/core.py", line 1097, in check_response_status
raise ex
solr.core.SolrException: HTTP code=400, reason=Bad Request
843169 [qtp1151734776-20] INFO org.apache.solr.update.processor.LogUpdateProcessor ? [collection1] webapp=/solr path=/update params={commit=true} {} 0 0
843170 [qtp1151734776-20] ERROR org.apache.solr.core.SolrCore ? org.apache.solr.common.SolrException: Document is missing mandatory uniqueKey field: id
add(**params)
Add a document. Pass in all document fields as
keyword parameters:
add(id='foo', notes='bar')
You must "commit" for the addition to be saved.
s.add(commit=True, **doc)