编程语言
首页 > 编程语言> > php – 如何使用Elastica正确运行_count_查询?

php – 如何使用Elastica正确运行_count_查询?

作者:互联网

我一直在使用ElasticSearch和Elastica(http://elastica.io/)整理我们的搜索实现.

目前我无法弄清楚如何执行计数搜索,正如ElasticSearch的Count API中所说的那样.

任何人都可以为我清除这个吗?一些示例代码会很棒,因为我不是100%关于所有Elastica对象的关联.

提前致谢.

解决方法:

您可以使用Search类(http://elastica.io/api/classes/Elastica.Search.html)对elastica执行elasticsearch search_type计数查询(http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-search-type.html#count).

您可以在搜索类上调用count($query,$fullResult = false)方法,以将查询作为search_type count运行.默认情况下,仅返回总命中数.如果$fullResult设置为true,则返回包含facets的完整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>";

标签:php,elasticsearch,search,elastica
来源: https://codeday.me/bug/20190831/1775691.html