我一直在使用 ElasticSearch 和 Elastica ( http://elastica.io/ ) 组合我们的搜索实现。
目前我无法弄清楚如何执行计数搜索,正如 ElasticSearch 的计数 API 中所说的那样。
谁能帮我解决这个问题?一些示例代码会很棒,因为我还不是 100% 了解所有 Elastica 对象之间的关系。
提前致谢。
我一直在使用 ElasticSearch 和 Elastica ( http://elastica.io/ ) 组合我们的搜索实现。
目前我无法弄清楚如何执行计数搜索,正如 ElasticSearch 的计数 API 中所说的那样。
谁能帮我解决这个问题?一些示例代码会很棒,因为我还不是 100% 了解所有 Elastica 对象之间的关系。
提前致谢。
您可以使用 Search 类 ( http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-search-type.html#count ) 与elastica ://elastica.io/api/classes/Elastica.Search.html)。
您可以在搜索类上调用 count($query, $fullResult = false) 方法以 search_type count 的形式运行查询。默认情况下,仅返回总命中数。如果 $fullResult 设置为 true,则返回包含构面的完整 ResultSet。这比运行大小设置为零的标准搜索更有效。
例如,要获取查询的命中数,您可以执行以下操作:
// 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>";
一个更有用的示例可能是返回带有一些方面的完整 ResultSet 并将其限制为某个索引和类型:
// 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>";
使用 Elastica 2.2,您可以这样做:
public function count($index)
{
$query = new Elastica\Query;
$query->setSize(0);
$results = $index->search($query);
$count = $results->getTotalHits();
return $count;
}