python - 连接到在本地主机上运行的 solr 服务器

标签 python solr connection server localhost

我正在尝试连接到 solr服务器使用 this教程。至此,我确信我的 solr 设置正确。我能跑了

> solr start -p 8983

它似乎启动了一些东西。

果然

> solr status
Solr process 31421 running on port 8983

现在在我的 python 代码中,我尝试了我认为应该是基本连接脚本的内容。

import solr
host = "http://localhost:8983/solr"

# also tried
# host = "http://localhost:8983"

# also tried
# host = "http://127.0.0.1:8983/solr"

# also tried
# host = "http://127.0.0.1:8983"

connection = solr.SolrConnection(host)

try:
    connection.add(
        id= 1,
        title= "Lucene in Action",
        author= ['Zack', 'Hank Hill']
    )
except Exception as e:
    import pdb
    pdb.set_trace()

connection.commit()

我的代码从未到达connection.commit(),相反,它到达了异常中的调试点。查看异常 e

HTTP code=404, Reason=Not Found, body=<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>Error 404 Not Found</title>
</head>
<body><h2>HTTP ERROR 404</h2>
    <p>Problem accessing /solr/update. Reason:
    <pre>    Not Found</pre></p><hr><i><small>Powered by Jetty://</small></i><hr/>
</body>
</html>

所以看起来 python 客户端由于 404 而找不到 solr 服务器?这看起来应该很简单,所以我不确定我在这里搞砸了。有人能指出我正确的方向吗?

编辑:我添加了这个脚本来检查各种主机,不行

hosts = [
    'http://localhost:8983/solr',
    'http://localhost:8983',
    'http://127.0.0.1:8983/solr',
    'http://127.0.0.1:8983'
]

def connect(host):
    connection = solr.SolrConnection(host)
    try:
        connection.add(
            id= 1,
            title='Lucene in Action',
            author= ['Zack Botkin', 'Hank Hill']
        )
    except:
        raise

for host in hosts:
    try:
        connect(host)
    except Exception as e:
        import pdb
        pdb.set_trace()

每个异常都是一样的,404错误

编辑2:我能够

> telnet localhost 8983

它已连接,所以我很确定 solr 服务器正在该端口上运行?

最佳答案

要使用 solr 建立索引,您还需要创建一个核心,并确保在您的 url 中使用该核心。例如,启动 solr 后,运行以下命令来创建名为 test 的核心:

solr创建-c测试

创建后,您应该会看到它列在 solr 管理页面中。要使用它,您只需将该核心名称添加到您的连接 URL 中即可。简单的Python代码示例:

import solr

# create a connection to a solr server
s = solr.SolrConnection('http://localhost:8983/solr/test')

# add 2 documents to the index
s.add(id=1, title='Lucene in Action', author=['bob', 'asdf'])
s.add(id=2, title='test2', author=['Joe', 'test'])
s.commit()

# do a search
response = s.query('joe')
for hit in response.results:
    print hit['title']

更多信息请点击 https://cwiki.apache.org/confluence/display/solr/Running+Solr

关于python - 连接到在本地主机上运行的 solr 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33336626/

相关文章:

python - 我可以在不先使用 gspread 获取当前值的情况下更新单元格吗

python - 使用Python中的循环将项目添加到现有字典中

python - 全文搜索: Whoosh Vs SOLR

python - 使用python将数据插入mongodb

Python 单元测试和何时模拟

python - Python-使用 “FLANN”- “Type Error”

solr - 在使用 nutch 和 solr 抓取或索引期间从 html 中删除菜单

solr - 根据 Solr 中的字段长度更改文档提升?

android - 如何使用 (a) smack 在 Android 上保持 XMPP 连接稳定?

php - 无法更改 Doctrine 连接