image.png

Field属性

Lucene存储对象是以Document为存储单元,对象中相关的属性值则存放到Field中。
每个Field包含3部分信息:域的名称,域的类型,域的值。域的值可以是String,Java.io.Reader,TokenStream,可以是byte[]字节数组,可以是数字等等,而域的类型则是有IndexableFieldType类表示的。
Field是文档中的域,包括Field名和Field值两部分,一个文档可以包括多个Field,Document只是Field 的一个承载体,Field值即为要索引的内容,也是要搜索的内容。

Field的三大属性:
是否分析:是否对域的内容进行分词处理前提是我们要对域的内容进行查询

是否索引:将Field分析后的词或整个Field值进行索引,只有索引方可搜索到
比如:商品名称、商品简介分析后进行索引,订单号、身份证号不用分析但也要索引,这些将来都要作为查询条件。

是否存储:将Field值存储在文档中,存储在文档中的Field才可以从Document中获取
比如:商品名称、订单号,凡是将来要从Document中获取的Field都要存储

Field常用类型

Field对应的类是 org.apache.lucene.document.Field,该类实现了org.apache.lucene.document.IndexableField 接口,代表用于indexing的一个字段。

Field类比较底层一些,所以Lucene实现了许多Field子类,用于不同的场景。

Field类型 数据类型 分词 索引 存储 说明
StringField(FieldName,FieldValue,Store.YES) 字符
N Y Y/N 字符串类型Field,不分词,作为一个整体进行索引(如:身份证号,订单编号),是否需要存储由Store.YES或Store.NO决定
TextField(FieldName,FieldValue,Store.NO) 文本类型 Y Y Y/N 文本类型Field,分词并且索引,是否需要存储由Store.YES或Store.NO决定
LongField(FieldName,FieldValue,Store.YES)或LongPoint(Stringname,int…point)等 数值型代表 Y Y Y/N 在Lucene6.0中,LongField替换为LongPoint,IntField替换为IntPoint,FloatField替换为FloatPoint,DoubleField替换为DoublePoint。对数值型字段索引,索引不存储。要存储结合StoredField即可。
StoredField(FieldName,FieldValue) 支持
多种类型
N N Y 构建不同类型的Field,不分词,不索引,要存储.(如:商品图片路径)

Field应用代码

  1. import org.apache.lucene.analysis.Analyzer;
  2. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  3. import org.apache.lucene.document.*;
  4. import org.apache.lucene.index.*;
  5. import org.apache.lucene.search.*;
  6. import org.apache.lucene.search.similarities.ClassicSimilarity;
  7. import org.apache.lucene.search.Query;
  8. import org.apache.lucene.queryparser.classic.QueryParser;
  9. import org.apache.lucene.store.*;
  10. import org.junit.Test;
  11. import java.nio.file.Paths;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. @Test
  15. public void createIndex() throws Exception {
  16. // 1.采集数据
  17. Book booka = new Book();
  18. List<Book> bookList = new ArrayList<Book>();
  19. booka.setId(1);
  20. booka.setDesc("Lucene Core is a Java library providing powerful indexing and search features, as well as spellchecking, hit highlighting and advanced analysis/tokenization capabilities. The PyLucene sub project provides Python bindings for Lucene Core. ");
  21. booka.setName("Lucene");
  22. booka.setPrice(100.45f);
  23. bookList.add(booka);
  24. Book bookb = new Book();
  25. bookb.setId(11);
  26. bookb.setDesc("Solr is highly scalable, providing fully fault tolerant distributed indexing, search and analytics. It exposes Lucene's features through easy to use JSON/HTTP interfaces or native clients for Java and other languages. ");
  27. bookb.setName("Solr");
  28. bookb.setPrice(320.45f);
  29. bookList.add(bookb);
  30. Book bookc = new Book();
  31. bookc.setId(21);
  32. bookc.setDesc("The Apache Hadoop software library is a framework that allows for the distributed processing of large data sets across clusters of computers using simple programming models.");
  33. bookc.setName("Hadoop");
  34. bookc.setPrice(620.45f);
  35. bookList.add(bookc);
  36. // 2.将采集到的数据封装到Document对象中
  37. List<Document> docList = new ArrayList<>();
  38. Document document;
  39. for (Book book : bookList) {
  40. document = new Document();
  41. // IntPoint 分词 索引 不存储 存储结合 StoredField
  42. Field id = new IntPoint("id", book.getId());
  43. //看这个field是不是分词还有是不是存储
  44. System.out.println(id.fieldType().tokenized() + ":" + id.fieldType().stored());
  45. Field id_v = new StoredField("id", book.getId());
  46. // 分词、索引、存储 TextField
  47. Field name = new TextField("name", book.getName(), Field.Store.YES);
  48. // 分词、索引、不存储 但是是数字类型,所以使用FloatPoint
  49. Field price = new FloatPoint("price", book.getPrice());
  50. // 分词、索引、不存储 TextField
  51. Field desc = new TextField("desc", book.getDesc(), Field.Store.NO);
  52. // 将field域设置到Document对象中
  53. document.add(id);
  54. document.add(id_v);
  55. document.add(name);
  56. document.add(price);
  57. document.add(desc);
  58. docList.add(document);
  59. }
  60. //3.创建Analyzer 分词器 对文档进行分词
  61. Analyzer analyzer = new StandardAnalyzer();
  62. // 创建Directory 和 IndexWriterConfig 对象
  63. Directory directory = FSDirectory.open(Paths.get("/Users/xiajiandong/Desktop/luceneTest/index"));
  64. IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
  65. // 4.创建IndexWriter 写入对象
  66. IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
  67. // 添加文档对象
  68. for (Document doc : docList) {
  69. indexWriter.addDocument(doc);
  70. }
  71. // 释放资源
  72. indexWriter.close();
  73. }

上面的desc虽然没有存储,但是可以搜索
不存储不代表没索引, 但是没有desc
image.png