I've been putting together our search implementation using ElasticSearch and Elastica (http://elastica.io/).

Currently I am having trouble figuring out how to execute a count search, as is spoken about in ElasticSearch's Count API.

Can anyone clear this up for me? Some example code would be great, as I'm not 100% on how all the Elastica objects relate, yet.

Thanks in advance.

有帮助吗?

解决方案

You can perform an elasticsearch search_type count query (http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-search-type.html#count) with elastica by using the Search class (http://elastica.io/api/classes/Elastica.Search.html).

You can call the count($query, $fullResult = false) method on the search class to run a query as search_type count. By default only the total hit count is returned. If $fullResult is set to true, the full ResultSet including facets is returned. This is more efficient than running a standard search with size set to zero.

For example to just get the hit count for a query you could do:

// Define a Query. E.g. a string query.
$elasticaQueryString = new \Elastica\Query\QueryString();

//'And' or 'Or' default : 'Or'
$elasticaQueryString->setDefaultOperator('AND');
$elasticaQueryString->setQuery('london');

// Create the actual search query object with some data.
$elasticaQuery = new \Elastica\Query();
$elasticaQuery->setQuery($elasticaQueryString);

// Setup elastica client connection to your easticsearch server (with default host and port)
$elasticaClient = new \Elastica\Client();

// Create an elastica search object
$elasticaSearch = new \Elastica\Search($elasticaClient);

// Call count method on search object to run a
$count = $elasticaSearch->count($elasticaQuery);

// output count
echo "<p>$count<p>";

A more useful example might be to return the full ResultSet with some facets and restrict it to a certain index and type:

// Define a Query. e.g. a string query.
$elasticaQueryString = new \Elastica\Query\QueryString();

//'And' or 'Or' default : 'Or'
$elasticaQueryString->setDefaultOperator('AND');
$elasticaQueryString->setQuery('london');

// Create some facets to be generated by query
$elasticaFacet1 = new \Elastica\Facet\Terms('myFacet1');
$elasticaFacet1->setField('colour');
$elasticaFacet1->setSize(10);

$elasticaFacet2 = new \Elastica\Facet\Terms('myFacet2');
$elasticaFacet2->setField('shape');
$elasticaFacet2->setSize(10);

// Create the actual search object with some data.
$elasticaQuery = new \Elastica\Query();

// add query string to query
$elasticaQuery->setQuery($elasticaQueryString);

//add facets to query
$elasticaQuery->addFacet($elasticaFacet1);
$elasticaQuery->addFacet($elasticaFacet2);

// Setup elastica client connection to your elasticsearch server
$elasticaClient = new \Elastica\Client(array(
    'host' => 'localhost',
    'port' => 9200
));

// If you want to restrict your search to a particular index then get that
$elasticaIndex = $elasticaClient->getIndex('myIndex');

// If you want to restrict your search to a particular type then get that
$elasticaType = $elasticaIndex->getType('myType');

// Create and elastica Search object
$elasticaSearch = new \Elastica\Search($elasticaClient);

// addIndex and addType are optional and will restrict the search to those respectivly
$countResults = $elasticaSearch->addIndex($elasticaIndex)->addType($elasticaType)->count($elasticaQuery, true);

//output total hits
echo "<p>{$countResults->getTotalHits()}</p>";

//output list of facets
echo "<ul>";
foreach ($countResults->getFacets() as $facetField => $resultTerms) {
    echo "<li><span>$facetField</span><ul>";
    foreach ($resultTerms['terms'] as $resultTerm) {
        echo "<li>{$resultTerm['term']}: {$resultTerm['count']}</li>";
    }
    echo "</ul></li>";
}
echo "</ul>";

其他提示

With Elastica 2.2, you can do this:

public function count($index)
{
    $query = new Elastica\Query;
    $query->setSize(0);
    $results = $index->search($query);
    $count = $results->getTotalHits();

    return $count;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top