关于elasticsearch php的使用
这是文件, 问题在下面的屏幕截图中: 编辑: 例如,我想在下面的示例中获得搜索结果,如何编写控制器 视图:关于elasticsearch php的使用,php,laravel,
elasticsearch,Php,Laravel,
elasticsearch,这是文件, 问题在下面的屏幕截图中: 编辑: 例如,我想在下面的示例中获得搜索结果,如何编写控制器 视图: <html> <head> <meta charset="utf-8"> <link href="https://cdn.bootcss.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" rel="stylesheet"> <link href="https
<html>
<head>
<meta charset="utf-8">
<link href="https://cdn.bootcss.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.bootcss.com/tether/1.3.2/css/tether.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<nav class="navbar navbar-light bg-faded">
<a class="navbar-brand" href="#">Navbar</a>
<ul class="nav navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
</ul>
<form class="form-inline pull-xs-right">
<input class="form-control" type="text" placeholder="Search">
<button class="btn btn-success-outline" type="submit">Search</button>
</form>
</nav>
</div>
<script src="https://cdn.bootcss.com/jquery/2.2.3/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/tether/1.3.2/js/tether.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/4.0.0-alpha.2/js/bootstrap.min.js"></script>
</body>
</html>
-
-
-
-
搜寻
路线:
<?php
Route::group(['middleware' => 'web'], function () {
Route::resource('/search', 'SearchController');
});
在我看来,他们似乎只提供了有关如何安装composer的说明,但您仍然需要使用以下方法来实际使用composer软件包:
composer require elasticsearch/elasticsearch
如果您运行composer安装,它应该为您自动加载,以便您可以从任何地方调用它。从那时起,您可以在需要的地方实例化Elasticsearch clientbuilder
“左边的代码”是您得到的返回响应,它是转换为php数组的elasticsearch json
要真正开始跑步,您需要:
$client = Elasticsearch\ClientBuilder::create()->build();
$params = [
'index' => 'my_index',
'type' => 'my_type',
'id' => 'my_id',
'body' => ['testField' => 'abc']
];
$response = $client->index($params);
print_r($response);
以上内容应该是您所需要的,您需要将参数更改为您的设置,将主体更改为您的搜索查询
编辑
查看了composer.json编辑,因此您实际上需要编写composer require,因为它已经在文件中。简单的“composer安装”就足够了
这就是我迅速想出的方法,我尝试了索引方法,效果很好。在本例中,我仍然有一个索引名为“node”的elasticsearch服务器和一种类型的“假期”,这需要根据您的个人elasticsearch服务器进行更改。当然,这里的逻辑是要有一种“用户”
class UsersController extends Controller
{
// the main elasticsearch instance created with the constructor
protected $client;
public function __construct() {
$hosts = [
// since I am using homestead I have to refer to the ip address of my host machine on which I have installed
// elasticcsearch, otherwise the default localhost option will point to homestead localhost
'192.168.178.10:9200'
];
$this->client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
}
public function index()
{
$params = [
'index' => 'node',
'type' => 'vacation',
'body' => [
'query' => [
'match_all' => []
]
]
];
$response = $this->client->search($params);
print_r($response);
}
public function create()
{
$params = [
'index' => 'node',
'type' => 'vacation',
'id' => '1029',
'body' => [
'query' => [
'match_all' => []
]
]
];
$response = $this->client->index($params);
print_r($response);
}
}
elasticsearch文档具有更新、删除、索引和搜索的所有设置,因此只需为每个资源方法实现这些设置
如果您想以laravel的方式完成并整洁地实现这一点,那么还有很大的改进空间。但这至少能让你走。一个更好的选择是为Elasticsearch客户端生成器创建一个服务提供者,并通过类型提示将其注入到您的UsersController中,但这将由您决定
祝你好运。非常感谢!我添加了一些代码,你能给我一个控制器的演示吗?目前无法尝试,但我今晚将尝试使用elasticsearch进行安装,以便我可以为你提供一些代码。
$client = Elasticsearch\ClientBuilder::create()->build();
$params = [
'index' => 'my_index',
'type' => 'my_type',
'id' => 'my_id',
'body' => ['testField' => 'abc']
];
$response = $client->index($params);
print_r($response);
class UsersController extends Controller
{
// the main elasticsearch instance created with the constructor
protected $client;
public function __construct() {
$hosts = [
// since I am using homestead I have to refer to the ip address of my host machine on which I have installed
// elasticcsearch, otherwise the default localhost option will point to homestead localhost
'192.168.178.10:9200'
];
$this->client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
}
public function index()
{
$params = [
'index' => 'node',
'type' => 'vacation',
'body' => [
'query' => [
'match_all' => []
]
]
];
$response = $this->client->search($params);
print_r($response);
}
public function create()
{
$params = [
'index' => 'node',
'type' => 'vacation',
'id' => '1029',
'body' => [
'query' => [
'match_all' => []
]
]
];
$response = $this->client->index($params);
print_r($response);
}
}