1

同様の質問がここで尋ねられます: Finding the position of search hits from Lucene 私の問題は、リンクが 3 年前のものであり、ほとんどのメソッドが推奨されていないか、Lucene 4.4 または 4.5 には存在しないことですexplain()。そうではない(または私が見ることができない)explain()メソッドから得られるものは次のとおりです。ポジションに関しては何も見えません:

0.40144306 = (MATCH) sum of:
  0.13381435 = (MATCH) weight(contents:inb344 in 52) [DefaultSimilarity], result of:
    0.13381435 = score(doc=52,freq=1.0 = termFreq=1.0
), product of:
      0.4472136 = queryWeight, product of:
        9.574976 = idf(docFreq=44, maxDocs=238384)
        0.046706498 = queryNorm
      0.299218 = fieldWeight in 52, product of:
        1.0 = tf(freq=1.0), with freq of:
          1.0 = termFreq=1.0
        9.574976 = idf(docFreq=44, maxDocs=238384)
        0.03125 = fieldNorm(doc=52)
  0.2676287 = (MATCH) weight(contents:inb344^2.0 in 52) [DefaultSimilarity], result of:
    0.2676287 = score(doc=52,freq=1.0 = termFreq=1.0
), product of:
      0.8944272 = queryWeight, product of:
        2.0 = boost
        9.574976 = idf(docFreq=44, maxDocs=238384)
        0.046706498 = queryNorm
      0.299218 = fieldWeight in 52, product of:
        1.0 = tf(freq=1.0), with freq of:
          1.0 = termFreq=1.0
        9.574976 = idf(docFreq=44, maxDocs=238384)
        0.03125 = fieldNorm(doc=52)

特定のドキュメントのクエリの既存の位置 (おそらく開始位置と終了位置) を確認する方法はありますか?

4

1 に答える 1

3

ここで私の答えを見つけました:

http://www.computergodzilla.blogspot.com/2013/07/how-to-use-lucene-highlighter.html

基本的にバージョン 4.2 を使用しており、完全に正常に動作します。

コードは次のとおりです(念のため):

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.computergodzilla.highlighter;

import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.InvalidTokenOffsetsException;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.search.highlight.TextFragment;
import org.apache.lucene.search.highlight.TokenSources;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

/**
 * Example of Lucene Highlighter
 * @author Mubin Shrestha
 */
public class LuceneHighlighter {

    public void highLighter() throws IOException, ParseException, InvalidTokenOffsetsException {
        IndexReader reader = DirectoryReader.open(FSDirectory.open(new File("D:/INDEXDIRECTORY")));
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_42);
        IndexSearcher searcher = new IndexSearcher(reader);
        QueryParser parser = new QueryParser(Version.LUCENE_42, "ncontent", analyzer);
        Query query = parser.parse("going");
        TopDocs hits = searcher.search(query, reader.maxDoc());
        System.out.println(hits.totalHits);
        SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter();
        Highlighter highlighter = new Highlighter(htmlFormatter, new QueryScorer(query));
        for (int i = 0; i < reader.maxDoc(); i++) {
            int id = hits.scoreDocs[i].doc;
            Document doc = searcher.doc(id);
            String text = doc.get("ncontent");
            TokenStream tokenStream = TokenSources.getAnyTokenStream(searcher.getIndexReader(), id, "ncontent", analyzer);
            TextFragment[] frag = highlighter.getBestTextFragments(tokenStream, text, false, 4);
            for (int j = 0; j < frag.length; j++) {
                if ((frag[j] != null) && (frag[j].getScore() > 0)) {
                    System.out.println((frag[j].toString()));
                }
            }
            //Term vector
            text = doc.get("content");
            tokenStream = TokenSources.getAnyTokenStream(searcher.getIndexReader(), hits.scoreDocs[i].doc, "content", analyzer);
            frag = highlighter.getBestTextFragments(tokenStream, text, false, 4);
            for (int j = 0; j < frag.length; j++) {
                if ((frag[j] != null) && (frag[j].getScore() > 0)) {
                    System.out.println((frag[j].toString()));
                }
            }
        }
    }
}
于 2013-10-19T12:53:25.263 に答える