當前位置: 首頁>>代碼示例>>Java>>正文


Java Highlighter.getBestFragments方法代碼示例

本文整理匯總了Java中org.apache.lucene.search.highlight.Highlighter.getBestFragments方法的典型用法代碼示例。如果您正苦於以下問題:Java Highlighter.getBestFragments方法的具體用法?Java Highlighter.getBestFragments怎麽用?Java Highlighter.getBestFragments使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.lucene.search.highlight.Highlighter的用法示例。


在下文中一共展示了Highlighter.getBestFragments方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: highlight

import org.apache.lucene.search.highlight.Highlighter; //導入方法依賴的package包/類
@Override
public String highlight(String locale, boolean useStopWords, String query, String content, String pre, String post, int preview) {
    Analyzer analyzer = termAnalyzers.findAnalyzer(locale, useStopWords);
    QueryParser parser = new QueryParser(defaultField, analyzer);

    String summary = null;
    try {
        SimpleHTMLFormatter formatter = new SimpleHTMLFormatter(pre, post);
        Highlighter hg = new Highlighter(formatter, new QueryTermScorer(parser.parse(query)));
        hg.setMaxDocCharsToAnalyze(preview);
        hg.setTextFragmenter(new SimpleFragmenter(100));

        TokenStream tokens = TokenSources.getTokenStream(defaultField, content, analyzer);
        summary = hg.getBestFragments(tokens, content, 4, " ... ");
    } catch (InvalidTokenOffsetsException | IOException | ParseException ex) {
        LOG.error("Failed to highlight", ex);
    }

    return StringUtils.isBlank(summary) ? null : summary;
}
 
開發者ID:jivesoftware,項目名稱:miru,代碼行數:21,代碼來源:LuceneBackedQueryParser.java

示例2: highlightField

import org.apache.lucene.search.highlight.Highlighter; //導入方法依賴的package包/類
/**
 * This method intended for use with
 * <tt>testHighlightingWithDefaultField()</tt>
 */
private String highlightField(Query query, String fieldName,
    String text) throws IOException, InvalidTokenOffsetsException {
  TokenStream tokenStream = new MockAnalyzer(random(), MockTokenizer.SIMPLE,
      true, MockTokenFilter.ENGLISH_STOPSET, true).tokenStream(fieldName,
      new StringReader(text));
  // Assuming "<B>", "</B>" used to highlight
  SimpleHTMLFormatter formatter = new SimpleHTMLFormatter();
  MyQueryScorer scorer = new MyQueryScorer(query, fieldName, FIELD_NAME);
  Highlighter highlighter = new Highlighter(formatter, scorer);
  highlighter.setTextFragmenter(new SimpleFragmenter(Integer.MAX_VALUE));

  String rv = highlighter.getBestFragments(tokenStream, text, 1,
      "(FIELD TEXT TRUNCATED)");
  return rv.length() == 0 ? text : rv;
}
 
開發者ID:pkarmstr,項目名稱:NYBC,代碼行數:20,代碼來源:HighlightCustomQueryTest.java

示例3: highlightField

import org.apache.lucene.search.highlight.Highlighter; //導入方法依賴的package包/類
/**
 * This method intended for use with
 * <tt>testHighlightingWithDefaultField()</tt>
 */
private String highlightField(Query query, String fieldName,
    String text) throws IOException, InvalidTokenOffsetsException {
  TokenStream tokenStream = new MockAnalyzer(random(), MockTokenizer.SIMPLE,
      true, MockTokenFilter.ENGLISH_STOPSET).tokenStream(fieldName, text);
  // Assuming "<B>", "</B>" used to highlight
  SimpleHTMLFormatter formatter = new SimpleHTMLFormatter();
  MyQueryScorer scorer = new MyQueryScorer(query, fieldName, FIELD_NAME);
  Highlighter highlighter = new Highlighter(formatter, scorer);
  highlighter.setTextFragmenter(new SimpleFragmenter(Integer.MAX_VALUE));

  String rv = highlighter.getBestFragments(tokenStream, text, 1,
      "(FIELD TEXT TRUNCATED)");
  return rv.length() == 0 ? text : rv;
}
 
開發者ID:europeana,項目名稱:search,代碼行數:19,代碼來源:HighlightCustomQueryTest.java

示例4: doHighlight

import org.apache.lucene.search.highlight.Highlighter; //導入方法依賴的package包/類
/**
 * Highlight (bold,color) query words in result-document. Set HighlightResult for content or description.
 * 
 * @param query
 * @param analyzer
 * @param doc
 * @param resultDocument
 * @throws IOException
 */
