【问题标题】:php code to run elasticsearch query运行elasticsearch查询的php代码
【发布时间】:2013-07-11 07:50:53
【问题描述】:

我是弹性搜索的新手。我想使用 elasticsearch 搜索 mysql dB。我想要并得到结果。我什至为此安装了 elastica。但我没有从这段代码中得到任何结果:

<?php
 require_once '/home/babloo/vendor/autoload.php';

$client = new Elastica_Client();
$index = $client->getIndex('jdbc');
$index->getType('jdbc');

$query_string = new Elastica_Query_QueryString('ashish');
$query_string->setFields(array('name'));    
$query = new Elastica_Query($query_string);

$index->refresh();
$searchResults = $index->search($query);
?>

我哪里出错了?

【问题讨论】:

  • 我没有看到您连接到数据库,这是在舞台外的某个地方完成的吗?
  • @DevZer0: 不...怎么办?
  • 我不了解自己,我检查了文档,它非常生锈。我现在正在下载 elastica,如果我有机会检查一下,我会尽快回复您。
  • 您不能使用 elasticsearch 搜索 mySQL 数据库。把它想象成另一个数据库,但优化了搜索。您需要先将数据从 mySQL 获取到 elasticsearch,然后将查询发送到 elasticsearch 服务器。搜索导入数据的方法,我建议使用命令行 cURL 语句开始。

标签: php elasticsearch elastica


【解决方案1】:

改进语法并按照以下步骤操作: 1) 将错误报告设置为 ON

   error_reporting(E_ALL);
   ini_set('display_errors', TRUE);
   ini_set('display_startup_errors', TRUE);
   session_start();

2) 需要'vendor/autoload.php'; (使用作曲家安装)

3) 创建对象

   $client = new Elasticsearch\Client();
   $elasticaClient = new Elastica\Client();
   $reque=new Elastica\Request($elasticaClient);

4) 程序示例

$index = $elasticaClient->getIndex('test');
$index->create(array(), true);
$type = $index->getType('test');
$type->addDocument(new Elastica\Document(1, array('username' => 'ruflin')));
$index->refresh();

$query = array(
    'query' => array(
        'query_string' => array(
            'query' => 'ruflin',
        )
    )
);
 //$typee =$reque->GET;
$path = $index->getName() . '/' . $type->getName() . '/_search';

$response = $elasticaClient->request($path, $reque::GET, $query);
$responseArray = $response->getData();

echo "<pre>";
print_r($responseArray);

参考链接

作曲家

b)https://getcomposer.org/doc/01-basic-usage.md H)https://www.digitalocean.com/community/articles/how-to-install-and-use-composer-on-your-vps-running-ubuntu

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 2023-02-13
    • 2012-09-13
    相关资源
    最近更新 更多