List of usage examples for org.apache.lucene.search Sort Sort
public Sort(SortField... fields)
From source file:com.zimbra.cs.index.AbstractIndexStoreTest.java
License:Open Source License
@Test public void sortedFilteredTermQuery() throws Exception { ZimbraLog.test.debug("--->TEST sortedFilteredTermQuery"); Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID); Folder folder = mbox.getFolderById(null, Mailbox.ID_FOLDER_CONTACTS); Contact con1 = createContact(mbox, "a", "bc", "abc@zimbra.com"); Contact con2 = createContact(mbox, "abcd@zimbra.com"); Contact con3 = createContact(mbox, "xy@zimbra.com"); Thread.sleep(1001); // To ensure different sort date Contact con4 = createContact(mbox, "xyz@zimbra.com"); Contact con5 = createContact(mbox, "xyz@zimbra.com"); mbox.index.indexDeferredItems(); // Make sure we don't index items after the deleteIndex() below IndexStore index = mbox.index.getIndexStore(); index.deleteIndex();/*ww w . jav a 2 s .c o m*/ Indexer indexer = index.openIndexer(); indexer.addDocument(folder, con1, con1.generateIndexData()); indexer.addDocument(folder, con2, con2.generateIndexData()); indexer.addDocument(folder, con3, con3.generateIndexData()); indexer.addDocument(folder, con4, con4.generateIndexData()); // Note: NOT indexed contact5 indexer.close(); List<Term> terms = Lists.newArrayList(); terms.add(new Term(LuceneFields.L_MAILBOX_BLOB_ID, String.valueOf(con2.getId()))); terms.add(new Term(LuceneFields.L_MAILBOX_BLOB_ID, String.valueOf(con4.getId()))); terms.add(new Term(LuceneFields.L_MAILBOX_BLOB_ID, String.valueOf(con5.getId()))); ZimbraTermsFilter filter = new ZimbraTermsFilter(terms); ZimbraIndexSearcher srchr = index.openSearcher(); ZimbraTopDocs result; Sort sort = new Sort(new SortField(LuceneFields.L_SORT_DATE, SortField.STRING, false)); result = srchr.search(new TermQuery(new Term(LuceneFields.L_CONTACT_DATA, "zimbra.com")), filter, 100, sort); Assert.assertNotNull("searcher.search result object - searching for zimbra.com", result); ZimbraLog.test.debug("Result for search for 'zimbra.com', filtering by IDs 2,4 & 5\n%s", result.toString()); Assert.assertEquals("Number of hits", 2, result.getTotalHits()); Assert.assertEquals("Match Blob ID 1", String.valueOf(con2.getId()), getBlobIdForResultDoc(srchr, result, 0)); Assert.assertEquals("Match Blob ID 2", String.valueOf(con4.getId()), getBlobIdForResultDoc(srchr, result, 1)); // Repeat but with a reverse sort this time sort = new Sort(new SortField(LuceneFields.L_SORT_DATE, SortField.STRING, true)); result = srchr.search(new TermQuery(new Term(LuceneFields.L_CONTACT_DATA, "zimbra.com")), filter, 100, sort); Assert.assertNotNull("searcher.search result object - searching for zimbra.com", result); ZimbraLog.test.debug("Result for search for 'zimbra.com' sorted reverse, filter by IDs\n%s", result.toString()); Assert.assertEquals("Number of hits", 2, result.getTotalHits()); Assert.assertEquals("Match Blob ID 1", String.valueOf(con4.getId()), getBlobIdForResultDoc(srchr, result, 0)); Assert.assertEquals("Match Blob ID 2", String.valueOf(con2.getId()), getBlobIdForResultDoc(srchr, result, 1)); srchr.close(); }
From source file:com.zimbra.cs.index.LuceneIndex.java
License:Open Source License
/** * Runs a common search query + common sort order (and throw away the result) to warm up the Lucene cache and OS * file system cache.// w w w .j a v a2s . c o m */ @Override public synchronized void warmup() { if (SEARCHER_CACHE.asMap().containsKey(mailbox.getId()) || GAL_SEARCHER_CACHE.containsKey(mailbox.getId())) { return; // already warmed up } long start = System.currentTimeMillis(); IndexSearcher searcher = null; try { searcher = (IndexSearcher) openSearcher(); searcher.search(new TermQuery(new Term(LuceneFields.L_CONTENT, "zimbra")), 1, new Sort(new SortField(LuceneFields.L_SORT_DATE, SortField.STRING, true))); } catch (IOException e) { ZimbraLog.search.warn("Failed to warm up", e); } finally { Closeables.closeQuietly(searcher); } ZimbraLog.search.debug("WarmUpLuceneSearcher elapsed=%d", System.currentTimeMillis() - start); }
From source file:com.zimbra.cs.index.LuceneQueryOperation.java
License:Open Source License
private Sort toLuceneSort(SortBy sortBy) { if (sortBy == null) { return null; }/*from w w w . j a va 2 s. co m*/ switch (sortBy.getKey()) { case NONE: return null; case NAME: case NAME_NATURAL_ORDER: case SENDER: return new Sort(new SortField(LuceneFields.L_SORT_NAME, SortField.STRING, sortBy.getDirection() == SortBy.Direction.DESC)); case SUBJECT: return new Sort(new SortField(LuceneFields.L_SORT_SUBJECT, SortField.STRING, sortBy.getDirection() == SortBy.Direction.DESC)); case SIZE: return new Sort(new SortField(LuceneFields.L_SORT_SIZE, SortField.LONG, sortBy.getDirection() == SortBy.Direction.DESC)); case ATTACHMENT: return new Sort(new SortField(LuceneFields.L_SORT_ATTACH, SortField.STRING, sortBy.getDirection() == SortBy.Direction.DESC)); case FLAG: return new Sort(new SortField(LuceneFields.L_SORT_FLAG, SortField.STRING, sortBy.getDirection() == SortBy.Direction.DESC)); case PRIORITY: return new Sort(new SortField(LuceneFields.L_SORT_PRIORITY, SortField.STRING, sortBy.getDirection() == SortBy.Direction.DESC)); case RCPT: assert false : sortBy; // should already be checked in the compile phase case DATE: default: // default to DATE_DESCENDING return new Sort(new SortField(LuceneFields.L_SORT_DATE, SortField.STRING, sortBy.getDirection() == SortBy.Direction.DESC)); } }
From source file:de.csw.linkgenerator.plugin.lucene.LucenePlugin.java
License:Open Source License
/** * Creates and submits a query to the Lucene engine. * //from w w w.ja va 2s.c o m * @param query The base query, using the query engine supported by Lucene. * @param sortField The name of a field to sort results by. If the name starts with '-', then * the field (excluding the -) is used for reverse sorting. If <tt>null</tt> or * empty, sort by hit score. * @param virtualWikiNames Comma separated list of virtual wiki names to search in, may be * <tt>null</tt> to search all virtual wikis. * @param languages Comma separated list of language codes to search in, may be <tt>null</tt> * or empty to search all languages. * @param indexes List of Lucene indexes (searchers) to search. * @param context The context of the request. * @return The list of search results. * @throws IOException If the Lucene searchers encounter a problem reading the indexes. * @throws ParseException If the query is not valid. */ private SearchResults search(String query, String sortField, String virtualWikiNames, String languages, IndexSearcher[] indexes, XWikiContext context) throws IOException, org.apache.lucene.queryparser.classic.ParseException { SortField sort = getSortField(sortField); // Perform the actual search return search(query, (sort != null) ? new Sort(sort) : null, virtualWikiNames, languages, indexes, context); }
From source file:de.csw.linkgenerator.plugin.lucene.LucenePlugin.java
License:Open Source License
/** * Creates and submits a query to the Lucene engine. * /*w w w .j a v a2 s.co m*/ * @param query The base query, using the query engine supported by Lucene. * @param sortFields A list of fields to sort results by. For each field, if the name starts * with '-', then that field (excluding the -) is used for reverse sorting. If * <tt>null</tt> or empty, sort by hit score. * @param virtualWikiNames Comma separated list of virtual wiki names to search in, may be * <tt>null</tt> to search all virtual wikis. * @param languages Comma separated list of language codes to search in, may be <tt>null</tt> * or empty to search all languages. * @param indexes List of Lucene indexes (searchers) to search. * @param context The context of the request. * @return The list of search results. * @throws IOException If the Lucene searchers encounter a problem reading the indexes. * @throws ParseException If the query is not valid. */ private SearchResults search(String query, String[] sortFields, String virtualWikiNames, String languages, IndexSearcher[] indexes, XWikiContext context) throws IOException, org.apache.lucene.queryparser.classic.ParseException { // Turn the sorting field names into SortField objects. SortField[] sorts = null; if (sortFields != null && sortFields.length > 0) { sorts = new SortField[sortFields.length]; for (int i = 0; i < sortFields.length; ++i) { sorts[i] = getSortField(sortFields[i]); } // Remove any null values from the list. int prevLength = -1; while (prevLength != sorts.length) { prevLength = sorts.length; sorts = (SortField[]) ArrayUtils.removeElement(sorts, null); } } // Perform the actual search return search(query, (sorts != null) ? new Sort(sorts) : null, virtualWikiNames, languages, indexes, context); }
From source file:de.ingrid.interfaces.csw.domain.filter.impl.LuceneFilterParser.java
License:EUPL
@Override public SpatialQuery parse(CSWQuery cswQuery) throws Exception { Document filterDoc = cswQuery.getConstraint(); if (this.filterUnmarshaller == null) { MarshallerPool marshallerPool = new MarshallerPool( "org.geotoolkit.ogc.xml.v110:org.geotoolkit.gml.xml.v311:org.geotoolkit.gml.xml.v321"); this.filterUnmarshaller = marshallerPool.acquireUnmarshaller(); }/*w w w .j a va2s. co m*/ if (filterDoc == null) { return new SpatialQuery(defaultField); } JAXBElement<FilterType> filterEl = this.filterUnmarshaller.unmarshal(filterDoc, FilterType.class); FilterType filter = filterEl.getValue(); SpatialQuery query = null; if (filter != null) { Filter nullFilter = null; // process logical operators like AND, OR, ... if (filter.getLogicOps() != null) { query = this.processLogicalOperator(filter.getLogicOps()); } // process comparison operators: PropertyIsLike, IsNull, IsBetween, // ... else if (filter.getComparisonOps() != null) { query = new SpatialQuery(this.processComparisonOperator(filter.getComparisonOps()), nullFilter, SerialChainFilter.AND); } // process spatial constraint : BBOX, Beyond, Overlaps, ... else if (filter.getSpatialOps() != null) { query = new SpatialQuery("", this.processSpatialOperator(filter.getSpatialOps()), SerialChainFilter.AND); } // process id else if (filter.getId() != null) { query = new SpatialQuery(this.processIDOperator(filter.getId()), nullFilter, SerialChainFilter.AND); } } Document sortBy = cswQuery.getSort(); if (sortBy != null) { NodeList sortProperties = this.xpath.getNodeList(sortBy, "//csw:SortProperty"); if (sortProperties != null && sortProperties.getLength() > 0) { List<SortField> sortFields = new ArrayList<SortField>(); for (int i = 0; i < sortProperties.getLength(); i++) { Node sortProperty = sortProperties.item(i); String propertyName = this.xpath.getString(sortProperty, "//csw:PropertyName"); String sortOrder = this.xpath.getString(sortProperty, "//csw:SortOrder"); if (sortOrder == null) { sortOrder = "ASC"; } // TODO determine type of sort field by queryable type sortFields.add(new SortField(propertyName + "_sort", SortField.STRING, sortOrder.equalsIgnoreCase("DESC") ? true : false)); } SortField[] a = sortFields.toArray(new SortField[0]); query.setSort(new Sort(a)); } } return query; }
From source file:de.innovationgate.wgpublisher.lucene.LuceneManager.java
License:Open Source License
public WGResultSet search(WGDatabase db, List<String> fields, String phrase, Map parameters, WGA wga) throws WGQueryException { if (wga == null) { wga = WGA.get(_core);//from w ww .j a va 2s . c om } // set max clause count for boolean queries BooleanQuery.setMaxClauseCount(_booleanQueryMaxClauseCount); if (this.isRebuildingIndex()) { throw new WGQueryException(phrase, "Lucene search temporary disabled. Rebuilding lucene index ..."); } // Registering problem in that case but not cancelling the query, as this is old, expected behaviour. The query will just return no results. if (!_core.getLuceneManager().indexIsEnabled(db.getDbReference())) { _core.getProblemRegistry().addProblem( Problem.create(new TMLContext.WebTMLOccasion(), new DatabaseScope(db.getDbReference()), "webtmlProblem.luceneIndexExpected", ProblemSeverity.LOW)); } if (phrase == null || phrase.trim().equals("")) { return null; } try { BooleanQuery wholeQuery = new BooleanQuery(); int max = WGACore.DEFAULT_QUERY_MAXRESULTS; Integer maxResults = (Integer) parameters.get(WGDatabase.QUERYOPTION_MAXRESULTS); if (maxResults != null) { if (maxResults == 0 || maxResults == -1) { max = Integer.MAX_VALUE; } else { max = maxResults; } } // handle dboption EXCLUDEDOCUMENT WGContent excludeContent = (WGContent) parameters.get(WGDatabase.QUERYOPTION_EXCLUDEDOCUMENT); if (excludeContent != null) { String uniqueKey = buildUniqueIndexKey(excludeContent.getDatabase().getDbReference(), excludeContent.getDocumentKey()); wholeQuery.add(new TermQuery(new Term(INDEXFIELD_UNIQUEKEY, uniqueKey)), BooleanClause.Occur.MUST_NOT); wholeQuery.add(new TermQuery(new Term(INDEXFIELD_PARENTKEY, uniqueKey)), BooleanClause.Occur.MUST_NOT); } // list of dbs to search in String searchScope = (String) parameters.get(LuceneManager.QUERYOPTION_SEARCHSCOPE); List searchDBKeys = new ArrayList(); if (searchScope.equals(LuceneManager.SEARCHSCOPE_DB)) { searchDBKeys.add(db.getDbReference()); } if (searchScope.equals(LuceneManager.SEARCHSCOPE_DOMAIN)) { Iterator<WGDatabase> dbs = _core .getDatabasesForDomain((String) db.getAttribute(WGACore.DBATTRIB_DOMAIN)).iterator(); while (dbs.hasNext()) { WGDatabase currentDB = dbs.next(); if (wga.openDatabase(currentDB)) { searchDBKeys.add(currentDB.getDbReference()); } } } if (searchScope.equals(LuceneManager.SEARCHSCOPE_WGA)) { Iterator dbs = _core.getContentdbs().values().iterator(); while (dbs.hasNext()) { WGDatabase currentDB = (WGDatabase) dbs.next(); if (wga.openDatabase(currentDB)) { searchDBKeys.add(currentDB.getDbReference()); } } } if (searchScope.equals(LuceneManager.SEARCHSCOPE_DB_LIST)) { String dbListCSV = (String) parameters.get(QUERYOPTION_SEARCHDBKEYS); if (dbListCSV == null || dbListCSV.trim().equals("")) { throw new WGQueryException(phrase, "Search scope is 'dblist' but no db keys given."); } else { Iterator dbkeys = WGUtils.deserializeCollection(dbListCSV, ",").iterator(); while (dbkeys.hasNext()) { String dbkey = (String) dbkeys.next(); WGDatabase currentDB = wga.db(dbkey); if (currentDB.isSessionOpen()) { searchDBKeys.add(dbkey.trim().toLowerCase()); } } } } // Handle language selection; List<WGLanguage> languagesPriorityList = null; boolean filterLanguages = false; if (parameters.containsKey(WGDatabase.QUERYOPTION_LANGUAGES)) { List<WGLanguage> langs = (List<WGLanguage>) parameters.get(WGDatabase.QUERYOPTION_LANGUAGES); if (langs.size() > 1) { BooleanQuery langQuery = new BooleanQuery(); for (WGLanguage lang : langs) { langQuery.add(new TermQuery(new Term(WGContent.META_LANGUAGE, lang.getName())), BooleanClause.Occur.SHOULD); } wholeQuery.add(langQuery, BooleanClause.Occur.MUST); languagesPriorityList = langs; filterLanguages = true; } else if (langs.size() == 1) { wholeQuery.add(new TermQuery(new Term(WGContent.META_LANGUAGE, langs.get(0).getName())), BooleanClause.Occur.MUST); languagesPriorityList = Collections.singletonList(langs.get(0)); } } else if (parameters.containsKey(WGDatabase.QUERYOPTION_ONLYLANGUAGE)) { String language = (String) parameters.get(WGDatabase.QUERYOPTION_ONLYLANGUAGE); wholeQuery.add(new TermQuery(new Term(WGContent.META_LANGUAGE, language)), BooleanClause.Occur.MUST); languagesPriorityList = Collections.singletonList(db.getLanguage(language)); } if (languagesPriorityList == null) { languagesPriorityList = getLanguagesForSearchDBKeys(searchDBKeys); ; } // Handle visibility selection if (!parameters.containsKey(WGDatabase.QUERYOPTION_ENHANCE) || parameters.get(WGDatabase.QUERYOPTION_ENHANCE).equals(new Boolean(true))) { wholeQuery.add(new TermQuery(new Term(WGContent.META_VISIBLE, "true")), BooleanClause.Occur.MUST); String role = (String) parameters.get(WGDatabase.QUERYOPTION_ROLE); if (role != null) { if (!role.equalsIgnoreCase(WGContent.DISPLAYTYPE_NONE)) { wholeQuery.add(new TermQuery(new Term("HIDDENIN" + role.toUpperCase(), "false")), BooleanClause.Occur.MUST); } } } if (parameters.containsKey(WGDatabase.QUERYOPTION_ONLYRELEASED)) { wholeQuery.add(new TermQuery(new Term(WGContent.META_STATUS, WGContent.STATUS_RELEASE)), BooleanClause.Occur.MUST); } // build dbQuery (OR combination of all searchDbs indexed by lucene) BooleanQuery dbQuery = new BooleanQuery(); Iterator itSearchDBKeys = searchDBKeys.iterator(); while (itSearchDBKeys.hasNext()) { String currentDBKey = (String) itSearchDBKeys.next(); if (_indexedDbs.containsKey(currentDBKey)) { dbQuery.add(new TermQuery(new Term(INDEXFIELD_DBKEY, currentDBKey)), BooleanClause.Occur.SHOULD); } } wholeQuery.add(dbQuery, BooleanClause.Occur.MUST); // Add parsed search phrase. // Search in allcontent for each language using the configured analyzer // if no analyzer is configured for a language search at least with one // default analyzer boolean searchWithDefaultAnalyzer = false; //if no languages found search at least with DefaultAnalyzer if (languagesPriorityList.size() <= 0) { searchWithDefaultAnalyzer = true; } // parse native options Sort sort = null; String sortFieldName = ""; Operator defaultOperator = QueryParser.AND_OPERATOR; String nativeOptionsStr = (String) parameters.get(WGDatabase.QUERYOPTION_NATIVEOPTIONS); boolean includeVirtualContent = false; String doctype = DOCTYPE_CONTENT; if (nativeOptionsStr != null) { Iterator nativeOptions = WGUtils.deserializeCollection(nativeOptionsStr, ",", true).iterator(); while (nativeOptions.hasNext()) { String option = (String) nativeOptions.next(); if (option.startsWith("sort:")) { sortFieldName = option.substring(5).trim(); boolean reverse = false; if (sortFieldName.toLowerCase().endsWith("(asc)")) { sortFieldName = sortFieldName.substring(0, sortFieldName.length() - 5).trim(); } else if (sortFieldName.toLowerCase().endsWith("(desc)")) { sortFieldName = sortFieldName.substring(0, sortFieldName.length() - 6).trim(); reverse = true; } if (sortFieldName.length() > 0) { char first = sortFieldName.charAt(0); if (first >= 'A' && first <= 'Z') { // meta sort sortFieldName = sortFieldName.toUpperCase(); } else { // item sort sortFieldName = sortFieldName.toLowerCase(); } } // sort order currently only german sort = new Sort(new SortField(SORTITEM_PREFIX + sortFieldName, Locale.GERMANY, reverse)); } else if (option.equalsIgnoreCase(NATIVE_QUERYOPTION_INCLUDEVIRTUALCONTENT)) { includeVirtualContent = true; } else if (option.startsWith("doctype:")) { doctype = option.substring("doctype:".length()).trim(); } else if (option.startsWith("operator:")) { String op = option.substring("operator:".length()).trim(); if (op.equalsIgnoreCase("or")) defaultOperator = QueryParser.OR_OPERATOR; } } } if (!includeVirtualContent) { wholeQuery.add(new TermQuery(new Term(INDEXFIELD_ISVIRTUALCONTENT, String.valueOf(true))), BooleanClause.Occur.MUST_NOT); } // handle doctype option // we cannot be sure that all documents in index already contains the field DOCTYPE (introduced with OpenWGA 7.1) therefore we have to perform some excludes if (doctype.equals(DOCTYPE_CONTENT)) { wholeQuery.add(new TermQuery(new Term(INDEXFIELD_DOCTYPE, DOCTYPE_ATTACHMENT)), BooleanClause.Occur.MUST_NOT); } else if (!doctype.equals(DOCTYPE_ALL)) { wholeQuery.add(new TermQuery(new Term(INDEXFIELD_DOCTYPE, doctype)), BooleanClause.Occur.MUST); } //build phrase query BooleanQuery phraseQuery = new BooleanQuery(); phraseQuery.setBoost(10); Iterator languageList = languagesPriorityList.iterator(); List<String> searchFields = new ArrayList<String>(); Map<String, Float> searchBoosts = new HashMap<String, Float>(); for (String field : fields) { String[] parts = field.split("\\^"); searchFields.add(parts[0]); if (parts.length == 2) { searchBoosts.put(parts[0], Float.parseFloat(parts[1])); } } if (!searchFields.contains("allcontent")) searchFields.add("allcontent"); if (!searchFields.contains("TITLE")) searchFields.add("TITLE"); if (!searchFields.contains("DESCRIPTION")) searchFields.add("DESCRIPTION"); if (!searchFields.contains("KEYWORDS")) searchFields.add("KEYWORDS"); while (languageList.hasNext()) { WGLanguage languageItem = (WGLanguage) languageList.next(); Analyzer analyzer = _core.getAnalyzerForLanguageCode(languageItem.getName().substring(0, 2)); if (analyzer != null) { QueryParser parser = new IndexingRuleBasedQueryParser(searchFields.toArray(new String[0]), analyzer, searchBoosts, _indexedDbs, searchDBKeys, _metaKeywordFields); parser.setDefaultOperator(defaultOperator); Query query = parser.parse(phrase); if (filterLanguages) { BooleanQuery testPhraseAndLangQuery = new BooleanQuery(); testPhraseAndLangQuery.add(query, BooleanClause.Occur.MUST); testPhraseAndLangQuery.add( new TermQuery(new Term(WGContent.META_LANGUAGE, languageItem.getName())), BooleanClause.Occur.MUST); phraseQuery.add(testPhraseAndLangQuery, BooleanClause.Occur.SHOULD); } else { phraseQuery.add(query, BooleanClause.Occur.SHOULD); } } else { searchWithDefaultAnalyzer = true; } } if (searchWithDefaultAnalyzer) { QueryParser parser = new IndexingRuleBasedQueryParser(searchFields.toArray(new String[0]), _core.getDefaultAnalyzer(), searchBoosts, _indexedDbs, searchDBKeys, _metaKeywordFields); parser.setDefaultOperator(defaultOperator); Query query = parser.parse(phrase); phraseQuery.add(query, BooleanClause.Occur.SHOULD); } //LOG.info(phraseQuery.toString()); wholeQuery.add(phraseQuery, BooleanClause.Occur.MUST); TopDocs hits; //register executed query as output parameter parameters.put(WGDatabase.QUERYOPTION_RETURNQUERY, wholeQuery.toString()); // simplify query and register as taginfo parameters.put(TAGINFO_SIMPLIFIEDQUERY, rewrite(wholeQuery)); long timeBefore = System.currentTimeMillis(); if (sort != null) { try { hits = search(wholeQuery, max, sort); } catch (NullPointerException e) { // lucene bug when sorting for non existing fields with Locale throw new WGQueryException(wholeQuery.toString(), "Sortfield '" + sortFieldName + "' not indexed."); } } else { try { hits = search(wholeQuery, max, null); } catch (BooleanQuery.TooManyClauses e) { parameters.put(TAGINFO_UNSPECIFICQUERY, new Boolean(true)); throw new WGQueryException(phrase, "Too many BooleanClauses in query. " + "Please use a more specific query or increase value of " + "'booleanQueryMaxClauseCount' via WGAManager. Current value is '" + this.getBooleanQueryMaxClauseCount() + "'."); } } long timeAfter = System.currentTimeMillis(); long executionTime = timeAfter - timeBefore; LuceneResultSet resultSet; if (filterLanguages) { resultSet = new LuceneLanguageChoosingResultSet(hits, wga, parameters, wholeQuery, executionTime, languagesPriorityList); } else { resultSet = new LuceneMultiDBResultSet(hits, wga, parameters, wholeQuery, executionTime); } // put resultset in per thread list List rsList = (List) _resultsetList.get(); if (rsList == null) { rsList = new LinkedList(); _resultsetList.set(rsList); } rsList.add(resultSet); return resultSet; } catch (org.apache.lucene.queryParser.ParseException e) { throw new WGQueryException("Unable to parse lucene query", e.getMessage(), e); } catch (Exception e) { LOG.error("Error executing lucene search: " + e.getClass().getName() + " - " + e.getMessage(), e); throw new WGQueryException(phrase, e.getClass().getName() + ": " + e.getMessage(), e); } }
From source file:de.iteratec.iteraplan.businesslogic.service.SearchServiceImpl.java
License:Open Source License
/** * Execute the hibernate search/*w ww . j a v a 2 s .c o m*/ * * @param searchMap * Multimap of SearhRowDTOs contains the results * @param queryString * the query string which the user entered */ private void executeSearchQuery(Multimap<String, SearchRowDTO> searchMap, String queryString, String buildingBlockTypeFilter) { // reader provider is required to close the reader after search has finished ReaderProvider readerProvider = searchDAO.getReaderProvider(); IndexReader reader = searchDAO.openReader(readerProvider, getClassArray()); // if the reader is null (i.e. the user has no functional permissions to search any one of the // building blocks) return without executing a search if (reader == null) { return; } try { // index fields that will be searched String[] productFields = { "attributeValueAssignments.attributeValue.valueString", "name", "version", "description", "informationSystem.name", "technicalComponent.name", "runtimePeriod.start", "runtimePeriod.end", "informationSystemReleaseA.informationSystem.name", "informationSystemReleaseA.version", "informationSystemReleaseB.informationSystem.name", "informationSystemReleaseB.version" }; QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_31, productFields, new StandardAnalyzer(Version.LUCENE_31)); // allow wildcard * at the beginning of a query string parser.setAllowLeadingWildcard(true); // automatically put quotes around the search term for phrase search, because that's what most people expect parser.setAutoGeneratePhraseQueries(true); // workaround for known issue with highlighter and wildcard queries parser.setMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE); Query luceneQuery = null; try { // parse the query string luceneQuery = parser.parse(queryString); // rewrite luceneQuery for highlighting luceneQuery = luceneQuery.rewrite(reader); } catch (TooManyClauses tmcEx) { throw new IteraplanBusinessException(IteraplanErrorMessages.LUCENE_QUERY_TOO_COMPLEX, tmcEx); } catch (ParseException e) { throw new IteraplanBusinessException(IteraplanErrorMessages.LUCENE_QUERY_PARSE_FAILED, e); } catch (IOException e) { throw new IteraplanTechnicalException(IteraplanErrorMessages.LUCENE_QUERY_REWRITE_FAILED, e); } // the found content is being highlighted SimpleHTMLFormatter sHtmlF = new SimpleHTMLFormatter("<span class=\"highlighted\">", "</span>"); Highlighter highlighter = new Highlighter(sHtmlF, new QueryScorer(luceneQuery)); highlighter.setTextFragmenter(new SimpleFragmenter(40)); // sorted by Building block types SortField sortField = new SortField("buildingBlockType.name", SortField.STRING); Sort sort = new Sort(sortField); String[] projections = { "id", "buildingBlockType.name", FullTextQuery.DOCUMENT, "name", "informationSystem.name", "technicalComponent.name", "version", "description", "runtimePeriod.start", "runtimePeriod.end", "informationSystemReleaseA.informationSystem.name", "informationSystemReleaseA.version", "informationSystemReleaseB.informationSystem.name", "informationSystemReleaseB.version" }; List<Object[]> results = searchDAO.search(luceneQuery, sort, getClassArray(), projections); addResultsToSearchMap(searchMap, results, buildingBlockTypeFilter, highlighter); } finally { readerProvider.closeReader(reader); } }
From source file:de.powerstaff.business.service.impl.ProfileSearchServiceImpl.java
License:Open Source License
@Override public DataPage<ProfileSearchEntry> findProfileDataPage(SavedProfileSearch aRequest, int startRow, int pageSize) throws Exception { if (aRequest.getId() == null) { // Kann passieren, wenn die Suche das erste mal aufgerufen wird return new DataPage<ProfileSearchEntry>(0, 0, new ArrayList<ProfileSearchEntry>()); }//from ww w.j av a 2 s . co m Analyzer theAnalyzer = ProfileAnalyzerFactory.createAnalyzer(); FullTextSession theSession = Search.getFullTextSession(sessionFactory.getCurrentSession()); Query theQuery = getRealQuery(aRequest, theAnalyzer); LOGGER.info("Search query is " + theQuery + " from " + startRow + " with pagesize " + pageSize); Highlighter theHighlighter = new Highlighter(new SpanGradientFormatter(1, "#000000", "#0000FF", null, null), new QueryScorer(theQuery)); BooleanQuery theRealQuery = new BooleanQuery(); theRealQuery.add(theQuery, Occur.MUST); if (aRequest != null) { for (String theId : aRequest.getProfilesToIgnore()) { theRealQuery.add(new TermQuery(new Term(ProfileIndexerService.UNIQUE_ID, theId)), Occur.MUST_NOT); } } LOGGER.info("Query with ignore is " + theRealQuery); Sort theSort = null; if (!StringUtils.isEmpty(aRequest.getSortierung())) { int theSortType = SortField.STRING; boolean theReverse = false; String theSortField = aRequest.getSortierungField(); if (ProfileIndexerService.STUNDENSATZ.equals(theSortField)) { theSortType = SortField.LONG; } if (ProfileIndexerService.VERFUEGBARKEIT.equals(theSortField)) { theReverse = true; } if (ProfileIndexerService.LETZTERKONTAKT.equals(theSortField)) { theReverse = true; } if (aRequest.isSortierungReverse()) { theReverse = !theReverse; } theSort = new Sort(new SortField(theSortField, theSortType, theReverse)); } List<Filter> theFilterList = new ArrayList<Filter>(); TermsFilter theContactForbidden = new TermsFilter(); theContactForbidden.addTerm(new Term(ProfileIndexerService.KONTAKTSPERRE, "false")); theFilterList.add(theContactForbidden); if (aRequest.getStundensatzVon() != null || aRequest.getStundensatzBis() != null) { if (aRequest.getStundensatzVon() != null) { theFilterList.add(NumericRangeFilter.newLongRange(ProfileIndexerService.STUNDENSATZ, aRequest.getStundensatzVon(), Long.MAX_VALUE, true, true)); } if (aRequest.getStundensatzBis() != null) { theFilterList.add(NumericRangeFilter.newLongRange(ProfileIndexerService.STUNDENSATZ, 0l, aRequest.getStundensatzBis(), true, true)); } } Filter theFilter = new ChainedFilter(theFilterList.toArray(new Filter[theFilterList.size()]), ChainedFilter.AND); int theEnd = startRow + pageSize; FullTextQuery theHibernateQuery = theSession.createFullTextQuery(theRealQuery, Freelancer.class); if (theFilter != null) { theHibernateQuery.setFilter(theFilter); } if (theSort != null) { theHibernateQuery.setSort(theSort); } theHibernateQuery.setFirstResult(startRow); theHibernateQuery.setMaxResults(theEnd - startRow); theHibernateQuery.setProjection(FullTextQuery.THIS, FullTextQuery.DOCUMENT); List<ProfileSearchEntry> theResult = new ArrayList<ProfileSearchEntry>(); for (Object theSingleEntity : theHibernateQuery.list()) { Object[] theRow = (Object[]) theSingleEntity; Freelancer theFreelancer = (Freelancer) theRow[0]; Document theDocument = (Document) theRow[1]; ProfileSearchEntry theEntry = createResultEntry(theAnalyzer, theQuery, theHighlighter, theFreelancer, theDocument); theResult.add(theEntry); } return new DataPage<ProfileSearchEntry>(theHibernateQuery.getResultSize(), startRow, theResult); }
From source file:de.uni_koeln.spinfo.maalr.lucene.core.Dictionary.java
License:Apache License
public QueryResult queryExact(String phrase, boolean firstLanguage) throws NoIndexAvailableException, BrokenIndexException, InvalidQueryException { String sortField = null;/* w w w . j a va2s . co m*/ List<Query> queries = null; sortField = description.getSortOrder(firstLanguage); if (firstLanguage) { queries = exactMatchesLangA.transform(phrase); } else { queries = exactMatchesLangB.transform(phrase); } int pageSize = 120; try { BooleanQuery query = new BooleanQuery(true); for (Query q : queries) { query.add(q, Occur.SHOULD); } BooleanQuery bc = new BooleanQuery(); bc.add(query, Occur.MUST); bc.add(new TermQuery(new Term(LemmaVersion.VERIFICATION, Verification.ACCEPTED.toString())), Occur.MUST); query = bc; TopDocs docs = indexProvider.getSearcher().search(query, null, pageSize, new Sort(new SortField(sortField, SortField.Type.STRING))); return toQueryResult(docs, 0, pageSize); } catch (IOException e) { throw new BrokenIndexException("Broken index!", e); } catch (InvalidTokenOffsetsException e) { throw new InvalidQueryException("Highlighting failed", e); } }