private void doHighlight(final Query query, final Analyzer analyzer, final Document doc, final ResultDocument resultDocument) throws IOException {
    final Highlighter highlighter = new Highlighter(new SimpleHTMLFormatter(HIGHLIGHT_PRE_TAG, HIGHLIGHT_POST_TAG), new QueryScorer(query));
    // Get 3 best fragments of content and seperate with a "..."
    try {
        // highlight content
        final String content = doc.get(AbstractOlatDocument.CONTENT_FIELD_NAME);
        TokenStream tokenStream = analyzer.tokenStream(AbstractOlatDocument.CONTENT_FIELD_NAME, new StringReader(content));
        String highlightResult = highlighter.getBestFragments(tokenStream, content, 3, HIGHLIGHT_SEPARATOR);

        // if no highlightResult is in content => look in description
        if (highlightResult.length() == 0) {
            final String description = doc.get(AbstractOlatDocument.DESCRIPTION_FIELD_NAME);
            tokenStream = analyzer.tokenStream(AbstractOlatDocument.DESCRIPTION_FIELD_NAME, new StringReader(description));
            highlightResult = highlighter.getBestFragments(tokenStream, description, 3, HIGHLIGHT_SEPARATOR);
            resultDocument.setHighlightingDescription(true);
        }
        resultDocument.setHighlightResult(highlightResult);

        // highlight title
        final String title = doc.get(AbstractOlatDocument.TITLE_FIELD_NAME);
        tokenStream = analyzer.tokenStream(AbstractOlatDocument.TITLE_FIELD_NAME, new StringReader(title));
        final String highlightTitle = highlighter.getBestFragments(tokenStream, title, 3, " ");
        resultDocument.setHighlightTitle(highlightTitle);
    } catch (final InvalidTokenOffsetsException e) {
        log.warn("", e);
    }
}
 
開發者ID:huihoo,項目名稱:olat,代碼行數:37,代碼來源:SearchResultsImpl.java

示例5: getResult

import org.apache.lucene.search.highlight.Highlighter; //導入方法依賴的package包/類
public String getResult(String fieldName, String fieldValue) throws Exception{
    BuguIndex index = BuguIndex.getInstance();
    QueryParser parser = new QueryParser(index.getVersion(), fieldName, index.getAnalyzer());
    Query query = parser.parse(keywords);
    TokenStream tokens = index.getAnalyzer().tokenStream(fieldName, new StringReader(fieldValue));
    QueryScorer scorer = new QueryScorer(query, fieldName);
    Highlighter highlighter = new Highlighter(formatter, scorer);
    highlighter.setTextFragmenter(new SimpleSpanFragmenter(scorer));
    return highlighter.getBestFragments(tokens, fieldValue, maxFragments, "...");
}
 
開發者ID:xbwen,項目名稱:bugu-mongo,代碼行數:11,代碼來源:BuguHighlighter.java

示例6: main

import org.apache.lucene.search.highlight.Highlighter; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
	if (args.length != 0) {
		QUERY = args[0];
	}
	// 將庖丁封裝成符合Lucene要求的Analyzer規範
	Analyzer analyzer = new PaodingAnalyzer();
	
	//讀取本類目錄下的text.txt文件
	String content = ContentReader.readText(English.class);

	//接下來是標準的Lucene建立索引和檢索的代碼
	Directory ramDir = new RAMDirectory();
	IndexWriter writer = new IndexWriter(ramDir, analyzer);
	Document doc = new Document();
	Field fd = new Field(FIELD_NAME, content, Field.Store.YES,
			Field.Index.TOKENIZED, Field.TermVector.WITH_POSITIONS_OFFSETS);
	doc.add(fd);
	writer.addDocument(doc);
	writer.optimize();
	writer.close();

	IndexReader reader = IndexReader.open(ramDir);
	String queryString = QUERY;
	QueryParser parser = new QueryParser(FIELD_NAME, analyzer);
	Query query = parser.parse(queryString);
	Searcher searcher = new IndexSearcher(ramDir);
	query = query.rewrite(reader);
	System.out.println("Searching for: " + query.toString(FIELD_NAME));
	Hits hits = searcher.search(query);

	BoldFormatter formatter = new BoldFormatter();
	Highlighter highlighter = new Highlighter(formatter, new QueryScorer(
			query));
	highlighter.setTextFragmenter(new SimpleFragmenter(50));
	for (int i = 0; i < hits.length(); i++) {
		String text = hits.doc(i).get(FIELD_NAME);
		int maxNumFragmentsRequired = 5;
		String fragmentSeparator = "...";
		TermPositionVector tpv = (TermPositionVector) reader
				.getTermFreqVector(hits.id(i), FIELD_NAME);
		TokenStream tokenStream = TokenSources.getTokenStream(tpv);
		String result = highlighter.getBestFragments(tokenStream, text,
				maxNumFragmentsRequired, fragmentSeparator);
		System.out.println("\n" + result);
	}
	reader.close();
}
 
