Lucene 8.5.2核心API

Lucene 8.5.2核心API

Apache Lucene是一个高性能的全功能文本搜索引擎库。

另请:说明

配套
描述
org.apache.lucene
顶级程序包。
org.apache.lucene.analysis
文字分析。
org.apache.lucene.analysis.standard
快速,通用的基于语法的令牌生成器根据Unicode标准附件#29中StandardTokenizer 指定的Unicode文本分段算法实现分词规则 。
org.apache.lucene.analysis.token属性
文本分析的通用属性。
org.apache.lucene.codecs
编解码器API:用于自定义索引编码和结构的API。
org.apache.lucene.codecs.blocktree
BlockTree术语词典。
org.apache.lucene.codecs.compressing
StoredFieldsFormat,它允许对存储的字段进行跨文档和跨字段的压缩。
org.apache.lucene.codecs.lucene50
Lucene 5.0索引格式的组件有关索引格式org.apache.lucene.codecs.lucene80的概述,请参见。
org.apache.lucene.codecs.lucene60
Lucene 6.0索引格式的组件。
org.apache.lucene.codecs.lucene70
Lucene 7.0索引格式的组件。
org.apache.lucene.codecs.lucene80
Lucene 8.0索引格式的组件有关索引格式org.apache.lucene.codecs.lucene84的概述,请参见。
org.apache.lucene.codecs.lucene84
Lucene 8.4文件格式。
org.apache.lucene.codecs.perfield
可以将每个字段委派为不同格式的帖子格式。
org.apache.lucene.document
Document用于索引和搜索的逻辑表示。
org.apache.lucene.geo
Lucene Core的地理空间实用程序实现
org.apache.lucene.index
维护和访问索引的代码。
org.apache.lucene.search
代码以搜索索引。
org.apache.lucene.search.similarities
该软件包包含可在Lucene中使用的各种排名模型。
org.apache.lucene.search.spans
跨度的演算。
org.apache.lucene.store
二进制I / O API,用于所有索引数据。
org.apache.lucene.util
一些实用程序类。
org.apache.lucene.util.automaton
用于正则表达式的有限状态自动机。
org.apache.lucene.util.bkd
块KD树,实现在所描述的通用的空间数据结构 本文
org.apache.lucene.util.compress
压缩实用程序。
org.apache.lucene.util.fst
有限状态传感器
org.apache.lucene.util.graph
用于将令牌流作为图形使用的实用程序类。
org.apache.lucene.util.mutable
可比对象包装
org.apache.lucene.util.packed
压缩整数数组和流。

Apache Lucene是一个高性能的全功能文本搜索引擎库。这是一个简单的示例,说明如何使用Lucene进行索引和搜索(使用JUnit检查结果是否符合我们的预期):

    Analyzer analyzer = new StandardAnalyzer();

    Path indexPath = Files.createTempDirectory("tempIndex");
    Directory directory = FSDirectory.open(indexPath)
    IndexWriterConfig config = new IndexWriterConfig(analyzer);
    IndexWriter iwriter = new IndexWriter(directory, config);
    Document doc = new Document();
    String text = "This is the text to be indexed.";
    doc.add(new Field("fieldname", text, TextField.TYPE_STORED));
    iwriter.addDocument(doc);
    iwriter.close();
    
    // Now search the index:
    DirectoryReader ireader = DirectoryReader.open(directory);
    IndexSearcher isearcher = new IndexSearcher(ireader);
    // Parse a simple query that searches for "text":
    QueryParser parser = new QueryParser("fieldname", analyzer);
    Query query = parser.parse("text");
    ScoreDoc[] hits = isearcher.search(query, 10).scoreDocs;
    assertEquals(1, hits.length);
    // Iterate through the results:
    for (int i = 0; i < hits.length; i++) {
      Document hitDoc = isearcher.doc(hits[i].doc);
      assertEquals("This is the text to be indexed.", hitDoc.get("fieldname"));
    }
    ireader.close();
    directory.close();
    IOUtils.rm(indexPath);

Lucene API分为几个软件包:

要使用Lucene,应用程序应:
  1. Document通过添加来 创建Field
  2. 使用创建一个IndexWriter 并向其中添加文档addDocument()
  3. 调用QueryParser.parse() 从字符串中构建查询;和
  4. 创建一个IndexSearcher 并将查询传递给其search() 方法。
一些简单的代码示例如下: 为了演示这些,请尝试以下操作:
java -cp lucene-core.jar:lucene-demo.jar:lucene-analyzers-common.jar org.apache.lucene.demo.IndexFiles -index index -docs rec.food.recipes/soups
adding rec.food.recipes/soups/abalone-chowder
  [ ... ]

java -cp lucene-core.jar:lucene-demo.jar:lucene-queryparser.jar:lucene-analyzers-common.jar org.apache.lucene.demo.SearchFiles
Query: chowder
Searching for: chowder
34 total matching documents
1. rec.food.recipes/soups/spam-chowder
  [ ... thirty-four documents contain the word "chowder" ... ]

Query: "clam chowder" AND Manhattan
Searching for: +"clam chowder" +manhattan
2 total matching documents
1. rec.food.recipes/soups/clam-chowder
  [ ... two documents contain the phrase "clam chowder" and the word "manhattan" ... ]
    [ Note: "+" and "-" are canonical, but "AND", "OR" and "NOT" may be used. ]

猜你喜欢

转载自blog.csdn.net/qq_23853743/article/details/107038851