`
kevin2123
  • 浏览: 8187 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

lucene3.6多Field查询

    博客分类:
  • JAVA
 
阅读更多

创建索引文件

 

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;

public class IndexCreateForTable {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		File f = new File("F:\\lucene\\table");
		String url = "";
		FSDirectory fs =  null;
		try {
			fs = FSDirectory.open(f);
			IKAnalyzer ik = new IKAnalyzer();
			ik.setUseSmart(true);
			IndexWriterConfig ifg = new IndexWriterConfig(Version.LUCENE_36, ik);
			IndexWriter iw = new IndexWriter(fs, ifg);
			
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/articles", "root", "root") ;
			PreparedStatement ps = conn.prepareStatement("select s.pub_id,s.china_title,s.sponsor from bs_tb_publications s where s.status = '11A'") ;
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				Document doc = new Document();
				doc.add(new Field("id", rs.getString("pub_id"), Field.Store.YES, Field.Index.ANALYZED));
				doc.add(new Field("title", rs.getString("china_title"), Field.Store.YES, Field.Index.ANALYZED));
				doc.add(new Field("desc", rs.getString("sponsor")==null?"":rs.getString("sponsor"), Field.Store.YES, Field.Index.ANALYZED));
				iw.addDocument(doc);
			}
			iw.commit();
			iw.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

 搜索程序:

 

 

/**
	 * 多字段搜索
	 */
	public void test2(){
		FSDirectory fs;
		try {
			
			
			fs =  FSDirectory.open(new File("F:\\lucene\\table"));
			IndexReader ir = IndexReader.open(fs);
			long start  = System.currentTimeMillis();
			IndexSearcher search = new IndexSearcher(ir);
			String key = " 卫星 ";
			String fieds[] = new String[]{"title","desc"};
			IKAnalyzer ik = new IKAnalyzer();
			MultiFieldQueryParser m = new MultiFieldQueryParser(Version.LUCENE_36, fieds, ik);
			m.setDefaultOperator(Operator.AND);
			Query query = m.parse(key) ;
			ScoreDoc[] hits = search.search(query, null, 1000).scoreDocs; 
			
			System.out.println("共命中"+hits.length+"条记录");
			
			for(ScoreDoc scoreDoc:hits)
	        {
	            Document doc= search.doc(scoreDoc.doc);
	            System.out.println(scoreDoc.score + "\t id:"+doc.get("id")+"\ttitle:"+doc.get("title")+"\tdesc:"+doc.get("desc"));
	        }			
			
			System.out.println("执行时间:"+(System.currentTimeMillis()-start)+"毫秒");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
分享到:
评论

相关推荐

    Lucene 3.6 学习笔记

    - `Document`: 代表一个索引文档,包含多个字段(Field),每个字段有其特定的类型(如文本、数值或日期)。 #### 1.2 分词部分的核心类 - `Analyzer`: 分析器,负责将输入文本转换为Token流,包括分词、去停用词、...

    lucene 3.6 全文检索

    Lucene的核心组件包括索引(Index)、文档(Document)、字段(Field)和查询(Query)。索引是Lucene处理数据的主要方式,它将文本数据转换为高效的倒排索引结构,以便快速查找匹配的文档。文档是信息的基本单元,...

    lucene 3.6

    3. **Document 和 Field**:在 Lucene 中,信息被组织成 Document 对象,每个 Document 可包含多个 Field,Field 表示文档中的不同属性,如标题、内容等。每个 Field 都可以设置不同的存储和索引属性。 4. **...

    基于lucene3.6平台搜索工具相关包及使用说明

    在3.6版本中,Lucene提供了一套完整的搜索解决方案,包括索引构建、查询解析、结果排序等功能。本指南将深入探讨基于Lucene 3.6的搜索工具包及其使用方法。 一、Lucene 3.6核心组件 1. 分析器(Analyzer):负责对...

    lucene3.6的入门案例

    2. **文档(Document)**:在 Lucene 中,每个文档都是一个包含多个字段(Field)的对象,如标题、内容等。每个字段有其特定的属性,如是否被索引、是否可搜索等。 3. **分析器(Analyzer)**:分析器负责将输入...

    基于Lucene3.6进行全文检索的小案例

    2. **文档(Document)**:在Lucene中,每个文档是信息的最小单位,可以包含多个字段(Field),如标题、内容等。每个字段都有其特定的类型,如文本或数值,以决定其在索引中的处理方式。 3. **分析器(Analyzer)*...

    第一个Lucene 3.6 (3.X) 入门实例

    【标题】:“第一个Lucene 3.6 (3.X) 入门实例” 【内容详解】 Lucene是一个高性能、全文本搜索库,由Apache软件基金会开发。它为Java开发者提供了强大的文本检索功能,广泛应用于搜索引擎、信息检索系统等场景。...

    lucene3.6源码

    《深入剖析Lucene 3.6源码》 Lucene是一个高性能、全文检索库,它由Apache软件基金会开发并维护,被广泛应用于各种搜索引擎的构建。Lucene 3.6.1是其历史版本之一,虽然现在有更新的版本,但3.6.1版本因其稳定性及...

    lucene-3.6

    《Lucene 3.6 全文搜索引擎技术详解》 Lucene是一个开源的全文检索库,由Apache软件基金会开发并维护。在3.6版本中,Lucene为开发者提供了强大的文本搜索功能,使得在网站或应用程序中实现高效、精确的全文搜索成为...

    lucene_3.6.1_API

    总的来说,Lucene 3.6.1 API是一个强大且灵活的搜索工具,涵盖了文本分析、索引构建、查询执行等多个环节。通过对API的深入学习,开发者可以构建出满足各种需求的全文搜索引擎。这个CHM文件作为参考文档,将有助于...

    lucene3.6.1学习

    在实际项目中,你可能还需要了解如何优化查询性能,处理多语言文本,以及如何利用缓存和近实时搜索特性来提升用户体验。 总而言之,Lucene 3.6.1 学习是一个全面了解信息检索和搜索引擎技术的好起点。通过研究源码...

    lucene-3.6.0.zip

    1. 文档(Document):代表要索引的信息,可以包含多个字段(Field)。 2. 字段(Field):文档中的具体信息,如标题、内容、作者等,每个字段都有自己的属性,如是否可搜索、是否存储等。 3. 索引(Index):将文档...

    Lucene使用教程

    doc.add(new Field("title", "lucene introduction", Field.Store.YES, Field.Index.TOKENIZED)); doc.add(new Field("content", "lucene works well", Field.Store.YES, Field.Index.TOKENIZED)); ``` **3.3 字段...

    lucene索引

    每个文档由多个字段(Field)组成,字段是具有特定含义的数据单元,如标题、内容、作者等。 #### 1.2 字段(Field) 字段定义了文档中数据的类型和处理方式。例如,`text` 类型的字段适合全文搜索,而 `date` 类型...

    solr的增删改查和高亮以及分组

    Solr是Apache Lucene项目的一个子项目,是一个高性能、全文本搜索服务器,广泛应用于大数据环境下的文本检索。本文将深入探讨Solr 3.6版本中的核心功能:增删改查(CRUD)操作,高亮显示,以及分组查询。 ### 1. ...

Global site tag (gtag.js) - Google Analytics