List of usage examples for org.apache.lucene.search.highlight SpanGradientFormatter SpanGradientFormatter
public SpanGradientFormatter(float maxScore, String minForegroundColor, String maxForegroundColor, String minBackgroundColor, String maxBackgroundColor)
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>()); }//www .j a va 2 s . c om 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.powerstaff.business.service.impl.ProfileSearchServiceImpl.java
License:Open Source License
@Override public List<ProfileSearchEntry> getSimilarFreelancer(Freelancer aFreelancer) { List<ProfileSearchEntry> theResult = new ArrayList<ProfileSearchEntry>(); if (aFreelancer != null && aFreelancer.getId() != null) { FullTextSession theSession = Search.getFullTextSession(sessionFactory.getCurrentSession()); SearchFactory theSearchFactory = theSession.getSearchFactory(); Analyzer theAnalyzer = ProfileAnalyzerFactory.createAnalyzer(); DirectoryProvider theFreeelancerProvider = theSearchFactory.getDirectoryProviders(Freelancer.class)[0]; IndexReader theIndexReader = null; try {// w w w .j a va2s . c o m theIndexReader = theSearchFactory.getReaderProvider().openReader(theFreeelancerProvider); MoreLikeThis theMoreLikeThis = new MoreLikeThis(theIndexReader); // Zuerst den Freiberufler raussuchen Query theQuery = new TermQuery( new Term(ProfileIndexerService.UNIQUE_ID, aFreelancer.getId().toString())); FullTextQuery theHibernateQuery = theSession.createFullTextQuery(theQuery, Freelancer.class); theHibernateQuery.setProjection(FullTextQuery.THIS, FullTextQuery.DOCUMENT); for (Object theSingleEntity : theHibernateQuery.list()) { Object[] theRow = (Object[]) theSingleEntity; Freelancer theFreelancer = (Freelancer) theRow[0]; Document theDocument = (Document) theRow[1]; theMoreLikeThis.setMinDocFreq(1); theMoreLikeThis.setMinTermFreq(1); theMoreLikeThis.setAnalyzer(theAnalyzer); theMoreLikeThis.setFieldNames(new String[] { ProfileIndexerService.CONTENT }); Query theMltQuery = theMoreLikeThis .like(new StringReader(theDocument.get(ProfileIndexerService.ORIG_CONTENT))); FullTextQuery theMoreLikeThisQuery = theSession.createFullTextQuery(theMltQuery, Freelancer.class); theMoreLikeThisQuery.setProjection(FullTextQuery.THIS, FullTextQuery.DOCUMENT, FullTextQuery.SCORE); theMoreLikeThisQuery.setMaxResults(50); Highlighter theHighlighter = new Highlighter( new SpanGradientFormatter(1, "#000000", "#0000FF", null, null), new QueryScorer(theMltQuery)); for (Object theSingleMltEntry : theMoreLikeThisQuery.list()) { Object[] theMltRow = (Object[]) theSingleMltEntry; Freelancer theMltFreelancer = (Freelancer) theMltRow[0]; Document theMltDocument = (Document) theMltRow[1]; Float theMltScore = (Float) theMltRow[2]; if (theMltFreelancer != theFreelancer) { // Einen gefunden ProfileSearchEntry theEntry = createResultEntry(theAnalyzer, theMltQuery, theHighlighter, theMltFreelancer, theMltDocument); theResult.add(theEntry); } } } } catch (Exception e) { throw new RuntimeException(e); } finally { if (theIndexReader != null) { theSearchFactory.getReaderProvider().closeReader(theIndexReader); } } } return theResult; }
From source file:de.powerstaff.business.service.impl.ProfileSearchServiceImpl.java
License:Open Source License
@Override public List<ProfileSearchEntry> getSimilarFreelancer(Project aProject) { List<ProfileSearchEntry> theResult = new ArrayList<ProfileSearchEntry>(); if (aProject != null && aProject.getId() != null) { FullTextSession theSession = Search.getFullTextSession(sessionFactory.getCurrentSession()); SearchFactory theSearchFactory = theSession.getSearchFactory(); Analyzer theAnalyzer = ProfileAnalyzerFactory.createAnalyzer(); DirectoryProvider theFreeelancerProvider = theSearchFactory.getDirectoryProviders(Freelancer.class)[0]; IndexReader theIndexReader = null; try {/* ww w.java2 s. c o m*/ theIndexReader = theSearchFactory.getReaderProvider().openReader(theFreeelancerProvider); MoreLikeThis theMoreLikeThis = new MoreLikeThis(theIndexReader); theMoreLikeThis.setMinDocFreq(1); theMoreLikeThis.setMinTermFreq(1); theMoreLikeThis.setAnalyzer(theAnalyzer); theMoreLikeThis.setFieldNames(new String[] { ProfileIndexerService.CONTENT }); Query theMltQuery = theMoreLikeThis.like(new StringReader(aProject.getDescriptionShort() + " " + aProject.getDescriptionLong() + " " + aProject.getSkills())); FullTextQuery theMoreLikeThisQuery = theSession.createFullTextQuery(theMltQuery, Freelancer.class); theMoreLikeThisQuery.setProjection(FullTextQuery.THIS, FullTextQuery.DOCUMENT, FullTextQuery.SCORE); theMoreLikeThisQuery.setMaxResults(50); Highlighter theHighlighter = new Highlighter( new SpanGradientFormatter(1, "#000000", "#0000FF", null, null), new QueryScorer(theMltQuery)); for (Object theSingleMltEntry : theMoreLikeThisQuery.list()) { Object[] theMltRow = (Object[]) theSingleMltEntry; Freelancer theMltFreelancer = (Freelancer) theMltRow[0]; Document theMltDocument = (Document) theMltRow[1]; Float theMltScore = (Float) theMltRow[2]; ProfileSearchEntry theEntry = createResultEntry(theAnalyzer, theMltQuery, theHighlighter, theMltFreelancer, theMltDocument); theResult.add(theEntry); } } catch (Exception e) { throw new RuntimeException(e); } finally { if (theIndexReader != null) { theSearchFactory.getReaderProvider().closeReader(theIndexReader); } } } return theResult; }
From source file:org.compass.core.lucene.engine.highlighter.DefaultLuceneHighlighterFactory.java
License:Apache License
protected Formatter createFormatter(String highlighterName, CompassSettings settings) throws SearchEngineException { Formatter formatter;//from w w w . jav a 2s. c o m Object obj = settings.getSettingAsObject(LuceneEnvironment.Highlighter.Formatter.TYPE); if (obj instanceof Formatter) { formatter = (Formatter) obj; if (log.isDebugEnabled()) { log.debug("Highlighter [" + highlighterName + "] uses formatter instance [" + formatter + "]"); } } else { String formatterSettings = settings.getSetting(LuceneEnvironment.Highlighter.Formatter.TYPE, LuceneEnvironment.Highlighter.Formatter.SIMPLE); if (log.isDebugEnabled()) { log.debug("Highlighter [" + highlighterName + "] uses formatter [" + formatterSettings + "]"); } if (LuceneEnvironment.Highlighter.Formatter.SIMPLE.equals(formatterSettings)) { String preTag = settings.getSetting(LuceneEnvironment.Highlighter.Formatter.SIMPLE_PRE_HIGHLIGHT, "<b>"); String postTag = settings.getSetting(LuceneEnvironment.Highlighter.Formatter.SIMPLE_POST_HIGHLIGHT, "</b>"); formatter = new SimpleHTMLFormatter(preTag, postTag); if (log.isDebugEnabled()) { log.debug("Highlighter [" + highlighterName + "] uses pre [" + preTag + "] and post [" + postTag + "]"); } } else if (LuceneEnvironment.Highlighter.Formatter.HTML_SPAN_GRADIENT.equals(formatterSettings)) { float maxScore = settings.getSettingAsFloat( LuceneEnvironment.Highlighter.Formatter.HTML_SPAN_GRADIENT_MAX_SCORE, Float.MIN_VALUE); if (maxScore == Float.MIN_VALUE) { throw new SearchEngineException("Highlighter [" + highlighterName + "] uses span formatter and must set the [" + LuceneEnvironment.Highlighter.Formatter.HTML_SPAN_GRADIENT_MAX_SCORE + "] setting"); } String minForegroundColor = settings.getSetting( LuceneEnvironment.Highlighter.Formatter.HTML_SPAN_GRADIENT_MIN_FOREGROUND_COLOR); String maxForegroundColor = settings.getSetting( LuceneEnvironment.Highlighter.Formatter.HTML_SPAN_GRADIENT_MAX_FOREGROUND_COLOR); String minBackgroundColor = settings.getSetting( LuceneEnvironment.Highlighter.Formatter.HTML_SPAN_GRADIENT_MIN_BACKGROUND_COLOR); String maxBackgroundColor = settings.getSetting( LuceneEnvironment.Highlighter.Formatter.HTML_SPAN_GRADIENT_MAX_BACKGROUND_COLOR); try { formatter = new SpanGradientFormatter(maxScore, minForegroundColor, maxForegroundColor, minBackgroundColor, maxBackgroundColor); } catch (IllegalArgumentException e) { throw new SearchEngineException("Highlighter [" + highlighterName + "] using span gradient formatter failed [" + e.getMessage() + "]"); } } else { try { // the formatter is the fully qualified class name formatter = (Formatter) ClassUtils.forName(formatterSettings, settings.getClassLoader()) .newInstance(); } catch (Exception e) { throw new SearchEngineException( "Cannot instantiate Lucene formatter [" + formatterSettings + "] for highlighter [" + highlighterName + "]. Please verify the highlighter formatter setting at [" + LuceneEnvironment.Highlighter.Formatter.TYPE + "]", e); } } } if (formatter instanceof CompassConfigurable) { ((CompassConfigurable) formatter).configure(settings); } return formatter; }