/** * @param mixed $query * @param string $lang * @param string $type * * @return mixed|void */ public function defineSearch($query, $lang, $type) { $query = \Elastica\Util::escapeTerm($query); $elasticaQueryLang = new \Elastica\Query\Term(); $elasticaQueryLang->setTerm('lang', $lang); $elasticaQueryString = new \Elastica\Query\Match(); $elasticaQueryString->setFieldQuery('content', $query)->setFieldMinimumShouldMatch('content', '80%'); $elasticaQueryTitle = new \Elastica\Query\QueryString(); $elasticaQueryTitle->setDefaultField('title')->setBoost(2.0)->setQuery($query); $elasticaQueryBool = new \Elastica\Query\BoolQuery(); $elasticaQueryBool->addMust($elasticaQueryLang)->addShould($elasticaQueryTitle)->addShould($elasticaQueryString)->setMinimumNumberShouldMatch(1); $this->applySecurityContext($elasticaQueryBool); if (!is_null($type)) { $elasticaQueryType = new \Elastica\Query\Term(); $elasticaQueryType->setTerm('type', $type); $elasticaQueryBool->addMust($elasticaQueryType); } $rootNode = $this->domainConfiguration->getRootNode(); if (!is_null($rootNode)) { $elasticaQueryRoot = new \Elastica\Query\Term(); $elasticaQueryRoot->setTerm('root_id', $rootNode->getId()); $elasticaQueryBool->addMust($elasticaQueryRoot); } $this->query->setQuery($elasticaQueryBool); $this->query->setHighlight(array('pre_tags' => array('<strong>'), 'post_tags' => array('</strong>'), 'fields' => array('content' => array('fragment_size' => 150, 'number_of_fragments' => 3)))); }
/** * Cleans index. * * @param int $storeId * @param int $id * @param string $type * @return mixed */ public function cleanIndex($storeId = null, $id = null, $type = 'product') { $this->_prepareIndex(); if ($this->getStatus()->indexExists($this->_index)) { if (null === $storeId) { // no store filter if (empty($id)) { // delete ALL docs of type $type return $this->getIndex($this->_index)->getType($type)->delete(); } else { // delete docs of type $type with _id in $id foreach (Mage::app()->getStores() as $store) { $this->cleanIndex($store->getId(), $id, $type); } } } else { if (empty($id)) { // delete ALL docs from specific store $query = new \Elastica\Query\Term(); $query->setTerm('store_id', $storeId); $response = $this->getIndex($this->_index)->getType($type)->deleteByQuery($query); return $response; } else { // delete docs from specific store with _id in $id $ids = (array) $id; foreach ($ids as &$id) { $id .= '|' . $storeId; } unset($id); return $this->deleteIds($ids, $this->_index, $type); } } } return $this; }
/** * @group functional */ public function testQueryInsideHasParentSearch() { $index = $this->prepareSearchData(); $f = new \Elastica\Query\Term(); $f->setTerm('user', 'parent1'); $filter = new HasParent($f, 'parent'); $searchQuery = new \Elastica\Query(); $searchQuery->setPostFilter($filter); $searchResults = $index->search($searchQuery); $this->assertEquals(1, $searchResults->count()); $result = $searchResults->current()->getData(); $expected = array('id' => 'child1', 'user' => 'child1', 'email' => '*****@*****.**'); $this->assertEquals($expected, $result); }
public function searchRelatedEpisodes(Episode $episode, $numberResults = 5) { $tagtext = implode(" ", $episode->getTags()->toArray()); $boolQuery = new \Elastica\Query\BoolQuery(); $multiquery = new \Elastica\Query\MultiMatch(); $multiquery->setFields(['name', 'description', 'series']); $multiquery->setQuery($tagtext); $multiquery->setType(\Elastica\Query\MultiMatch::TYPE_MOST_FIELDS); $boolQuery->addMust($multiquery); $activeQuery = new \Elastica\Query\Term(); $activeQuery->setTerm('is_active', true); $boolQuery->addMust($activeQuery); $excludeEpisodeQuery = new \Elastica\Query\Term(); $excludeEpisodeQuery->setTerm('id', $episode->getId()); $boolQuery->addMustNot($excludeEpisodeQuery); return $this->episodeFinder->find($boolQuery, $numberResults); }
/** * @Route("/app/search", name="search") * @Template("AppBundle::search.html.twig") */ public function searchAction(Request $request) { $q = $request->query->get('q'); $finder = $this->get('fos_elastica.finder.app'); $tenantHelper = $this->get('multi_tenant.helper'); $boolQuery = new \Elastica\Query\BoolQuery(); $fieldQuery = new \Elastica\Query\MultiMatch(); $fieldQuery->setQuery($q); $fieldQuery->setFields(['_all']); $boolQuery->addMust($fieldQuery); $tenantQuery = new \Elastica\Query\Term(); $tenantQuery->setTerm('id', $tenantHelper->getCurrentTenant()->getId()); $nestedQuery = new \Elastica\Query\Nested(); $nestedQuery->setPath('tenant'); $nestedQuery->setQuery($tenantQuery); $boolQuery->addMust($nestedQuery); $results = $finder->findPaginated($boolQuery); return ['results' => $results, 'query' => $q]; }
public static function deleteByLibrary($libraryID) { Zotero_DB::beginTransaction(); // Delete from MySQL self::deleteByLibraryMySQL($libraryID); // Delete from Elasticsearch $type = self::getWriteType(); $libraryQuery = new \Elastica\Query\Term(); $libraryQuery->setTerm("libraryID", $libraryID); $query = new \Elastica\Query($libraryQuery); $start = microtime(true); $response = $type->deleteByQuery($query); StatsD::timing("elasticsearch.client.item_fulltext.delete_library", (microtime(true) - $start) * 1000); if ($response->hasError()) { throw new Exception($response->getError()); } Zotero_DB::commit(); }
/** * @param AJXP_Node $ajxpNode * @return Number */ public function getIndexedDocumentId($ajxpNode) { /* we used a term query that will check for the exact term (here is the path to a file) that can't be duplicate.Thus it will get the right result. */ $query = new Elastica\Query\Term(); $query->setTerm("node_url", $ajxpNode->getUrl()); /* returns a result set from the query */ $results = $this->currentIndex->search($query); if ($results->getTotalHits() == 0) { return null; } return $results->current()->getId(); }
protected function parseQueryString($queryString, array $opts) { $fields = $highlights = array(); $terms = preg_split('/\\s+/', $queryString); $match = $opts['match']; $case = $opts['case']; // Map each word in the query string with its corresponding field foreach ($terms as $term) { $prefix = strstr($term, '*', true); if ($prefix) { // For wildcard search $fields['content.prefix_complete'][] = $prefix; } elseif ($case === '1') { // For case sensitive search $fields['content.case_sensitive'][] = $term; } else { $fields['content'][] = $term; } } // Allow searching either by message content or message id (page name // without language subpage) with exact match only. $searchQuery = new \Elastica\Query\Bool(); foreach ($fields as $analyzer => $words) { foreach ($words as $word) { $boolQuery = new \Elastica\Query\Bool(); $contentQuery = new \Elastica\Query\Match(); $contentQuery->setFieldQuery($analyzer, $word); $boolQuery->addShould($contentQuery); $messageQuery = new \Elastica\Query\Term(); $messageQuery->setTerm('localid', $word); $boolQuery->addShould($messageQuery); if ($match === 'all') { $searchQuery->addMust($boolQuery); } else { $searchQuery->addShould($boolQuery); } // Fields for highlighting $highlights[$analyzer] = array('number_of_fragments' => 0); // Allow searching by exact message title (page name with // language subpage). $title = Title::newFromText($word); if (!$title) { continue; } $handle = new MessageHandle($title); if ($handle->isValid() && $handle->getCode() !== '') { $localid = $handle->getTitleForBase()->getPrefixedText(); $boolQuery = new \Elastica\Query\Bool(); $messageId = new \Elastica\Query\Term(); $messageId->setTerm('localid', $localid); $boolQuery->addMust($messageId); $searchQuery->addShould($boolQuery); } } } return array($searchQuery, $highlights); }