gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 00:45:45 25 4
gpt4 key购买 nike

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

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

谁能帮我解决这个问题?一些示例代码会很棒,因为我还不是 100% 了解所有 Elastica 对象的关联方式。

提前致谢。

最佳答案

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

您可以在搜索类上调用 count($query, $fullResult = false) 方法来运行查询作为 search_type 计数。默认情况下只返回总命中数。如果 $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>";

一个更有用的例子可能是返回带有某些方面的完整结果集并将其限制为特定索引和类型:

// 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 - 如何使用 Elastica 正确运行_count_查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19416578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com