Won*_*der 4 php elasticsearch elastica
这是我第一次使用Elastica并查询ElasticSearch的数据
对我来说,作为首发,我有一个问题,如何使用Elastica查询下面的代码?:
curl 'http://localhost:9200/myindex/_search?pretty=true' -d '{
"query" : {
"term": {
"click": "true"
} }, "facets" : {
"matches" : {
"terms" : {
"field" : "pubid",
"all_terms" : true,
"size": 200
}
}
}
}'
Run Code Online (Sandbox Code Playgroud)
希望有人能借给我一臂之力.
谢谢,
这应该做:
// Create a "global" query
$query = new Elastica_Query;
// Create the term query
$term = new Elastica_Query_Term;
$term->setTerm('click', 'true');
// Add term query to "global" query
$query->setQuery($term);
// Create the facet
$facet = new Elastica_Facet_Terms('matches');
$facet->setField('pubid')
->setAllTerms(true)
->setSize(200);
// Add facet to "global" query
$query->addFacet($facet);
// Output query
echo json_encode($query->toArray());
Run Code Online (Sandbox Code Playgroud)
要运行查询,您需要连接到ES服务器
// Connect to your ES servers
$client = new Elastica_Client(array(
'servers' => array(
array('host' => 'localhost', 'port' => 9200),
array('host' => 'localhost', 'port' => 9201),
array('host' => 'localhost', 'port' => 9202),
array('host' => 'localhost', 'port' => 9203),
array('host' => 'localhost', 'port' => 9204),
),
));
Run Code Online (Sandbox Code Playgroud)
并指定要对其运行查询的索引和类型
// Get index
$index = $client->getIndex('myindex');
$type = $index->getType('typename');
Run Code Online (Sandbox Code Playgroud)
现在您可以运行查询
$type->search($query);
Run Code Online (Sandbox Code Playgroud)
编辑:如果您使用的是命名空间环境和当前版本的Elastica,请更改创建新对象的所有行
$query = new \Elastica\Query;
$facet = new \Elastica\Facet\Terms
Run Code Online (Sandbox Code Playgroud)
等等
| 归档时间: |
|
| 查看次数: |
9285 次 |
| 最近记录: |