使用 python 向 Solr 添加数据时遇到问题?
Trouble adding data to Solr with python?
我正在使用 python 3.4.3 和 sunburnt 将一些文档添加到 Solr(5.2.1)。以下代码直接来自 Sunburnt 文档:
import sunburnt
si=sunburnt.SolrInterface("http://localhost:8983/solr/")
document = {"id":"0553573403",
"cat":"book",
"name":"A Game of Thrones",
"price":7.99,
"inStock": True,
"author_t":
"George R.R. Martin",
"series_t":"A Song of Ice and Fire",
"sequence_i":1,
"genre_s":"fantasy"}
si.add(document)
当我 运行 上述命令时,我得到以下信息:
NameError Traceback (most recent call last)
<ipython-input-1-1008a9ce394f> in <module>()----> 1 import sunburnt
2
3 si= sunburnt.SolrInterface("http://localhost:8983/solr/")
4
5 document = {"id":"0553573403",
/Users/rmohan/venv_py3/lib/python3.4/site-packages/sunburnt/__init__.py in <module>()
1 from __future__ import absolute_import
2
----> 3 from .strings import RawString
4 from .sunburnt import SolrError, SolrInterface
5
/Users/rmohan/venv_py3/lib/python3.4/site-packages/sunburnt/strings.py in <module>()
2
3
----> 4 class SolrString(unicode):
5 # The behaviour below is only really relevant for String fields rather
6 # than Text fields - most queryparsers will strip these characters out
NameError: name 'unicode' is not defined
所以我用pysolr尝试了同样的文档如下:
import pysolr
solr = pysolr.Solr('http://localhost:8983/solr/', timeout=10)
document = [{"id":"0553573403",
"cat":"book",
"name":"A Game of Thrones",
"price":7.99,
"inStock": True,
"author_t":
"George R.R. Martin",
"series_t":"A Song of Ice and Fire",
"sequence_i":1,
"genre_s":"fantasy"}]
solr.add(document)
给出以下内容:
/Users/rmohan/venv_py3/lib/python3.4/site-packages/pysolr.py in _scrape_response(self, headers, response)
443 dom_tree = None
444
--> 445 if response.startswith('<?xml'):
446 # Try a strict XML parse
447 try:
TypeError: startswith first arg must be bytes or a tuple of bytes, not str
我进行了一些谷歌搜索,但找不到关于如何解决输入的 unicode 或字节问题的明确答案。我尝试将字符串转换为字节和 unicode,但似乎没有任何效果。
如果有人知道在 SOLR 中插入文档的更好方法,请分享。任何帮助将不胜感激。
我明白了,文件 pysolr.py 有
if response.startswith('<?xml'):
需要更改为
if response.startswith(b'<?xml'):
我正在使用 python 3.4.3 和 sunburnt 将一些文档添加到 Solr(5.2.1)。以下代码直接来自 Sunburnt 文档:
import sunburnt
si=sunburnt.SolrInterface("http://localhost:8983/solr/")
document = {"id":"0553573403",
"cat":"book",
"name":"A Game of Thrones",
"price":7.99,
"inStock": True,
"author_t":
"George R.R. Martin",
"series_t":"A Song of Ice and Fire",
"sequence_i":1,
"genre_s":"fantasy"}
si.add(document)
当我 运行 上述命令时,我得到以下信息:
NameError Traceback (most recent call last)
<ipython-input-1-1008a9ce394f> in <module>()----> 1 import sunburnt
2
3 si= sunburnt.SolrInterface("http://localhost:8983/solr/")
4
5 document = {"id":"0553573403",
/Users/rmohan/venv_py3/lib/python3.4/site-packages/sunburnt/__init__.py in <module>()
1 from __future__ import absolute_import
2
----> 3 from .strings import RawString
4 from .sunburnt import SolrError, SolrInterface
5
/Users/rmohan/venv_py3/lib/python3.4/site-packages/sunburnt/strings.py in <module>()
2
3
----> 4 class SolrString(unicode):
5 # The behaviour below is only really relevant for String fields rather
6 # than Text fields - most queryparsers will strip these characters out
NameError: name 'unicode' is not defined
所以我用pysolr尝试了同样的文档如下:
import pysolr
solr = pysolr.Solr('http://localhost:8983/solr/', timeout=10)
document = [{"id":"0553573403",
"cat":"book",
"name":"A Game of Thrones",
"price":7.99,
"inStock": True,
"author_t":
"George R.R. Martin",
"series_t":"A Song of Ice and Fire",
"sequence_i":1,
"genre_s":"fantasy"}]
solr.add(document)
给出以下内容:
/Users/rmohan/venv_py3/lib/python3.4/site-packages/pysolr.py in _scrape_response(self, headers, response)
443 dom_tree = None
444
--> 445 if response.startswith('<?xml'):
446 # Try a strict XML parse
447 try:
TypeError: startswith first arg must be bytes or a tuple of bytes, not str
我进行了一些谷歌搜索,但找不到关于如何解决输入的 unicode 或字节问题的明确答案。我尝试将字符串转换为字节和 unicode,但似乎没有任何效果。
如果有人知道在 SOLR 中插入文档的更好方法,请分享。任何帮助将不胜感激。
我明白了,文件 pysolr.py 有
if response.startswith('<?xml'):
需要更改为
if response.startswith(b'<?xml'):