Lucene深入学习(5)Lucene的Document与Field

Document与Field

在Lucene中,document是一种逻辑文件。可以近似地认为它表示的是计算机中的一个文件。这个document是一种抽象的表示,它从各种维度来描述一个数据源中的每一条数据。
将一个Document与文件系统中的文件对应起来时,可以提取出很多数据单元,它们被一个称之为Field的类表示。
这里的Document-Field结构和关系型数据库结构类似,数据库的每一条记录可以表示为一个document,而每一列可以用Field表示:

Doc idFiled name1Field name2Field name3
document1filed1 valuefield2 valuefield3 value
document2field1 valuefield2 valuefield3 value

Lucene文档中表明,Document是索引和查询的最小单位。Document由一系列的Fields组成,每一个Field都有一个名字和值。每一个Document都应该至少有一个stored的filed,并且有一个主键id。

Document的实现逻辑

Document的内部实现相对简单,主要方法有如下几个:

 /** 为document添加field */
 public final void add(IndexableField field)

/** 删除一个field */
 public final void removeField(String field)

 /** 根据Field名称找出field, 如果多个Field名称一样,返回第一个 */
public final IndexableField getField(String name) 

/** 返回能读懂的document内容 */
public final Sting toString()

/** 把document中的所有field移除*/
public void clear()

可以看到,Document的主要方法都是在操作Field,还加入了toString()方便输出document中的内容。

## Field的实现逻辑
Field是document的一部分,每一个Field都有三部分组成:名称name,类型type,值value。目前大约有十多种功能各异的Fields: TextField,StringField,IntPoint,LongPoint,FloatPoint,DoublePoint,SortedDocValuesField,SortedSetValuesField,NumericaDocValuesField,SortedNumericDocValuesField,SotedField。

Field类中含有一个静态枚举类:

“`java

public static enum Store {

/* 存储该Field的原始值。表示在存储值之前不使用 Analyzer/
YES,
/* 不存储原始值于索引中 /
NO
}
“`
Filed类有众多的构造方法:

“`java

public Field(String name, Reader reader, FieldType type)

public Field(String name, TokenStream tokenStream, FieldType type)

public Field(String name, byte[] value, FieldType type)

public Field(String name, byte[] value, int offset, int length, FieldType type)

public Field(String name, BytesRef bytes, FieldType type)

public Field(String name, String value, FieldType type)
“`

这些方法基本也表明了一个 Field应该包含的三部分:名称,值,类型。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Lucene是一个开源的全文搜索引擎库,用于实现文本索引和搜索功能。下面是一个Lucene的使用示例: 1. 创建索引: ```java import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; // 创建索引 public class Indexer { public static void main(String[] args) { // 索引存储路径 String indexPath = "path_to_index_directory"; try { // 创建分词器 Analyzer analyzer = new StandardAnalyzer(); // 创建索引配置 IndexWriterConfig config = new IndexWriterConfig(analyzer); // 创建索引目录 Directory directory = FSDirectory.open(Paths.get(indexPath)); // 创建索引写入器 IndexWriter indexWriter = new IndexWriter(directory, config); // 创建文档 Document document = new Document(); document.add(new Field("content", "Hello world!", TextField.TYPE_STORED)); // 将文档添加到索引中 indexWriter.addDocument(document); // 提交索引 indexWriter.commit(); // 关闭索引写入器 indexWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 2. 搜索索引: ```java import org.apache.lucene.analysis.Analyzer; 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.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.FSDirectory; // 搜索索引 public class Searcher { public static void main(String[] args) { // 索引目录路径 String indexPath = "path_to_index_directory"; try { // 创建分词器 Analyzer analyzer = new StandardAnalyzer(); // 创建索引目录 Directory directory = FSDirectory.open(Paths.get(indexPath)); // 创建索引读取器 IndexReader indexReader = DirectoryReader.open(directory); // 创建索引搜索器 IndexSearcher indexSearcher = new IndexSearcher(indexReader); // 创建查询解析器 QueryParser parser = new QueryParser("content", analyzer); // 创建查询 Query query = parser.parse("Hello"); // 执行查询,获取前n个结果 TopDocs topDocs = indexSearcher.search(query, 10); ScoreDoc[] scoreDocs = topDocs.scoreDocs; // 遍历结果 for (ScoreDoc scoreDoc : scoreDocs) { int docId = scoreDoc.doc; Document document = indexSearcher.doc(docId); System.out.println("Content: " + document.get("content")); } // 关闭索引读取器 indexReader.close(); } catch (IOException | ParseException e) { e.printStackTrace(); } } } ``` 以上示例演示了如何使用Lucene创建索引并进行搜索。在创建索引时,需要定义分词器、索引配置、文档字段等。在搜索索引时,需要创建查询解析器、执行查询并获取结果。你可以根据自己的需求进行更多的定制和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cj96248

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值