dongtidai6519
2012-03-06 15:40 阅读 85
已采纳

如何从“Elastica ResultSet”对象获取结果

I am using the "elastica" php client for ElasticSearch.

I'm a bit new to OO-programming, especially in php.

However, I have managed to search my elasticsearch server using the elastica php client and store the response in an "Elastica_ResultSet" object. I have had no luck accessing the contents of that object whatsoever.

I would like to be able to list the total number of results, find an elasticsearch record id of a result and get the full content of an elasticsearch record for that result.

The Elastica class reference can be found here http://ruflin.github.com/Elastica/api/index.html , although I don't know what to do with it.

Here is the php code I have been using to get this far:

<?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');
?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    duanjianshen4871 duanjianshen4871 2012-05-25 02:22

    After a couple of months OO practise, it seemed performing a simple var_dump($resultSet) would have provided me with the structure and contents of the returned object... can't believe that nobody made any suggestions for such a basic question ;)

    点赞 评论 复制链接分享
  • douji2283 douji2283 2012-06-22 13:49

    So basicaly you can use var_export to output your resultset

    But in general the elastica search returns a Elastica_ResultSet object which has several attributes you can use like count, totalHits facets and so on.

    and also holds an array of Elastica_Result objects these can be accessed either by calling the Elastica_ResultSet getResults() method or by using the current() and next() methods or by simply using the php foreach function

    The Elastica_Result the data of the results and also has several methods you can use. getId(), getVersion(), getData() and so on.

    // Set Document Type
    $type = $index->getType('user');
    
    // Perform Search
    $resultSet = $index->search('halo');
    
    // Get IDs
    $resultIDs = array();
    foreach($resultSet as $result){
        $resultIDs[] = $result->getId();
    }
    

    I would like to let you know something that was a bit hard for me to get. The query and the sorting of results

    // 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);
    

    Hope this helps

    点赞 评论 复制链接分享

相关推荐