连接到本地主机上的 solr 服务器 运行
Connect to solr server running on localhost
我正在尝试连接到 solr server using 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
我正在尝试连接到 solr server using 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