当前位置: 代码迷 >> 综合 >> Lucene Scoring 评分机制 ( by quqi99 )
  详细解决方案

Lucene Scoring 评分机制 ( by quqi99 )

热度:57   发布时间:2023-12-13 09:50:12.0

                         Lucene评分机制( by quqi99 )


作者:张华 发表于:2011-01-24


版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明


 /**
     * 利用IndexReader获取下列信息,注意,建索引时需加Field.TermVector.YES:
     (1) 统计term在整个collection中的文档频度(document frequency, DF);
     (2) 统计term在整个collection中出现的词次(term frequency in whole collection);
     (3) 统计term在某个文档中出现的频度(term frequency, TF);
     (4) 列出term在某文档中出现的位置(position);
     (5) 整个collection中文档的个数;
     */
    public static void printIndex(IndexReader reader) throws Exception
    {
        // 显示有多少个document
        System.out.println(reader + "/tNumDocs = " + reader.numDocs());
        for (int i = 0; i < reader.numDocs(); i++)
        {
            System.out.println(reader.document(i));
        }

        // 枚举term,获得<document, term freq, position* >信息
        TermEnum termEnum = reader.terms();
        while (termEnum.next())
        {
            System.out.println(termEnum.term());
            System.out.println("/tDocFreq=" + termEnum.docFreq());

            TermPositions termPositions = reader.termPositions(termEnum.term());
            int i = 0;
            int j = 0;
            while (termPositions.next())
            {
                System.out.println((i++) + "->" + " DocNo:" + termPositions.doc() + ", Freq:" + termPositions.freq());
                for (j = 0; j < termPositions.freq(); j++)
                    System.out .println("[" + termPositions.nextPosition() + "]");
                System.out.println();
            }

            // 直接获取 <term freq, document> 的信息
            TermDocs termDocs = reader.termDocs(termEnum.term());
            while (termDocs.next())
            {
                System.out.println((i++) + "->" + " DocNo:" + termDocs.doc()  + ", Freq:" + termDocs.freq());
            }
        }

        //在reader是SegmentReader类型的情况下有效
        // FieldInfos fieldInfos = reader.fieldInfos;
        // FieldInfo pathFieldInfo = fieldInfos.FieldInfo("path");

        // 显示 term frequency vector
        for (int i = 0; i < reader.numDocs(); i++)
        {
            // 对contents的token之后的term存于了TermFreqVector
            TermFreqVector termFreqVector = reader.getTermFreqVector(i,"contents");
            if (termFreqVector == null)
            {
                System.out.println("termFreqVector is null.");
                continue;
            }
            String fieldName = termFreqVector.getField();
            String[] terms = termFreqVector.getTerms();
            int[] frequences = termFreqVector.getTermFrequencies();
            System.out.println("FieldName:" + fieldName);
            for (int j = 0; j < terms.length; j++)
            {
                System.out.println("[" + terms[j] + ":" + frequences[j] + "]");
            }
            System.out.println();
        }
        System.out.println();
    }

  相关解决方案