Lucene Highlighter

0 respostas
rafaelunp

Prezados,

Estou tendo problemas com o Highlighter do Lucene.
Preciso que o resultado da busca apresente quais trechos do arquivo foram localizados.
A busca já me retorna que determinados arquivos possuem o trecho que pesquiso.
Contudo ao usar o Highlighter, alguns trechos de busca não retornam (ou seja, ele acha o trecho no arquivo, mas nao exibe no Highlighter - retona null no Highlighter).

Alguem tem alguma sugestão?

Segue abaixo um exemplo do código utilizado.

public Collection<String> performSearch(String queryString) throws IOException, ParseException {

        Collection<String> retorno = new ArrayList<String>();

        //You can think of IndexSearcher
        //as a class that opens an index in a read-only mode.
        IndexSearcher searcher = new IndexSearcher(rootDirectory);

        QueryParser parser = new QueryParser(FieldTypeEnum.CONTENTS.getValue(), MeuAnalyzer.getInstance());
        Query query = parser.parse(queryString);

        // Collect enough docs to show 5 pages
        TopDocCollector collector = new TopDocCollector(MAX_TOP_COLLECTOR);
        searcher.search(query, collector);

        ScoreDoc[] scoreDocs = collector.topDocs().scoreDocs;

        int numTotalHits = collector.getTotalHits();
        log.debug("Conteudo encontrado em "+ numTotalHits + " documento(s).");


        for (ScoreDoc scoreDoc : scoreDocs) {
            Document doc = searcher.doc(scoreDoc.doc);
            log.debug("Localizado em: " + doc.get(FieldTypeEnum.PATH.getValue()) );
            log.debug("Score: "+scoreDoc.score);
            retorno.add( doc.get( FieldTypeEnum.PATH.getValue() ) );

            String conteudo = doc.get( FieldTypeEnum.CONTENTS.getValue() );

            TermQuery queryx = new TermQuery(new Term(FieldTypeEnum.CONTENTS.getValue(), queryString));
            Scorer scorer = new QueryScorer(queryx);
            Formatter formatter = new SimpleHTMLFormatter("<b>", "</b>");
            Highlighter highlighter = new Highlighter(formatter, scorer);
            Fragmenter fragmenter = new SimpleFragmenter();
            highlighter.setTextFragmenter(fragmenter);
            String[] result = highlighter.getBestFragments(MeuAnalyzer.getInstance(), conteudo, 5);
            for (String string : result) {
                System.out.println(string);
            }

        }

        return retorno;
    }
Criado 28 de setembro de 2009
Respostas 0
Participantes 1