Lucene常用的字段类型lucene检索打分原理

embedded/2025/2/2 19:41:30/

在 Apache Lucene 中,Field 类是文档中存储数据的基础。不同类型的 Field 用于存储不同类型的数据(如文本、数字、二进制数据等)。以下是一些常用的 Field 类型及其底层存储结构:

  1. TextField

    • 用途:用于存储文本数据,并对其进行分词和索引。
    • 底层存储结构:文本数据会被分词器(Analyzer)处理,将文本分割成词项(terms)。每个词项会被存储在倒排索引(inverted index)中,映射到包含该词项的文档。
    • 示例
      import org.apache.lucene.document.Document;
      import org.apache.lucene.document.TextField;
      import org.apache.lucene.document.Field.Store;Document doc = new Document();
      doc.add(new TextField("fieldName", "This is a sample text.", Store.YES));

  2. StringField

    • 用途:用于存储不需要分词的字符串数据,如唯一标识符(ID)等。
    • 底层存储结构:字符串数据作为一个整体存储在倒排索引中,不会进行分词。
    • 示例
      import org.apache.lucene.document.Document;
      import org.apache.lucene.document.StringField;
      import org.apache.lucene.document.Field.Store;Document doc = new Document();
      doc.add(new StringField("fieldName", "unique_identifier", Store.YES));

  3. IntPoint、LongPoint、FloatPoint、DoublePoint

    • 用途:用于存储数值数据,并支持范围查询。
    • 底层存储结构:数值数据会被转换成字节数组,并按照分块(block)的方式存储,以支持高效的范围查询。
    • 示例
      import org.apache.lucene.document.Document;
      import org.apache.lucene.document.IntPoint;
      import org.apache.lucene.document.StoredField;Document doc = new Document();
      int value = 123;
      doc.add(new IntPoint("fieldName", value));
      doc.add(new StoredField("fieldName", value)); // 如果需要存储原始值

  4. StoredField

    • 用途:用于存储不需要索引的数据,仅用于检索时返回的字段
    • 底层存储结构:数据以原始字节的形式存储在存储字段(stored field)中,不会被索引。
    • 示例
      import org.apache.lucene.document.Document;
      import org.apache.lucene.document.StoredField;Document doc = new Document();
      doc.add(new StoredField("fieldName", "This is the stored content."));

  5. BinaryField

    • 用途:用于存储二进制数据。
    • 底层存储结构:二进制数据以原始字节的形式存储在存储字段中,不会被索引。
    • 示例

      import org.apache.lucene.document.Document;
      import org.apache.lucene.document.StoredField;
      import org.apache.lucene.util.BytesRef;Document doc = new Document();
      byte[] byteArray = new byte[] {1, 2, 3, 4, 5};
      doc.add(new StoredField("fieldName", new BytesRef(byteArray)));

  6. SortedDocValuesField 和 NumericDocValuesField

    • 用途:用于存储排序和打分时需要的字段值。
    • 底层存储结构:数据以紧凑的格式存储在文档值(doc values)中,支持高效的排序和打分计算。
    • 示例
      import org.apache.lucene.document.Document;
      import org.apache.lucene.document.SortedDocValuesField;
      import org.apache.lucene.document.NumericDocValuesField;
      import org.apache.lucene.util.BytesRef;Document doc = new Document();
      doc.add(new SortedDocValuesField("fieldName", new BytesRef("sortable value")));
      doc.add(new NumericDocValuesField("numericField", 12345L));
      

lucene检索打分原理

在 Apache Lucene 中,"打分"(Scoring)是指在搜索过程中,根据文档与查询的匹配程度,为每个文档分配一个相关性分数(relevance score)。这个分数反映了文档与查询的相关性,分数越高,表示文档越相关。打分用于确定搜索结果的排序,即哪些文档应该排在前面展示给用户。

