`

lucene3.6.0的文档评估机制

 
阅读更多

lucene的评分机制:所有hits的分数<=1.0

每个document(d)的分数:

∑tf(t in d)*idf(t)*boost(t.field in d)*lengthNorm(t.field in d)

t In q

 

查询的得分:

score(q,d)=coord(q,d)·queryNorm(q)·∑tf(t in d)*idf(t)*boost(t.field in d)*lengthNorm(t.field in d)

t In q

 

tf(t in d):文档中d出现搜索项t的频率

idf(t):搜索项t在倒排文档中出现的频率

boost(t.field in d):域的加权因子,在插入文档中设置

lengthNorm(t.field in d):域的标准化值,即在某一域中所有项的个数。通常在索引时计算该值并存储到索引文件中。

coord(q,d):协调因子(normalization value),该因子的值基于文档中包含查询项的个数

queryNorm(q):每个查询的标准化值,指每个查询项的权重的平方和

 

 

 

query对象的加权因子,查询时如果是多个子句,则可以通过加权某一个查询子句来加权某一个query对象。

 

DefaultSimilarity.java默认处理计分规则/** Expert: Default scoring implementation. */

public class DefaultSimilarity extends Similarity {

  /** Implemented as
   *  <code>state.getBoost()*lengthNorm(numTerms)</code>, where
   *  <code>numTerms</code> is {@link FieldInvertState#getLength()} if {@link
   *  #setDiscountOverlaps} is false, else it's {@link
   *  FieldInvertState#getLength()} - {@link
   *  FieldInvertState#getNumOverlap()}.
   *
   *  @lucene.experimental */
  @Override
  public float computeNorm(String field, FieldInvertState state) {
    final int numTerms;
    if (discountOverlaps)
      numTerms = state.getLength() - state.getNumOverlap();
    else
      numTerms = state.getLength();
    return state.getBoost() * ((float) (1.0 / Math.sqrt(numTerms)));
  }
  
  /** Implemented as <code>1/sqrt(sumOfSquaredWeights)</code>. */
  @Override
  public float queryNorm(float sumOfSquaredWeights) {
    return (float)(1.0 / Math.sqrt(sumOfSquaredWeights));
  }

  /** Implemented as <code>sqrt(freq)</code>. */
  @Override
  public float tf(float freq) {
    return (float)Math.sqrt(freq);
  }
    
  /** Implemented as <code>1 / (distance + 1)</code>. */
  @Override
  public float sloppyFreq(int distance) {
    return 1.0f / (distance + 1);
  }
    
  /** Implemented as <code>log(numDocs/(docFreq+1)) + 1</code>. */
  @Override
  public float idf(int docFreq, int numDocs) {
    return (float)(Math.log(numDocs/(double)(docFreq+1)) + 1.0);
  }
    
  /** Implemented as <code>overlap / maxOverlap</code>. */
  @Override
  public float coord(int overlap, int maxOverlap) {
    return overlap / (float)maxOverlap;
  }

  // Default true
  protected boolean discountOverlaps = true;

  /** Determines whether overlap tokens (Tokens with
   *  0 position increment) are ignored when computing
   *  norm.  By default this is true, meaning overlap
   *  tokens do not count when computing norms.
   *
   *  @lucene.experimental
   *
   *  @see #computeNorm
   */
  public void setDiscountOverlaps(boolean v) {
    discountOverlaps = v;
  }

  /** @see #setDiscountOverlaps */
  public boolean getDiscountOverlaps() {
    return discountOverlaps;
  }
}
 

 

IndexSearcher.java的explain方法返回的Explanation对象包含了所有评分因子中各个因子的详细信息。

测试程序和数据参考http://zhwj184.iteye.com/admin/blogs/1522709

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class DocSearch {

	private static IndexSearcher isearcher = null;
	public static void search(String key) throws IOException, ParseException{
		 Directory directory = FSDirectory.open(new File("E:\\output\\lucence\\index"));
		 // Now search the index:
	    IndexReader ireader = IndexReader.open(directory); // read-only=true
	    isearcher  = new IndexSearcher(ireader);
	    // Parse a simple query that searches for "text":
	    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
	    QueryParser parser = new QueryParser(Version.LUCENE_CURRENT,"context", analyzer);
	    Query query = parser.parse(key);
	    ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;
	    // Iterate through the results:
	    for (int i = 0; i < hits.length; i++) {
	      Document hitDoc = isearcher.doc(hits[i].doc);
	      System.out.println(hitDoc.getValues("id")[0] + "\t" + hitDoc.getValues("context")[0] + "\t" + hits[i].score);
	      Explanation explan = isearcher.explain(query, hits[i].doc);
	      System.out.println(explan);
	    }
	}
	
	public static void main(String[] args) throws IOException, ParseException {
		search("旧水泥袋");
		isearcher.close();
	}
	
}
 

输出结果:

只截取第一篇文档的评分信息

4801857        采购旧编织袋、旧水泥袋        4.0172114
4.0172114 = (MATCH) sum of:
  1.4140004 = (MATCH) weight(context:旧 in 1682), product of:
    0.54585564 = queryWeight(context:旧), product of:
      5.861472 = idf(docFreq=13, maxDocs=1809)
      0.09312603 = queryNorm
    2.5904293 = (MATCH) fieldWeight(context:旧 in 1682), product of:
      1.4142135 = tf(termFreq(context:旧)=2)
      5.861472 = idf(docFreq=13, maxDocs=1809)
      0.3125 = fieldNorm(field=context, doc=1682)
  0.60229266 = (MATCH) weight(context:水 in 1682), product of:
    0.42365694 = queryWeight(context:水), product of:
      4.549286 = idf(docFreq=51, maxDocs=1809)
      0.09312603 = queryNorm
    1.4216518 = (MATCH) fieldWeight(context:水 in 1682), product of:
      1.0 = tf(termFreq(context:水)=1)
      4.549286 = idf(docFreq=51, maxDocs=1809)
      0.3125 = fieldNorm(field=context, doc=1682)
  1.1562659 = (MATCH) weight(context:泥 in 1682), product of:
    0.58700174 = queryWeight(context:泥), product of:
      6.3033047 = idf(docFreq=8, maxDocs=1809)
      0.09312603 = queryNorm
    1.9697827 = (MATCH) fieldWeight(context:泥 in 1682), product of:
      1.0 = tf(termFreq(context:泥)=1)
      6.3033047 = idf(docFreq=8, maxDocs=1809)
      0.3125 = fieldNorm(field=context, doc=1682)
  0.84465253 = (MATCH) weight(context:袋 in 1682), product of:
    0.42188305 = queryWeight(context:袋), product of:
      4.5302377 = idf(docFreq=52, maxDocs=1809)
      0.09312603 = queryNorm
    2.0021012 = (MATCH) fieldWeight(context:袋 in 1682), product of:
      1.4142135 = tf(termFreq(context:袋)=2)
      4.5302377 = idf(docFreq=52, maxDocs=1809)
      0.3125 = fieldNorm(field=context, doc=1682)
 

 

分享到:
评论

相关推荐

    lucene-3.6.0

    6. **多线程支持**:Lucene 3.6.0支持多线程索引和查询,通过适当的同步机制保证了并发环境下的正确性。 7. **JAR文件**:Lucene 3.6.0的发布包含了一系列JAR文件,如core库、示例程序、测试工具等,方便开发者直接...

    lucene-3.6.0.zip

    6. 更新管理:优化了索引更新机制,支持实时索引,允许在不关闭索引的情况下进行文档的添加、删除和修改。 7. API改进:提供更加稳定和易于使用的API,简化了开发者的工作流程。 三、Lucene的应用场景 Lucene广泛...

    lucene 3.6.0 源代码

    lucene-core-3.6.0-sources 绝对可用

    lucene-3.6.0 api 手册

    lucene-3.6.0 api 手册, 最新的 , lucene 是个好东东, 一直在用, 之前还在使用3.1的,发现已经到3.6了, 落后啊

    lucene-core-7.2.1-API文档-中文版.zip

    包含翻译后的API文档:lucene-core-7.2.1-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache.lucene:lucene-core:7.2.1; 标签:apache、lucene、core、中文文档、jar包、java; 使用方法:解压翻译后的API...

    Lucene技术文档doc

    **Lucene技术文档doc** **一、Lucene简介** Lucene是Apache软件基金会下的Jakarta项目组的一个核心项目,它是一款高性能、可扩展的全文检索引擎库。作为一个开源的Java库,Lucene提供了完整的搜索功能,包括索引、...

    lucene-core-3.6.0.jar

    lucene-core-3.6.0.jar,很好,很实用的一个包

    lucene文档,lucene相关文档

    3. 相关性(Relevance):Lucene使用TF-IDF(词频-逆文档频率)算法计算文档与查询的相关性,确定搜索结果的排名。 四、扩展与优化 1. 分布式搜索(Solr):Apache Solr基于Lucene,提供分布式、集群化搜索解决...

    lucene-4.6.0官方文档

    搜索则是通过查询解析和评分机制来定位匹配的文档,而结果排序则依据相关性来确定返回给用户的顺序。 2. **API详解** 在Lucene 4.6.0中,主要API包括Analyzer、IndexWriter、Directory、QueryParser、Searcher等。...

    Lucene-2.0学习文档

    本篇文章将围绕"Lucene-2.0学习文档"的主题,结合Indexer.java、MyScoreDocComparator.java和MySortComparatorSource.java这三个关键文件,深入探讨Lucene的核心概念和实际应用。 首先,我们来看`Indexer.java`。这...

    lucene帮助文档.chm

    《Lucene帮助文档详解》 Lucene是一款强大的全文搜索引擎库,由Apache软件基金会开发并维护。这个名为“lucene帮助文档.chm”的压缩包文件,包含了一份详尽的Lucene使用指南,通常以CHM(Microsoft Compiled ...

    lucene-core-7.7.0-API文档-中文版.zip

    包含翻译后的API文档:lucene-core-7.7.0-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache.lucene:lucene-core:7.7.0; 标签:apache、lucene、core、中文文档、jar包、java; 使用方法:解压翻译后的API...

    weblucene安装文档

    在本安装文档中,我们将详细介绍如何一步步地安装并配置WebLucene,以便于你能够快速地将它集成到你的项目中。 首先,我们需要了解安装WebLucene所需的环境。WebLucene基于Java,因此确保你的系统已经安装了Java ...

    lucene检索文档、检索大数据量数据

    Lucene提供了缓存机制,如字段缓存(FieldCache)和术语缓存(Term Dictionary Cache),以减少磁盘I/O,提升检索速度。另外,通过设置合适的相关度评分算法(如TF-IDF)和使用过滤器(Filter)可以进一步优化搜索...

    利用lucene实现文档关键字检索

    在这个使用案例中,我们将深入探讨如何利用Lucene实现对Word文档中的关键字检索并高亮显示结果。 首先,我们需要理解Lucene的基本工作原理。Lucene通过建立倒排索引(Inverted Index)来加速查询。倒排索引是一种...

    lucene.jar包,四包(全)

    - **lucene-core-3.6.0-javadoc.jar**:这个文件包含了Lucene 3.6.0核心组件的API文档,是开发者理解和使用Lucene的重要参考资料。它详细解释了每个类、接口和方法的功能和用法,便于查阅和学习。 - **lucene-test-...

    lucene整理文档,lucene详细描述,安装使用过程。

    Lucene 提供了索引和搜索文本的基本工具,包括分词、建立倒排索引、查询解析和评分机制等。 **安装与配置** 安装 Lucene 非常简单,只需要将对应的 JAR 包添加到项目依赖中。在 Maven 项目中,可以在 pom.xml 文件...

    lucene电子文档搜索

    **Lucene电子文档搜索** Lucene是一个开源的全文搜索引擎库,由Apache软件基金会开发并维护。它提供了高级的文本分析和索引功能,使得开发者能够快速地在大量数据中实现高效的搜索功能。Lucene.Net是Lucene的一个...

    非常详细的Lucene文档

    6. **缓存(Caching)**: 为了提高搜索性能,Lucene 引入了多种缓存机制,如 DocValues 缓存、Filter 缓存等。 **应用场景** Lucene 应用于各种需要全文搜索的场合,如内容管理系统、知识库、论坛、电子商务网站、...

Global site tag (gtag.js) - Google Analytics