Add must not part to query.
public addMustNot ( Elastica\Query\AbstractQuery | array $args ) | ||
$args | Elastica\Query\AbstractQuery | array | Must not query |
/** * @group unit */ public function testToArray() { $query = new BoolQuery(); $idsQuery1 = new Ids(); $idsQuery1->setIds(1); $idsQuery2 = new Ids(); $idsQuery2->setIds(2); $idsQuery3 = new Ids(); $idsQuery3->setIds(3); $filter1 = new TermFilter(); $filter1->setTerm('test', '1'); $filter2 = new TermFilter(); $filter2->setTerm('username', 'ruth'); $boost = 1.2; $minMatch = 2; $query->setBoost($boost); $query->setMinimumNumberShouldMatch($minMatch); $query->addMust($idsQuery1); $query->addMustNot($idsQuery2); $query->addShould($idsQuery3->toArray()); $query->addFilter($filter1); $query->addFilter($filter2); $expectedArray = array('bool' => array('must' => array($idsQuery1->toArray()), 'should' => array($idsQuery3->toArray()), 'filter' => array($filter1->toArray(), $filter2->toArray()), 'minimum_number_should_match' => $minMatch, 'must_not' => array($idsQuery2->toArray()), 'boost' => $boost)); $this->assertEquals($expectedArray, $query->toArray()); }
/** * @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); } } }
/** * Set the type of match for the query and then add it to the bool container. * * @param QueryContract $query * @param Bool $container * @return Bool */ protected function addQueryToContainer(QueryContract $query, BoolQuery $container) { switch ($query->getType()) { case "must": $container->addMust($query->getQuery()); break; case "should": $container->addShould($query->getQuery()); break; case "must_not": $container->addMustNot($query->getQuery()); break; } return $container; }
/** * search page index controller * * @param Request $request * @param int $page * @return \Symfony\Component\HttpFoundation\Response */ public function indexAction(Request $request, $page = 1) { $searchManager = $this->get('ojs_search_manager'); $journalId = null; $getRoles = $request->query->get('role_filters'); $getSubjects = $request->query->get('subject_filters'); $getJournals = $request->query->get('journal_filters'); $getLocales = $request->query->get('locale_filters'); $getPublishers = $request->query->get('publisher_filters'); $getIndexes = $request->query->get('index_filters'); $roleFilters = !empty($getRoles) ? explode(',', $getRoles) : []; $subjectFilters = !empty($getSubjects) ? explode(',', $getSubjects) : []; $journalFilters = !empty($getJournals) ? explode(',', $getJournals) : []; $localeFilters = !empty($getLocales) ? explode(',', $getLocales) : []; $publisherFilters = !empty($getPublishers) ? explode(',', $getPublishers) : []; $indexFilters = !empty($getIndexes) ? explode(',', $getIndexes) : []; $queryType = $request->query->has('type') ? $request->get('type') : 'basic'; $query = filter_var($request->get('q'), FILTER_SANITIZE_STRING); $section = filter_var($request->get('section'), FILTER_SANITIZE_STRING); $searcher = $this->get('fos_elastica.index.search'); $searchQuery = new Query('_all'); $boolQuery = new Query\BoolQuery(); $match = new Query\Match(); $match->setField('published', false); $boolQuery->addMustNot($match); //set query according to query type if ($queryType == 'basic') { $fieldQuery = new Query\Prefix(); $fieldQuery->setPrefix('_all', $query); $boolQuery->addMust($fieldQuery); } elseif ($queryType == 'advanced') { $parseQuery = $searchManager->parseSearchQuery($query); foreach ($parseQuery as $searchTerm) { $condition = $searchTerm['condition']; $advancedFieldQuery = new Query\MultiMatch(); $advancedFieldQuery->setFields([$searchTerm['searchField']]); $advancedFieldQuery->setQuery($searchTerm['searchText']); if ($condition == 'AND') { $boolQuery->addMust($advancedFieldQuery); } elseif ($condition == 'OR') { $boolQuery->addShould($advancedFieldQuery); } elseif ($condition == 'NOT') { $boolQuery->addMustNot($advancedFieldQuery); } } } elseif ($queryType == 'tag') { $regexpQuery = new Query\Regexp(); $regexpQuery->setParams(['tags' => ".*" . $query . ".*"]); $boolQuery->addMust($regexpQuery); } elseif ($queryType == 'injournal') { $journalId = $request->get('journalId'); $boolQuery->setParams($searchManager->getSearchInJournalQuery($journalId, $query)); } //set aggregations if requested if (!empty($roleFilters) || !empty($subjectFilters) || !empty($journalFilters) || !empty($localeFilters) || !empty($publisherFilters)) { foreach ($roleFilters as $role) { $match = new Query\Match(); $match->setField('user.userJournalRoles.role.name', $role); $boolQuery->addMust($match); } foreach ($subjectFilters as $subject) { $match = new Query\Match(); $match->setField('subjects', $subject); $boolQuery->addMust($match); } foreach ($journalFilters as $journal) { $match = new Query\Match(); $match->setField('user.userJournalRoles.journal.title', $journal); $boolQuery->addMust($match); } foreach ($localeFilters as $locale) { $match = new Query\Match(); $match->setField('translations.locale', $locale); $boolQuery->addMust($match); } foreach ($publisherFilters as $publisher) { $match = new Query\Match(); $match->setField('publisher.name', $publisher); $boolQuery->addMust($match); } foreach ($indexFilters as $index) { $match = new Query\Match(); $match->setField('journalIndexs.index.name', $index); $boolQuery->addMust($match); } } //set our boolean query $searchQuery->setQuery($boolQuery); //get all result $searchQuery->setSize(1000); //get role aggregation $roleAgg = new Aggregation\Terms('roles'); $roleAgg->setField('userJournalRoles.role.name'); $roleAgg->setOrder('_term', 'asc'); $roleAgg->setSize(0); $searchQuery->addAggregation($roleAgg); //get subject aggregation $subjectAgg = new Aggregation\Terms('subjects'); $subjectAgg->setField('subjects.subject'); $subjectAgg->setOrder('_term', 'asc'); $subjectAgg->setSize(0); $searchQuery->addAggregation($subjectAgg); //get journal aggregation $journalAgg = new Aggregation\Terms('journals'); $journalAgg->setField('journal.title.raw'); $journalAgg->setOrder('_term', 'asc'); $journalAgg->setSize(0); $searchQuery->addAggregation($journalAgg); $localeAgg = new Aggregation\Terms('locales'); $localeAgg->setField('translations.locale'); $localeAgg->setOrder('_term', 'asc'); $localeAgg->setSize(0); $searchQuery->addAggregation($localeAgg); $publisherAgg = new Aggregation\Terms('publishers'); $publisherAgg->setField('publisher.name.raw'); $publisherAgg->setOrder('_term', 'asc'); $publisherAgg->setSize(0); $searchQuery->addAggregation($publisherAgg); $indexAgg = new Aggregation\Terms('indexes'); $indexAgg->setField('journalIndexs.index.name.raw'); $indexAgg->setOrder('_term', 'asc'); $indexAgg->setSize(0); $searchQuery->addAggregation($indexAgg); /** * @var ResultSet $resultData */ $resultData = $searcher->search($searchQuery); $roles = $resultData->getAggregation('roles')['buckets']; $subjects = $resultData->getAggregation('subjects')['buckets']; $journals = $resultData->getAggregation('journals')['buckets']; $locales = $resultData->getAggregation('locales')['buckets']; $publishers = $resultData->getAggregation('publishers')['buckets']; $indexes = $resultData->getAggregation('indexes')['buckets']; if ($resultData->count() > 0) { /** * manipulate result data for easily use on template */ $results = $searchManager->buildResultsObject($resultData, $section); /** * if search section is not defined or empty redirect to first result section */ if (empty($section)) { $section = array_keys($results)[0]; $redirectParams = array_merge($request->query->all(), ['section' => $section]); return $this->redirectToRoute('ojs_search_index', $redirectParams); } else { /** * if section result is empty redirect to first that have result section */ if (!isset($results[$section])) { foreach ($results as $resultKey => $result) { if ($result['total_item'] > 0) { $redirectParams = array_merge($request->query->all(), ['section' => $resultKey]); return $this->redirectToRoute('ojs_search_index', $redirectParams); } } } } $adapter = new ArrayAdapter($results[$section]['data']); $pagerfanta = new Pagerfanta($adapter); $pagerfanta->setMaxPerPage(20); $pagerfanta->setCurrentPage($page); $results[$section]['data'] = $pagerfanta->getCurrentPageResults(); /** * add search query to query history * history data stores on session */ $this->addQueryToHistory($request, $query, $queryType, $resultData->count()); $data = ['journalId' => $journalId, 'results' => $results, 'query' => $query, 'queryType' => $queryType, 'section' => $section, 'total_count' => $searchManager->getTotalHit(), 'roles' => $roles, 'subjects' => $subjects, 'locales' => $locales, 'journals' => $journals, 'publishers' => $publishers, 'indexes' => $indexes, 'role_filters' => $roleFilters, 'subject_filters' => $subjectFilters, 'journal_filters' => $journalFilters, 'locale_filters' => $localeFilters, 'publisher_filters' => $publisherFilters, 'index_filters' => $indexFilters, 'pagerfanta' => $pagerfanta, 'page' => $page]; } else { $data = ['journalId' => $journalId, 'query' => $query, 'queryType' => $queryType, 'total_count' => $searchManager->getTotalHit(), 'journals' => []]; } return $this->render('OjsSiteBundle:Search:index.html.twig', $data); }