`

lucene入门代码三(near-real-time)

阅读更多
一个简单的接近实时搜索的例子源自(Lucene in  aiction)
package com.yale.lucene;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

/**
 * 
 * 接近实时搜索
 * 
 */
public class NearRealTimeTest
{
	public static void main(String[] args) throws Exception
	{
		// Create RAMDirectory
		Directory dir = new RAMDirectory();
		IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(
				Version.LUCENE_30), IndexWriter.MaxFieldLength.UNLIMITED);
		for(int i =0;i<10;i++){
			Document doc = new Document();
			doc.add(new Field("id",""+i,Field.Store.NO,Field.Index.NOT_ANALYZED_NO_NORMS));
			doc.add(new Field("text","aaa",Field.Store.NO,Field.Index.ANALYZED));
			writer.addDocument(doc);
		}
		//Create near-real-time reader
		IndexReader reader = writer.getReader();
		//Warp reader in  IndexSearcher
		IndexSearcher searcher = new IndexSearcher(reader);
		
		Query query = new TermQuery(new Term("text","aaa"));
		
		TopDocs docs = searcher.search(query,1);
		//Search returns 10 hits
		System.out.println("匹配结果:"+docs.totalHits+"次");
		//delete 1 document
		writer.deleteDocuments(new Term("id","7"));
		//add 1 document
		Document doc = new Document();
		doc.add(new Field("id","11",Field.Store.NO,Field.Index.NOT_ANALYZED_NO_NORMS));
		doc.add(new Field("text","bbb",Field.Store.NO,Field.Index.ANALYZED));
		writer.addDocument(doc);
		//Reopen reader
		IndexReader newReader = reader.reopen(); 
		//Confirm reader is new
		System.out.println(reader==newReader);
		reader.close();
		searcher = new IndexSearcher(newReader);
		TopDocs hits = searcher.search(query,10);
		//Search returns 9 hits
		System.out.println("匹配结果:"+hits.totalHits+"次");
		query = new TermQuery(new Term("text", "bbb"));
		hits = searcher.search(query, 1);
		//Search returns 1 hits
		System.out.println("匹配结果:"+hits.totalHits+"次");
		newReader.close();
		writer.close();
		
	}
}

分享到:
评论

相关推荐

    lucene-analyzers-smartcn-7.7.0-API文档-中文版.zip

    赠送源代码:lucene-analyzers-smartcn-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-analyzers-smartcn-7.7.0.pom; 包含翻译后的API文档:lucene-analyzers-smartcn-7.7.0-javadoc-API文档-中文(简体)版....

    lucene-analyzers-common-6.6.0-API文档-中文版.zip

    赠送源代码:lucene-analyzers-common-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-analyzers-common-6.6.0.pom; 包含翻译后的API文档:lucene-analyzers-common-6.6.0-javadoc-API文档-中文(简体)版.zip;...

    lucene-core-7.7.0-API文档-中文版.zip

    赠送源代码:lucene-core-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-core-7.7.0.pom; 包含翻译后的API文档:lucene-core-7.7.0-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache.lucene:lucene...

    lucene-backward-codecs-7.3.1-API文档-中英对照版.zip

    赠送源代码:lucene-backward-codecs-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-codecs-7.3.1.pom; 包含翻译后的API文档:lucene-backward-codecs-7.3.1-javadoc-API文档-中文(简体)-英语-对照...

    lucene-spatial-extras-7.3.1-API文档-中英对照版.zip

    赠送源代码:lucene-spatial-extras-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras-7.3.1.pom; 包含翻译后的API文档:lucene-spatial-extras-7.3.1-javadoc-API文档-中文(简体)-英语-对照版....

    lucene-analyzers-smartcn-7.7.0-API文档-中英对照版.zip

    赠送源代码:lucene-analyzers-smartcn-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-analyzers-smartcn-7.7.0.pom; 包含翻译后的API文档:lucene-analyzers-smartcn-7.7.0-javadoc-API文档-中文(简体)-英语...

    lucene-spatial-extras-7.2.1-API文档-中英对照版.zip

    赠送源代码:lucene-spatial-extras-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras-7.2.1.pom; 包含翻译后的API文档:lucene-spatial-extras-7.2.1-javadoc-API文档-中文(简体)-英语-对照版....

    lucene-spatial-extras-6.6.0-API文档-中英对照版.zip

    赠送源代码:lucene-spatial-extras-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras-6.6.0.pom; 包含翻译后的API文档:lucene-spatial-extras-6.6.0-javadoc-API文档-中文(简体)-英语-对照版....

    lucene-backward-codecs-7.2.1-API文档-中英对照版.zip

    赠送源代码:lucene-backward-codecs-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-codecs-7.2.1.pom; 包含翻译后的API文档:lucene-backward-codecs-7.2.1-javadoc-API文档-中文(简体)-英语-对照...

    lucene-backward-codecs-6.6.0-API文档-中文版.zip

    赠送源代码:lucene-backward-codecs-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-backward-codecs-6.6.0.pom; 包含翻译后的API文档:lucene-backward-codecs-6.6.0-javadoc-API文档-中文(简体)版.zip; ...

    lucene-backward-codecs-6.6.0-API文档-中英对照版.zip

    赠送源代码:lucene-backward-codecs-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-backward-codecs-6.6.0.pom; 包含翻译后的API文档:lucene-backward-codecs-6.6.0-javadoc-API文档-中文(简体)-英语-对照...

    lucene-core-7.2.1-API文档-中文版.zip

    赠送源代码:lucene-core-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-core-7.2.1.pom; 包含翻译后的API文档:lucene-core-7.2.1-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache.lucene:lucene...

    lucene-suggest-6.6.0-API文档-中文版.zip

    赠送源代码:lucene-suggest-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-suggest-6.6.0.pom; 包含翻译后的API文档:lucene-suggest-6.6.0-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache....

    lucene-highlighter-6.6.0-API文档-中文版.zip

    赠送源代码:lucene-highlighter-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-highlighter-6.6.0.pom; 包含翻译后的API文档:lucene-highlighter-6.6.0-javadoc-API文档-中文(简体)版.zip; Maven坐标:org...

    lucene-core-6.6.0-API文档-中文版.zip

    赠送源代码:lucene-core-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-core-6.6.0.pom; 包含翻译后的API文档:lucene-core-6.6.0-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache.lucene:lucene...

    lucene-sandbox-7.2.1-API文档-中文版.zip

    赠送源代码:lucene-sandbox-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-sandbox-7.2.1.pom; 包含翻译后的API文档:lucene-sandbox-7.2.1-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache....

    lucene-spatial-extras-7.7.0-API文档-中文版.zip

    赠送源代码:lucene-spatial-extras-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras-7.7.0.pom; 包含翻译后的API文档:lucene-spatial-extras-7.7.0-javadoc-API文档-中文(简体)版.zip; Maven...

    lucene-backward-codecs-7.7.0-API文档-中文版.zip

    赠送源代码:lucene-backward-codecs-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-backward-codecs-7.7.0.pom; 包含翻译后的API文档:lucene-backward-codecs-7.7.0-javadoc-API文档-中文(简体)版.zip; ...

    lucene-analyzers-common-7.7.0-API文档-中文版.zip

    赠送源代码:lucene-analyzers-common-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-analyzers-common-7.7.0.pom; 包含翻译后的API文档:lucene-analyzers-common-7.7.0-javadoc-API文档-中文(简体)版.zip;...

    lucene-analyzers-common-7.2.1-API文档-中文版.zip

    赠送源代码:lucene-analyzers-common-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-analyzers-common-7.2.1.pom; 包含翻译后的API文档:lucene-analyzers-common-7.2.1-javadoc-API文档-中文(简体)版.zip;...

Global site tag (gtag.js) - Google Analytics