gpt4 book ai didi

php - 如何从 “Elastica_ResultSet”对象获取结果

转载 作者:行者123 更新时间:2023-12-03 00:34:10 25 4
gpt4 key购买 nike

我正在为ElasticSearch使用“elastica” php客户端。

我对OO编程有点陌生,尤其是在php中。

但是,我设法使用elastica php客户端搜索我的elasticsearch服务器,并将响应存储在“Elastica_ResultSet”对象中。我没有运气访问该对象的任何内容。

我希望能够列出结果总数,找到结果的Elasticsearch记录ID,并获取该结果的Elasticsearch记录的完整内容。

Elastica类引用可以在http://ruflin.github.com/Elastica/api/index.html处找到,尽管我不知道该怎么做。

这是我一直在使用的php代码:

<?php
function __autoload_elastica ($class) {
$path = str_replace('_', '/', $class);

if (file_exists('extentions/' . $path . '.php')) {
require_once('extentions/' . $path . '.php');
//echo "$path EXISTS!!!";
}
}
spl_autoload_register('__autoload_elastica');

// New ES Client
$client = new Elastica_Client();

// Set Index
$index = $client->getIndex('test1');

// Set Document Type
$type = $index->getType('user');

// Perform Search
$resultSet = $index->search('halo');
?>

最佳答案

因此,基本上,您可以使用var_export输出结果集

但通常,elastica搜索会返回一个Elastica_ResultSet对象,该对象具有几个可以使用的属性,例如count,totalHits构面等。

并且还包含一个Elastica_Result对象数组,可以通过调用Elastica_ResultSet getResults()方法或使用current()和next()方法或仅使用php foreach函数来访问这些对象

Elastica_Result是结果数据,也可以使用几种方法。
getId(),getVersion(),getData()等。

// Set Document Type
$type = $index->getType('user');

// Perform Search
$resultSet = $index->search('halo');

// Get IDs
$resultIDs = array();
foreach($resultSet as $result){
$resultIDs[] = $result->getId();
}

我想让你知道一些我很难获得的东西。
查询和结果排序
// Set the query terms for your search
$queryTerm = new Elastica_Query_Terms();
$queryTerm->setTerms('user', array("test", "test1"));

// Create the sorting array
$sort = array("user" => array("order" => "desc"));

// Create the query
$query = Elastica_Query::create($queryTerm);

// Set the sorting to the query
$query->setSort($sort);

// Perform the search
$resultSet = $index->search($query);

希望这可以帮助

关于php - 如何从 “Elastica_ResultSet”对象获取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9586898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com