本文整理汇总了PHP中Elastica\Query::setPostFilter方法的典型用法代码示例。如果您正苦于以下问题:PHP Query::setPostFilter方法的具体用法?PHP Query::setPostFilter怎么用?PHP Query::setPostFilter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Elastica\Query
的用法示例。
在下文中一共展示了Query::setPostFilter方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testIndicesQuery
/**
* @group functional
*/
public function testIndicesQuery()
{
$docs = array(new Document(1, array('color' => 'blue')), new Document(2, array('color' => 'green')), new Document(3, array('color' => 'blue')), new Document(4, array('color' => 'yellow')));
$index1 = $this->_createIndex();
$index1->addAlias('indices_query');
$index1->getType('test')->addDocuments($docs);
$index1->refresh();
$index2 = $this->_createIndex();
$index2->addAlias('indices_query');
$index2->getType('test')->addDocuments($docs);
$index2->refresh();
$boolQuery = new Query\BoolQuery();
$boolQuery->addMustNot(new Term(array('color' => 'blue')));
$indicesQuery = new Indices($boolQuery, array($index1->getName()));
$boolQuery = new Query\BoolQuery();
$boolQuery->addMustNot(new Term(array('color' => 'yellow')));
$indicesQuery->setNoMatchQuery($boolQuery);
$query = new Query();
$query->setPostFilter($indicesQuery);
// search over the alias
$index = $this->_getClient()->getIndex('indices_query');
$results = $index->search($query);
// ensure that the proper docs have been filtered out for each index
$this->assertEquals(5, $results->count());
foreach ($results->getResults() as $result) {
$data = $result->getData();
$color = $data['color'];
if ($result->getIndex() === $index1->getName()) {
$this->assertNotEquals('blue', $color);
} else {
$this->assertNotEquals('yellow', $color);
}
}
}
示例2: testGeoPoint
/**
* @group functional
*/
public function testGeoPoint()
{
$index = $this->_createIndex();
$type = $index->getType('test');
// Set mapping
$type->setMapping(array('location' => array('type' => 'geo_point')));
// Add doc 1
$doc1 = new Document(1, array('name' => 'ruflin'));
$doc1->addGeoPoint('location', 17, 19);
$type->addDocument($doc1);
// Add doc 2
$doc2 = new Document(2, array('name' => 'ruflin'));
$doc2->addGeoPoint('location', 30, 40);
$type->addDocument($doc2);
$index->refresh();
// Only one point should be in polygon
$query = new Query();
$points = array(array(16, 16), array(16, 20), array(20, 20), array(20, 16), array(16, 16));
$geoFilter = new GeoPolygon('location', $points);
$query = new Query(new MatchAll());
$query->setPostFilter($geoFilter);
$this->assertEquals(1, $type->search($query)->count());
// Both points should be inside
$query = new Query();
$points = array(array(16, 16), array(16, 40), array(40, 40), array(40, 16), array(16, 16));
$geoFilter = new GeoPolygon('location', $points);
$query = new Query(new MatchAll());
$query->setPostFilter($geoFilter);
$this->assertEquals(2, $type->search($query)->count());
}
示例3: testGeoPoint
/**
* @group functional
*/
public function testGeoPoint()
{
$index = $this->_createIndex();
$type = $index->getType('test');
// Set mapping
$type->setMapping(array('point' => array('type' => 'geo_point')));
// Add doc 1
$doc1 = new Document(1, array('name' => 'ruflin'));
$doc1->addGeoPoint('point', 17, 19);
$type->addDocument($doc1);
// Add doc 2
$doc2 = new Document(2, array('name' => 'ruflin'));
$doc2->addGeoPoint('point', 30, 40);
$type->addDocument($doc2);
$index->optimize();
$index->refresh();
// Only one point should be in radius
$geoQuery = new GeoDistance('point', array('lat' => 30, 'lon' => 40), '1km');
$query = new Query(new MatchAll());
$query->setPostFilter($geoQuery);
$this->assertEquals(1, $type->search($query)->count());
// Both points should be inside
$query = new Query();
$geoQuery = new GeoDistance('point', array('lat' => 30, 'lon' => 40), '40000km');
$query = new Query(new MatchAll());
$query->setPostFilter($geoQuery);
$index->refresh();
$this->assertEquals(2, $type->search($query)->count());
}
示例4: testLookup
/**
* @group functional
*/
public function testLookup()
{
$index = $this->_createIndex();
$type1 = $index->getType('musicians');
$type2 = $index->getType('bands');
//index some test data
$type1->addDocuments(array(new Document(1, array('name' => 'robert', 'lastName' => 'plant')), new Document(2, array('name' => 'jimmy', 'lastName' => 'page')), new Document(3, array('name' => 'john paul', 'lastName' => 'jones')), new Document(4, array('name' => 'john', 'lastName' => 'bonham')), new Document(5, array('name' => 'jimi', 'lastName' => 'hendrix'))));
$type2->addDocument(new Document('led zeppelin', array('members' => array('plant', 'page', 'jones', 'bonham'))));
$index->refresh();
//use the terms lookup feature to query for some data
$termsFilter = new Terms();
$termsFilter->setLookup('lastName', $type2, 'led zeppelin', 'members', null);
$query = new Query();
$query->setPostFilter($termsFilter);
$results = $index->search($query);
$this->assertEquals($results->count(), 4, 'Terms lookup with null index');
$termsFilter->setLookup('lastName', $type2, 'led zeppelin', 'members', $index);
$query->setPostFilter($termsFilter);
$results = $index->search($query);
$this->assertEquals($results->count(), 4, 'Terms lookup with index as object');
//Query with index given as string
$termsFilter->setLookup('lastName', $type2, 'led zeppelin', 'members', $index->getName());
$query->setPostFilter($termsFilter);
$results = $index->search($query);
$this->assertEquals($results->count(), 4, 'Terms lookup with index as string');
//Query with array of options
$termsFilter->setLookup('lastName', $type2, 'led zeppelin', 'members', array('index' => $index));
$query->setPostFilter($termsFilter);
$results = $index->search($query);
$this->assertEquals($results->count(), 4, 'Terms lookup with options array');
$index->delete();
}
示例5: testQuery
/**
* @group functional
*/
public function testQuery()
{
$query = new Query();
$match = new Match();
$match->setField('make', 'ford');
$query->setQuery($match);
$filter = new Term();
$filter->setTerm('color', 'green');
$query->setPostFilter($filter);
$results = $this->_getTestIndex()->search($query);
$this->assertEquals(1, $results->getTotalHits());
}
示例6: __construct
public function __construct(Index $index, Query $query, LoggerInterface $logger = null)
{
//Optimise the query by just getting back the ids and types
$query->setFields(array('_id', '_type'));
//If we are in live reading mode, only return published documents
if (\Versioned::get_reading_mode() == \Versioned::DEFAULT_MODE) {
$publishedFilter = new Query\BoolQuery();
$publishedFilter->addMust(new Query\Term([Searchable::$published_field => 'true']));
$query->setPostFilter($publishedFilter);
}
$this->index = $index;
$this->query = $query;
$this->logger = $logger;
}
示例7: testRandomRead
/**
* @depends testAddDocument
* @dataProvider providerTransport
*/
public function testRandomRead(array $config, $transport)
{
$type = $this->getType($config);
$type->search('test');
$times = array();
for ($i = 0; $i < $this->_max; $i++) {
$test = rand(1, $this->_max);
$query = new Query();
$query->setQuery(new MatchAllQuery());
$query->setPostFilter(new TermFilter(array('test' => $test)));
$result = $type->search($query);
$times[] = $result->getResponse()->getQueryTime();
}
self::logResults('random read', $transport, $times);
}
示例8: search
/**
* @param array $fullRequest
*/
protected function search(array $fullRequest)
{
$query = new El\Query($fullRequest);
$usedFilters = $this->addFilters();
if ($usedFilters !== false) {
$query->setPostFilter($usedFilters);
}
$resultSet = $this->connection->getIndex()->search($query);
$this->totalHits = $resultSet->getTotalHits();
if (intval($this->totalHits) <= intval($this->currentPage * $this->perPage)) {
$this->view->assign('endingAtItem', intval($this->totalHits));
} else {
$this->view->assign('endingAtItem', $this->currentPage * $this->perPage);
}
$this->view->assignMultiple(['results' => $resultSet->getResults(), 'pagesToLinkTo' => $this->getPages(), 'currentPage' => $this->currentPage, 'prev' => $this->currentPage - 1, 'next' => $this->currentPage < ceil($this->totalHits / $this->perPage) ? $this->currentPage + 1 : 0, 'totalResults' => $this->totalHits, 'startingAtItem' => $this->currentPage * $this->perPage - ($this->perPage - 1), 'aggregations' => $resultSet->getAggregations(), 'context' => $this->context]);
}
示例9: testBoolFilter
public function testBoolFilter()
{
$index = $this->_createIndex('bool_filter_test');
$type = $index->getType('book');
//index some test data
$type->addDocument(new \Elastica\Document(1, array('author' => 'Michael Shermer', 'title' => 'The Believing Brain', 'publisher' => 'Robinson')));
$type->addDocument(new \Elastica\Document(2, array('author' => 'Jared Diamond', 'title' => 'Guns, Germs and Steel', 'publisher' => 'Vintage')));
$type->addDocument(new \Elastica\Document(3, array('author' => 'Jared Diamond', 'title' => 'Collapse', 'publisher' => 'Penguin')));
$type->addDocument(new \Elastica\Document(4, array('author' => 'Richard Dawkins', 'title' => 'The Selfish Gene', 'publisher' => 'OUP Oxford')));
$type->addDocument(new \Elastica\Document(5, array('author' => 'Anthony Burges', 'title' => 'A Clockwork Orange', 'publisher' => 'Penguin')));
$index->refresh();
//use the terms lookup feature to query for some data
//build query
//must
// should
// author = jared
// author = richard
// must_not
// publisher = penguin
//construct the query
$query = new Query();
$mainBoolFilter = new Bool();
$shouldFilter = new Bool();
$authorFilter1 = new Term();
$authorFilter1->setTerm('author', 'jared');
$authorFilter2 = new Term();
$authorFilter2->setTerm('author', 'richard');
$shouldFilter->addShould(array($authorFilter1, $authorFilter2));
$mustNotFilter = new Bool();
$publisherFilter = new Term();
$publisherFilter->setTerm('publisher', 'penguin');
$mustNotFilter->addMustNot($publisherFilter);
$mainBoolFilter->addMust(array($shouldFilter, $mustNotFilter));
$query->setPostFilter($mainBoolFilter);
//execute the query
$results = $index->search($query);
//check the number of results
$this->assertEquals($results->count(), 2, 'Bool filter with child Bool filters: number of results check');
//count compare the id's
$ids = array();
/** @var \Elastica\Result $result **/
foreach ($results as $result) {
$ids[] = $result->getId();
}
$this->assertEquals($ids, array("2", "4"), 'Bool filter with child Bool filters: result ID check');
$index->delete();
}
示例10: testIndicesFilter
public function testIndicesFilter()
{
$filter = new Indices(new BoolNot(new Term(array("color" => "blue"))), array($this->_index1->getName()));
$filter->setNoMatchFilter(new BoolNot(new Term(array("color" => "yellow"))));
$query = new Query();
$query->setPostFilter($filter);
// search over the alias
$index = $this->_getClient()->getIndex("indices_filter");
$results = $index->search($query);
// ensure that the proper docs have been filtered out for each index
$this->assertEquals(5, $results->count());
foreach ($results->getResults() as $result) {
$data = $result->getData();
$color = $data["color"];
if ($result->getIndex() == $this->_index1->getName()) {
$this->assertNotEquals("blue", $color);
} else {
$this->assertNotEquals("yellow", $color);
}
}
}
示例11: findPaginatedByQueryAndCategory
/**
* @inheritdoc
*/
public function findPaginatedByQueryAndCategory(\string $queryString = null, \string $category = null, \int $priceFrom = null, \int $priceTo = null, \bool $availability = null)
{
$queryObject = new Query();
$filter = new BoolAnd();
if ($queryString) {
$query = new Match('_all', ['query' => $queryString, 'operator' => 'AND']);
} else {
$query = new Query\MatchAll();
}
if ($availability !== null) {
$filter->addFilter(new Term(['availability' => $availability]));
}
$range = [];
if ($priceFrom) {
$range['gte'] = $priceFrom;
}
if ($priceTo) {
$range['lte'] = $priceTo;
}
if ($range) {
$filter->addFilter(new Range('price', $range));
}
if ($category) {
$term = new Term(['category.id' => $category]);
$queryObject->setPostFilter($term);
}
$terms = new Terms('categories');
$terms->setField('category.id');
$terms->setSize(0);
$queryObject->addAggregation($terms);
if ($filter->getFilters()) {
$filtered = new Query\Filtered($query, $filter);
$queryObject->setQuery($filtered);
} else {
$queryObject->setQuery($query);
}
$queryObject->setSort(['updated' => 'desc']);
$queryObject->setMinScore(0.5);
return $this->findPaginated($queryObject);
}
示例12: testFilter
/**
* @group functional
*/
public function testFilter()
{
$index = $this->_createIndex();
$type = $index->getType('test');
$mapping = new Mapping($type, array('pin' => array('type' => 'geo_point', 'geohash' => true, 'geohash_prefix' => true)));
$type->setMapping($mapping);
$type->addDocument(new Document(1, array('pin' => '9q8yyzm0zpw8')));
$type->addDocument(new Document(2, array('pin' => '9mudgb0yued0')));
$index->refresh();
$filter = new GeohashCell('pin', array('lat' => 32.828326, 'lon' => -117.255854));
$query = new Query();
$query->setPostFilter($filter);
$results = $type->search($query);
$this->assertEquals(1, $results->count());
//test precision parameter
$filter = new GeohashCell('pin', '9', 1);
$query = new Query();
$query->setPostFilter($filter);
$results = $type->search($query);
$this->assertEquals(2, $results->count());
$index->delete();
}
示例13: search
/**
* Search for logs
*
* @param array $params Search parameters:
* - project: Project to find logs for
* - query: Elasticsearch simple query string
* - items: Number of results to return per page
* - page: Page of results to return (0-index)
* @return ResultSet
*/
public function search(array $params = [])
{
$params = array_merge(['project' => 'production', 'query' => null, 'items' => 20, 'page' => 0, 'date' => null], $params);
$filters = new BoolQuery();
if ($params['query'] !== null) {
$filters->addMust(new SimpleQueryString($params['query'], ['message', 'nick']));
}
if ($params['date'] !== null) {
$filters->addMust(new Range('@timestamp', ['lte' => "{$params['date']}||/d"]));
}
$query = new Query($filters);
$query->setPostFilter(new Term(['project' => $params['project']]));
$query->setFrom($params['page'] * $params['items'])->setSize($params['items'])->setFields(self::$DEFAULT_FIELDS)->setSort(['@timestamp' => ['order' => 'desc']]);
return $this->doSearch($query);
}
示例14: compileElasticaTaxonQuery
/**
* @param null $facets
* @param $configuration
* @param $taxon
* @param null $types
*
* @return mixed
*/
public function compileElasticaTaxonQuery($facets = null, $configuration, $taxon, $types = null)
{
$elasticaQuery = new Query();
$boolFilter = new BoolFilter();
$this->addAvailableProductsFilters($boolFilter);
if (!empty($types)) {
foreach ($types as $type) {
$typeFilter = new Type($type);
$boolFilter->addMust($typeFilter);
}
$elasticaQuery->setPostFilter($boolFilter);
}
if ($channel = $this->channelContext->getChannel()) {
$channelFilter = new FilterTerms();
$channelFilter->setTerms('channels', [(string) $channel]);
$boolFilter->addMust($channelFilter);
$elasticaQuery->setPostFilter($boolFilter);
}
$query = new Filtered();
$taxonFromRequestFilter = new FilterTerms();
$taxonFromRequestFilter->setTerms('taxons', [$taxon]);
$boolFilter->addMust($taxonFromRequestFilter);
$query->setFilter($boolFilter);
$elasticaQuery->setQuery($query);
return $this->compileElasticsearchQuery($elasticaQuery, $facets, $configuration);
}
示例15: get
/**
* {@inheritdoc}
*/
public function get(array $data, array $keys, $namespace)
{
if (!$this->hasNamespace($namespace)) {
return array();
}
// Build the query
$query = new Query();
// Add hash term
$term = new Term();
$term->setTerm('hash', $this->getHash($data, $keys));
$query->setPostFilter($term);
$esResults = $this->index->getType($namespace)->search($query)->getResults();
$results = array();
foreach ($esResults as $esResult) {
$results[] = $esResult->getData();
}
return $results;
}