打分的基本概念

  1. 相关性分数

    • 每个文档在搜索结果中都会有一个相关性分数,数值越高,表示文档越符合查询条件。
    • 相关性分数是一个浮点数,通常在 0 到 1 之间,但也可以大于 1。
  2. TF-IDF 模型

    • Lucene 使用 TF-IDF(Term Frequency-Inverse Document Frequency)模型来计算相关性分数。
    • TF(词频):在一个文档中某个词的出现频率。词频越高,表示该词对文档的重要性越大。
    • IDF(逆文档频率):某个词在所有文档中出现的频率。文档频率越低,表示该词对区分文档的重要性越大。
  3. BM25 算法

    • BM25 是 Lucene 默认的打分算法,是 TF-IDF 的进化版本,能够更好地处理长查询和长文档。
    • BM25 考虑了词频、逆文档频率、文档长度等因素。

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;public class LuceneScoringExample {public static void main(String[] args) throws Exception {// 创建分析器StandardAnalyzer analyzer = new StandardAnalyzer();// 创建索引Directory index = new RAMDirectory();IndexWriterConfig config = new IndexWriterConfig(analyzer);IndexWriter writer = new IndexWriter(index, config);// 添加文档addDoc(writer, "Lucene in Action", "193398817");addDoc(writer, "Lucene for Dummies", "55320055Z");addDoc(writer, "Managing Gigabytes", "55063554A");addDoc(writer, "The Art of Computer Science", "9900333X");writer.close();// 创建查询String querystr = "Lucene";// 解析查询Query query = new QueryParser("title", analyzer).parse(querystr);// 搜索int hitsPerPage = 10;IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(index));TopDocs docs = searcher.search(query, hitsPerPage);ScoreDoc[] hits = docs.scoreDocs;// 显示结果System.out.println("Found " + hits.length + " hits.");for (int i = 0; i < hits.length; ++i) {int docId = hits[i].doc;Document d = searcher.doc(docId);System.out.println((i + 1) + ". " + d.get("isbn") + "\t" + d.get("title") + "\t" + hits[i].score);}}private static void addDoc(IndexWriter w, String title, String isbn) throws Exception {Document doc = new Document();doc.add(new TextField("title", title, Field.Store.YES));doc.add(new StringField("isbn", isbn, Field.Store.YES));w.addDocument(doc);}
}

 

在 Apache Lucene 中,打分(scoring)是一个动态计算的过程,相关性分数并不是预先存储在索引中的,而是根据查询和文档在搜索时实时计算的。因此,打分的值是临时的,不会永久存储在索引中。

  1. 动态计算

    • 当你执行一个查询时,Lucene 会根据查询条件和文档内容,动态计算每个匹配文档的相关性分数。
    • 这个计算过程基于查询的类型、词频(TF)、逆文档频率(IDF)、文档长度等因素。
  2. 不存储在索引中

    • 相关性分数并不会被存储在索引中。存储在索引中的信息包括倒排索引、词项频率、文档值等。
    • 每次执行查询时,Lucene 都会重新计算相关性分数,这确保了分数总是根据最新的查询条件和文档内容而更新。

http://www.ppmy.cn/embedded/158990.html

相关文章

活动回顾和预告|微软开发者社区 Code Without Barriers 上海站首场活动成功举办!

Code Without Barriers 上海活动回顾 Code Without Barriers&#xff1a;AI & DATA 深入探索人工智能与数据如何变革行业 2025年1月16日&#xff0c;微软开发者社区 Code Without Barriers &#xff08;CWB&#xff09;携手 She Rewires 她原力在大中华区的首场活动“AI &…

ChatGPT 搜索测试整合记忆功能

据 TestingCatalog 报道&#xff0c;OpenAI 正在测试 ChatGPT 搜索的整合记忆功能&#xff0c;被命名为 “Memory in search”2。以下是关于该功能的具体情况123&#xff1a; 功能特点 个性化搜索&#xff1a;启用该功能后&#xff0c;ChatGPT 能利用存储的记忆数据&#xff0…

【自然语言处理(NLP)】深度学习架构:Transformer 原理及代码实现

文章目录 介绍Transformer核心组件架构图编码器&#xff08;Encoder&#xff09;解码器&#xff08;Decoder&#xff09; 优点应用代码实现导包基于位置的前馈网络残差连接后进行层规范化编码器 Block编码器解码器 Block解码器训练预测 个人主页&#xff1a;道友老李 欢迎加入社…

解锁维特比算法:探寻复杂系统的最优解密码

引言 在复杂的技术世界中&#xff0c;维特比算法以其独特的魅力和广泛的应用&#xff0c;成为通信、自然语言处理、生物信息学等领域的关键技术。今天&#xff0c;让我们一同深入探索维特比算法的奥秘。 一、维特比算法的诞生背景 维特比算法由安德鲁・维特比在 1967 年提出…

[LeetCode]day10 707.设计链表

707. 设计链表 - 力扣&#xff08;LeetCode&#xff09; 题目描述 你可以选择使用单链表或者双链表&#xff0c;设计并实现自己的链表。 单链表中的节点应该具备两个属性&#xff1a;val 和 next 。val 是当前节点的值&#xff0c;next 是指向下一个节点的指针/引用。 如果…

如何用大语言模型做一个Html+CSS+JS的词卡网站

一、引言 词汇是语言学习的核心&#xff0c;如何有效地帮助学生记忆并使用词汇是英语教学中的一个重要课题。大语言模型精通各类编程语言&#xff0c;能够为开发各类小项目提供帮助。为了辅助外语教学中的词汇学习&#xff0c;我借助大语言模型开发有声词卡网站&#xff0c;网…

低代码开发中的开源与闭源之争

在低代码开发的迅猛发展浪潮下&#xff0c;开源与闭源两种模式逐渐成为行业焦点&#xff0c;引发了激烈的讨论和争议。这两种模式各有千秋&#xff0c;也各自面临着不同的挑战&#xff0c;对于开发者和企业来说&#xff0c;如何抉择至关重要。 开源低代码平台&#xff1a;开放共…

基于vscode的cppcmake调试环境配置

1. 创建项目文件 创建cpp文件及CMakeLists.txt文件 helloOpenCV.cpp #include <opencv2/opencv.hpp> int main() {// 创建图像&#xff0c;初始化为黑色cv::Mat image cv::Mat::zeros(200, 300, CV_8UC3);// 设置为纯绿色 (BGR格式&#xff1a;0, 255, 0)image.setTo…