Skip to content

Instantly share code, notes, and snippets.

@luismayta
Forked from basdenooijer/basicSelect.php
Created December 11, 2013 04:28
Show Gist options
  • Save luismayta/7905096 to your computer and use it in GitHub Desktop.
Save luismayta/7905096 to your computer and use it in GitHub Desktop.
<?php
// A query without any settings will use default values.
// This will result in a "*:*" query, 10 rows, all fields.
$query = new Solarium_Query_Select;
$result = $client->select($query);
echo 'Number of results found: ' . $result->getNumFound();
// The resultset is iterable, you could also use $result->getDocuments() to get an array with documents.
foreach ($result AS $document) {
print_r($document->getFields());
}
<?php
// create a query with a custom start, rows, fields and sort setting
$query = new Solarium_Query_Select;
$query->setStart(100)
->setRows(15)
->setFields(array('id','name','population','countrycode'))
->addSortField('name', Solarium_Query_Select::SORT_ASC)
->addSortField('population', Solarium_Query_Select::SORT_DESC);
// add a filterquery
$filterQuery = new Solarium_Query_Select_FilterQuery;
$filterQuery->setKey('fq1')
->addTag('populationLimit')
->setQuery('population:[1 TO 1000000]');
$query->addFilterQuery($filterQuery);
// add a facet field
$countryFacet = new Solarium_Query_Select_Facet_Field;
$countryFacet->setKey('countries')
->setField('countrycode');
$query->addFacet($countryFacet);
// add a facet query that excludes the filterquery
$bigCityFacet = new Solarium_Query_Select_Facet_Query;
$bigCityFacet->setKey('bigcities')
->addExclude('populationLimit')
->setQuery('population:[1000000 TO *]');
$query->addFacet($bigCityFacet);
$result = $client->select($query);
echo 'Number of results found: ' . $result->getNumFound() . '<br/>';
// facet query has a single count (value)
echo 'Facet query count: ' . $result->getFacet('bigcities')->getValue() . '<br/>';
// facet field can have multiple counts, it is iterable
foreach($result->getFacet('countries') AS $value => $count)
{
echo $value . ': ' . $count . '<br/>';
}
// and the resulting documents...
foreach ($result AS $document) {
print_r($document->getFields());
}
<?php
$query = new Solarium_Query_Select;
// add a facet field
$countryFacet = new Solarium_Query_Select_Facet_Field;
$countryFacet->setKey('countries')
->setField('countrycode');
$query->addFacet($countryFacet);
$result = $client->select($query);
// facet field is iterable
foreach($result->getFacet('countries') AS $value => $count)
{
echo $value . ': ' . $count . '<br/>';
}
<?php
$query = new Solarium_Query_Select;
// add a filterquery
$filterQuery = new Solarium_Query_Select_FilterQuery;
$filterQuery->setKey('fq1')
->addTag('populationLimit')
->setQuery('population:[1 TO 1000000]');
$query->addFilterQuery($filterQuery);
// add a facet query that excludes the filterquery
$bigCityFacet = new Solarium_Query_Select_Facet_Query;
$bigCityFacet->setKey('bigcities')
->addExclude('populationLimit')
->setQuery('population:[1000000 TO *]');
$query->addFacet($bigCityFacet);
$result = $client->select($query);
// facet query has a single count (value)
echo 'Facet query count: ' . $result->getFacet('bigcities')->getValue();
<?php
$query = new Solarium_Query_Select;
$countryFacet = new Solarium_Query_Select_Facet_Field;
$countryFacet->setKey('countries')
->setField('countrycode');
$query->addFacet($countryFacet);
$bigCityFacet = new Solarium_Query_Select_Facet_Query;
$bigCityFacet->setKey('bigcities')
->setQuery('population:[1000000 TO *]');
$query->addFacet($bigCityFacet);
$result = $client->select($query);
// get facet query result:
echo 'Facet query count: ' . $result->getFacet('bigcities')->getValue() . '<br/>';
// facet field can have multiple counts, it is iterable
foreach($result->getFacet('countries') AS $value => $count)
{
echo $value . ': ' . $count . '<br/>';
}
// or you can get an array
print_r($result->getFacet('countries')->getValues());
// facet field result counting:
echo 'Number of facet value counts: ' . count($result->getFacet('countries'));
<?php
// create a query with a custom start, rows, fields and sort setting
$query = new Solarium_Query_Select;
// you can add a filterquery based on a config array
$query->addFilterQuery(array(
'key' => 'fq1',
'tag' => array('populationLimit'),
'query' => 'population:[1 TO 1000000]',
));
// or create a filterquery instance yourself, using its API
$filterQuery = new Solarium_Query_Select_FilterQuery;
$filterQuery->setKey('fq1')
->addTag('populationLimit')
->setQuery('population:[1 TO 1000000]');
$query->addFilterQuery($filterQuery);
<?php
// first create a base query as a query class
class CityQuery extends Solarium_Query_Select
{
protected function _init()
{
$this->setRows(15)
->setFields(array('id','name','population','countrycode'))
->addSortField('name', Solarium_Query_Select::SORT_ASC)
->addSortField('population', Solarium_Query_Select::SORT_DESC);
}
}
// the query instance easily be altered based on user input
$query = new CityQuery;
if(isset($userInput['start'])){
$query->setStart($userInput['start']);
}
// alternatively you can use class inheritance to create query inheritance
class BigCityQuery extends CityQuery{
protected function _init()
{
parent::_init();
$filterQuery->setKey('fq1')
->addTag('populationFilter')
->setQuery('population:[1000000 TO *]');
$this->addFilterQuery($filterQuery);
}
}
<?php
$query = new Solarium_Query_Select;
$result = $client->select($query);
// get total number of query results
echo 'Number of results found: ' . $result->getNumFound();
// count number of fetched results
echo 'Number of results fetched: ' . count($result);
// the resultset is iterable
foreach ($result AS $document) {
print_r($document->getFields());
}
// but you can also get the documentset as an array
$documents = $result->getDocuments();
foreach ($documents AS $document) {
print_r($document->getFields());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment