前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >lucene6按照整形数据排序搜索结果

lucene6按照整形数据排序搜索结果

作者头像
johnhuster的分享
发布2022-03-29 14:08:45
3660
发布2022-03-29 14:08:45
举报
文章被收录于专栏:johnhuster

lucene6跟早期版本有蛮大的区别,这里给出一个按照整形排序的例子,希望帮到有需要的小伙伴:

代码语言:javascript
复制
		Analyzer analyzer = new StandardAnalyzer();
		// 1. create the index
		Directory index = new RAMDirectory();

		IndexWriterConfig config = new IndexWriterConfig(analyzer);

		IndexWriter w = new IndexWriter(index, config);
		addDoc(w, "Lucene in Action", "193398817",70);
		addDoc(w, "lucene for Action", "55320055Z",80);
		addDoc(w, "Managing Gigabytes", "55063554A",90);
		addDoc(w, "The Art of Computer Science", "9900333X",50);
		w.close();		
		
		TermQuery q1 = new TermQuery(new Term("title","Lucene in Action"));
		Query q2 = IntPoint.newRangeQuery("price", 60, 100);
		BooleanQuery q3 = new Builder().add(q1, Occur.SHOULD).add(q2, Occur.MUST).build();
		//
		QueryParser parser = new QueryParser("title",analyzer);
		Query q4 = parser.parse("Lucene in Action");
		IndexReader reader = DirectoryReader.open(index);
		IndexSearcher searcher = new IndexSearcher(reader);
代码语言:javascript
复制
		//升序排列
		TopDocs matches = searcher.search(q4, 10,new Sort(new SortField("price",SortField.Type.LONG,false)));
		for(ScoreDoc doc:matches.scoreDocs){
			logger.info("title:"+searcher.doc(doc.doc).get("title")+"\tisbn:"+searcher.doc(doc.doc).get("isbn")
					+"\nprice:"+searcher.doc(doc.doc).get("price"));
		}
		index.close();
代码语言:javascript
复制
	private static void addDoc(IndexWriter w, String title, String isbn,int price) throws IOException {
		Document doc = new Document();
		doc.add(new TextField("title", title,Field.Store.YES));
		doc.add(new StringField("isbn", isbn, Field.Store.YES));
代码语言:javascript
复制
		//使用IntPoint时需要同时创建同名NumericDocValuesField(排序用)以及同名StoredField对象
		doc.add(new NumericDocValuesField("price",price));
		doc.add(new IntPoint("price",price));
		doc.add(new StoredField("price",price));
		//doc.add(new LegacyIntField("price",price, Field.Store.YES));
		w.addDocument(doc);
	}	
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017/03/16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档