Sets post_filter argument for the query. The filter is applied after the query has executed.
public setPostFilter ( Elastica\Query\AbstractQuery $filter ) | ||
$filter | Elastica\Query\AbstractQuery |
/** * @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); } } }
/** * @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()); }
/** * @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()); }
/** * @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(); }
/** * @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()); }
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; }
/** * @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); }
/** * @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]); }
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(); }
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); } } }
/** * @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); }
/** * @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(); }
/** * 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); }
/** * @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); }
/** * {@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; }
/** * @group functional */ public function testMoreLikeThisApi() { $client = $this->_getClient(array('persistent' => false)); $index = $client->getIndex('elastica_test'); $index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true); $type = new Type($index, 'mlt_test'); $type->addDocuments(array(new Document(1, array('visible' => true, 'name' => 'bruce wayne batman')), new Document(2, array('visible' => true, 'name' => 'bruce wayne')), new Document(3, array('visible' => false, 'name' => 'bruce wayne')), new Document(4, array('visible' => true, 'name' => 'batman')), new Document(5, array('visible' => false, 'name' => 'batman')), new Document(6, array('visible' => true, 'name' => 'superman')), new Document(7, array('visible' => true, 'name' => 'spiderman')))); $index->refresh(); $document = $type->getDocument(1); // Return all similar $resultSet = $type->moreLikeThis($document, array('min_term_freq' => '1', 'min_doc_freq' => '1')); $this->assertEquals(4, $resultSet->count()); // Return just the visible similar $query = new Query(); $filterTerm = new Term(); $filterTerm->setTerm('visible', true); $query->setPostFilter($filterTerm); $resultSet = $type->moreLikeThis($document, array('min_term_freq' => '1', 'min_doc_freq' => '1'), $query); $this->assertEquals(2, $resultSet->count()); }
/** * @param string $term * @param string $value * @param int $size * * @return \Elastica\ResultSet */ public function termQuery($term, $value, $size = 30) { $_agg = new Aggregation(); $_facet = $_agg->date_histogram('occurred_on', '@timestamp', ElkIntervals::DAY); // $_facet = new DateHistogram('occurred_on'); // $_facet->setField('@timestamp'); // $_facet->setInterval('day'); $_query = new Query(); $_query->setSize($size); $_query->setSort(['@timestamp']); // Filter for term $_filter = new Prefix($term, $value); $_and = new Bool(); $_and->addMust($_filter); $_query->setPostFilter($_and); $_query->addAggregation($_facet); $_results = $this->_doSearch($_query); return $_results; }
/** * @group unit */ public function testLegacySetPostFilterToArrayCast() { $this->hideDeprecated(); $query = new Query(); $postFilter = new \Elastica\Filter\Terms(); $postFilter->setTerms('key', array('term')); $query->setPostFilter($postFilter); $postFilter->setTerms('another key', array('another term')); $anotherQuery = new Query(); $anotherQuery->setPostFilter($postFilter); $this->showDeprecated(); $this->assertEquals($query->toArray(), $anotherQuery->toArray()); }
/** * @param $searchClosed * @return \Elastica\ResultSet * @throws Exception */ public function doSearch($searchClosed) { $this->connection = new ElasticSearchConnection(); $this->connection->init(); $this->whereClause = new Query\QueryString(); $this->whereClause->setQuery($this->searchTerms); $this->utility = new Util(); if (isset($_GET['page'])) { $this->currentPage = intval($_GET['page']); } $this->fieldMapping = $this->configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, 'WMDB.Forger.SearchTermMapping'); $elasticaQuery = new Query(); if ($searchClosed === 'true') { $elasticaQuery->setQuery($this->whereClause); } else { $boolSearch = new Query\Bool(); $boolSearch->addMust($this->whereClause); $boolSearch->addMustNot(['term' => ['status.name' => 'Closed']]); $boolSearch->addMustNot(['term' => ['status.name' => 'Rejected']]); $boolSearch->addMustNot(['term' => ['status.name' => 'Resolved']]); $elasticaQuery->setQuery($boolSearch); } $elasticaQuery->setSize($this->perPage); $elasticaQuery->setFrom($this->currentPage * $this->perPage - $this->perPage); $usedFilters = $this->addFilters(); if ($usedFilters !== false) { $elasticaQuery->setPostFilter($usedFilters); } $this->addAggregations($elasticaQuery); $elasticaResultSet = $this->connection->getIndex()->search($elasticaQuery); $results = $elasticaResultSet->getResults(); $maxScore = $elasticaResultSet->getMaxScore(); $aggs = $elasticaResultSet->getAggregations(); $this->totalHits = $elasticaResultSet->getTotalHits(); $out = array('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), 'endingAtItem' => $this->currentPage * $this->perPage, 'results' => $results, 'maxScore' => $maxScore, 'aggs' => $aggs); if (intval($this->totalHits) <= intval($out['endingAtItem'])) { $out['endingAtItem'] = intval($this->totalHits); } return $out; }