作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为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/
我是一名优秀的程序员,十分优秀!