【问题标题】:Solr indexing issue with solrpysolrpy 的 Solr 索引问题
【发布时间】:2013-10-01 09:16:49
【问题描述】:

刚开始学习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',
    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']

这是来自here 给出的示例

我的 solr schema.xml 是 solr 发行版附带的默认架构。我没有对此进行任何更改。它有一个 uniqueKey 字段作为“id”。

<uniqueKey>id</uniqueKey>

而且是字符串类型

<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

在 solr 跟踪端出现错误:

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

schema.xml 文件位于 solr-4.4.0/example/solr/collection1/conf

我通过简单地在示例目录中运行 start.jar 来运行 solr。

知道我哪里出错了吗?

【问题讨论】:

    标签: python solr


    【解决方案1】:

    我没有太多使用 solrpy(并且还没有安装它),但从最初的例子来看,它看起来想用属性=值对而不是字典来调用。 (我知道您发布的示例正好来自在线 0.9.2 文档!但是 github 上的当前来源在 cmets 中有这个):

    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)     
    

    它可能会起作用。你可能需要拉出提交并单独执行,我不知道。

    我不是 solr 专家,只是玩了一下,但我使用 sunburnt 比使用 solrpy 更幸运。值得一试,也许。

    编辑:指向该文件的 github 指针在这里:http://code.google.com/p/solrpy/source/browse/solr/core.py

    【讨论】:

    • 问题确实出在传递参数的方式上。 “提交”过程仍然存在一些问题。但是根据你的建议,我要花一些时间晒伤。浏览它,文档看起来更加详细。谢谢:)
    【解决方案2】:

    我没有使用过 Solr,所以我可能完全错了,但在示例中,您链接到 id 的是 int。尝试将您的 ID 设为 int,将您的 id 从 'testid123' 更改为 123 之类的其他名称,看看会发生什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-06
      • 2012-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-19
      • 1970-01-01
      • 2013-03-04
      相关资源
      最近更新 更多