package com.lucene.search;
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.queryParser.MultiFieldQueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MultiSearcher;
import org.apache.lucene.search.Query;
public class Multisearcher {
private static String INDEX_STORE_PATH1 = "C:\\multi\\1";
private static String INDEX_STORE_PATH2 = "C:\\multi\\2";
public static void main(String[] args) throws Exception {
Multisearcher.multisearcher();
}
public static void multisearcher() throws Exception {
IndexWriter writer = new IndexWriter(INDEX_STORE_PATH1, new StandardAnalyzer(), true);
writer.setUseCompoundFile(false);
Document doc1 = new Document();
Field f1 = new Field("bookname", "钢铁是怎样炼成的", Field.Store.YES, Field.Index.TOKENIZED);
Field f11 = new Field("price", "20.5", Field.Store.YES, Field.Index.UN_TOKENIZED);
doc1.add(f1);
doc1.add(f11);
Document doc2 = new Document();
Field f2 = new Field("bookname", "钢铁战士", Field.Store.YES, Field.Index.TOKENIZED);
Field f22 = new Field("price", "18.4", Field.Store.YES, Field.Index.UN_TOKENIZED);
doc2.add(f2);
doc2.add(f22);
Document doc3 = new Document();
Field f3 = new Field("bookname", "钢和铁是两种不同的元素", Field.Store.YES, Field.Index.TOKENIZED);
Field f33 = new Field("price", "7.6", Field.Store.YES, Field.Index.UN_TOKENIZED);
doc3.add(f3);
doc3.add(f33);
writer.addDocument(doc1);
writer.addDocument(doc2);
writer.addDocument(doc3);
writer.close();
//创建第二个索引器;
IndexWriter writer2 = new IndexWriter(INDEX_STORE_PATH2, new StandardAnalyzer(), true);
writer2.setUseCompoundFile(false);
Document doc4 = new Document();
Field f4 = new Field("bookname", "钢要比铁有更多的元素", Field.Store.YES, Field.Index.TOKENIZED);
Field f44 = new Field("price", "22.5", Field.Store.YES, Field.Index.UN_TOKENIZED);
doc4.add(f4);
doc4.add(f44);
Document doc5 = new Document();
Field f5 = new Field("bookname", "钢和铁是两种重要的金属", Field.Store.YES, Field.Index.TOKENIZED);
Field f55 = new Field("price", "15.9", Field.Store.YES, Field.Index.UN_TOKENIZED);
doc5.add(f5);
doc5.add(f55);
Document doc6 = new Document();
Field f6 = new Field("bookname", "钢铁是两种重要的金属", Field.Store.YES, Field.Index.TOKENIZED);
Field f66 = new Field("price", "19.00", Field.Store.YES, Field.Index.UN_TOKENIZED);
doc6.add(f6);
doc6.add(f66);
writer2.addDocument(doc4);
writer2.addDocument(doc5);
writer2.addDocument(doc6);
writer2.close();
String query1 = "钢";
String query2 = "[10 TO 20]";//注意格式:中括号还有关键字TO是大写的
String[] queries = { query1, query2 };
//指定两个域Field
String field1 = "bookname";
String field2 = "price";
String[] fields = { field1, field2 };
//指定查询字句之间的关系
BooleanClause.Occur[] clauses = { BooleanClause.Occur.MUST, BooleanClause.Occur.MUST };
//转成多域查询MultiFieldQuery
Query q = MultiFieldQueryParser.parse(queries, fields, clauses, new StandardAnalyzer());
//打印Query的内容
System.out.println(q.toString());
//创建两个IndexSearcher,以实现在多个索引目录进行查询
IndexSearcher searcher1 = new IndexSearcher(INDEX_STORE_PATH1);
IndexSearcher searcher2 = new IndexSearcher(INDEX_STORE_PATH2);
IndexSearcher[] searchers = { searcher1, searcher2 };
//使用MultiSearcher进行多域搜索
MultiSearcher searcher = new MultiSearcher(searchers);
Hits hits = searcher.search(q);
for (int i = 0; i < hits.length(); i++) {
System.out.println(hits.doc(i));
}
}
}
ZT: http://blog.chinaunix.net/u2/79131/showart_1189409.html
分享到:
相关推荐
本教程主要探讨的是如何利用Lucene进行索引优化,特别是通过多线程和处理多个目录来提高索引创建效率。 首先,我们需要理解Lucene的索引原理。Lucene将文档分解为词项(tokens),并对每个词项创建倒排索引。倒排...
本文将深入探讨如何利用Lucene实现增量索引,这是一种在数据库或文件系统更新时仅对新数据或变化数据进行索引的技术,以降低资源消耗并保持搜索性能。 **1. Lucene基础知识** Lucene首先需要理解的是它的核心概念,...
本项目中的代码旨在展示如何利用Lucene对多个文件夹下的数据进行索引创建和查询操作。 首先,我们需要了解Lucene的基本概念。Lucene的核心思想是将文本数据转换为结构化的索引,以便于快速查找相关文档。这个过程...
在本文中,我们将深入探讨如何基于Lucene 2.4版本创建简单的全文索引并进行搜索操作。 一、Lucene基本概念 1. 文档(Document):在Lucene中,一个文档代表了要索引的信息源,它可以是网页、电子邮件、PDF文档等。...
在Lucene中,索引是由多个称为“段”的部分组成的,每个段都是不可变的。当你添加、删除或更新文档时,Lucene会在新的段中进行操作,原有的段保持不变。当`IndexWriter`执行段合并时,它会检查每个段的位向量,并跳...
标题中的“Lucene对本地文件多目录创建索引”指的是使用Apache Lucene库来构建一个搜索引擎,该搜索引擎能够索引本地计算机上的多个文件目录。Lucene是一个强大的全文搜索库,它允许开发者在Java应用程序中实现高级...
在本案例中,我们将讨论如何利用Lucene.NET对数据库中的数据进行索引和搜索。 ### 1. 数据库表结构 在建立索引前,我们需要了解数据库的表结构。例如,这里有一个名为`userblog`的博客表,包含以下字段: - `id`...
基于Lucene的索引与搜索技术,不仅涉及到数据的高效存储和检索,还包括了文本预处理、分词、相关性排序等多个环节。 第二章 搜索引擎的结构 2.1 系统概述 搜索引擎主要由三部分组成:网络爬虫(也称为网络机器人...
这个"Lucene.NET全文索引搜索Demo项目"是一个实际应用示例,展示了如何在.NET环境中使用Lucene.NET进行全文索引和搜索操作。 首先,我们要理解什么是全文索引。全文索引是一种特殊的数据库索引,它允许用户通过输入...
- **多字段索引**:一个文档可以有多个字段被索引,方便针对不同字段进行搜索。 - **模糊搜索**:支持通配符、前缀、短语等查询,如`new WildcardQuery(new Term("title", "Java*"))`。 - **评分和排序**:Lucene...
- **分布式处理**:将待索引的文档分割成多个子集,并分配给不同的计算节点进行并行处理。这样可以充分利用集群中多台机器的计算能力,大大缩短索引构建的时间。 - **合并索引**:各计算节点完成本地索引构建后,...
在实现上,Zoie利用了Lucene的Segment Commit API,每个Segment代表一个索引的快照,每次数据变更都会生成一个新的Segment。当需要进行查询时,Zoie会合并多个Segment,返回最新的结果。为了保证性能,Zoie还引入了...
Lucene(这里用到的是Lucene.net版本也成为DotLucene)是一个信息检索的函数库(Library),利用它你可以为你的应用加上索引和搜索的功能. Lucene的使用者不需要深入了解有关全文检索的知识,仅仅学会使用库中的一个类,...
6. **段(Segment)**:为了提高效率,Lucene将大型索引拆分为多个较小的段。每个段都包含一个完整的倒排索引,新添加的文档会被写入新的段,旧的段可以被合并以减少段的数量。 7. **字段(Field)**:文档由多个...
Lucene(这里用到的是Lucene.net版本也成为DotLucene)是一个信息检索的函数库(Library),利用它你可以为你的应用加上索引和搜索的功能. Lucene的使用者不需要深入了解有关全文检索的知识,仅仅学会使用库中的一个类,...
Zoie(Zero-Offset Indexing Engine)是一个基于Lucene的实时索引框架,它通过将索引分片并行处理,实现了在不影响搜索性能的同时,对新数据进行即时索引。Zoie的核心理念是将索引分为多个版本,每个版本对应一个...
通过这种技术,可以在多台机器上分布式地建立索引,并通过并行处理来提高索引速度,从而有效地缓解了因索引构建导致的搜索性能瓶颈。 分布式并行索引技术的应用能够显著提升索引的构建效率,因为索引任务被分散到多...
Lucene 删除 合并索引,可以指定几个索引文件合并成一个索引文件。自己写的,有很多不足之处请多指教
你可以添加多个Field对象到Document中,每个Field对应记录的一个属性。 5. **创建Field对象**:Field是文档中的一个字段,具有名称和值。例如,`Field.Store.YES`表示该字段会被存储,以便在查询结果中返回;`Field...
在IT领域,搜索引擎的开发与优化是一项关键技术,而Apache Lucene作为一款高性能、全文本搜索库,是许多开发者进行文本检索的首选工具。本文将深入探讨如何使用Lucene5来创建一个基本的索引,帮助初学者入门。 首先...