開發者ID:no8899,項目名稱:paoding-for-lucene-2.4,代碼行數:48,代碼來源:English.java

示例7: main

import org.apache.lucene.search.highlight.Highlighter; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
	if (args.length != 0) {
		QUERY = args[0];
	}
	// 將庖丁封裝成符合Lucene要求的Analyzer規範
	Analyzer analyzer = new PaodingAnalyzer();
	
	//讀取本類目錄下的text.txt文件
	String content = ContentReader.readText(Chinese.class);

	//接下來是標準的Lucene建立索引和檢索的代碼
	Directory ramDir = new RAMDirectory();
	IndexWriter writer = new IndexWriter(ramDir, analyzer);
	Document doc = new Document();
	Field fd = new Field(FIELD_NAME, content, Field.Store.YES,
			Field.Index.TOKENIZED, Field.TermVector.WITH_POSITIONS_OFFSETS);
	doc.add(fd);
	writer.addDocument(doc);
	writer.optimize();
	writer.close();

	IndexReader reader = IndexReader.open(ramDir);
	String queryString = QUERY;
	QueryParser parser = new QueryParser(FIELD_NAME, analyzer);
	Query query = parser.parse(queryString);
	Searcher searcher = new IndexSearcher(ramDir);
	query = query.rewrite(reader);
	System.out.println("Searching for: " + query.toString(FIELD_NAME));
	Hits hits = searcher.search(query);

	BoldFormatter formatter = new BoldFormatter();
	Highlighter highlighter = new Highlighter(formatter, new QueryScorer(
			query));
	highlighter.setTextFragmenter(new SimpleFragmenter(50));
	for (int i = 0; i < hits.length(); i++) {
		String text = hits.doc(i).get(FIELD_NAME);
		int maxNumFragmentsRequired = 5;
		String fragmentSeparator = "...";
		TermPositionVector tpv = (TermPositionVector) reader
				.getTermFreqVector(hits.id(i), FIELD_NAME);
		TokenStream tokenStream = TokenSources.getTokenStream(tpv);
		String result = highlighter.getBestFragments(tokenStream, text,
				maxNumFragmentsRequired, fragmentSeparator);
		System.out.println("\n" + result);
	}
	reader.close();
}
 
開發者ID:no8899,項目名稱:paoding-for-lucene-2.4,代碼行數:48,代碼來源:Chinese.java

示例8: main

import org.apache.lucene.search.highlight.Highlighter; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {

        if (args.length != 1) {
            System.err.println("Usage: HighlightIt <filename-out>");
            System.exit(-1);
        }

        String filename = args[0];

        String searchText = "term"; //
        QueryParser parser = new QueryParser(Version.LUCENE_41, //
                "f", //
                new StandardAnalyzer(Version.LUCENE_41));// #1
        Query query = parser.parse(searchText); //

        SimpleHTMLFormatter formatter = //
        new SimpleHTMLFormatter("<span class=\"highlight\">", //
                "</span>"); //

        TokenStream tokens = new StandardAnalyzer(Version.LUCENE_41) //
                .tokenStream("f", new StringReader(text)); //

        QueryScorer scorer = new QueryScorer(query, "f"); //

        Highlighter highlighter = new Highlighter(formatter, scorer); //
        highlighter.setTextFragmenter( //
                new SimpleSpanFragmenter(scorer)); //

        String result = //
        highlighter.getBestFragments(tokens, text, 3, "..."); //

        FileWriter writer = new FileWriter(filename); //
        writer.write("<html>"); //
        writer.write("<style>\n" + //
                ".highlight {\n" + //
                " background: yellow;\n" + //
                "}\n" + //
                "</style>"); //
        writer.write("<body>"); //
        writer.write(result); //
        writer.write("</body></html>"); //
        writer.close(); //
    }
 
開發者ID:xuzhikethinker,項目名稱:t4f-data,代碼行數:44,代碼來源:HighlightIt.java


注:本文中的org.apache.lucene.search.highlight.Highlighter.getBestFragments方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。