lucene 添加date 和 数字 Field类型

添加索引过程中,如何添加Date 类型和 int 类型,下面是例子:

 

package com.hb.lucence.test;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.NumericField;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

public class NumberAndBoostTest {

	private Date[] dates = null;
	private int[] attachs = { 2, 3, 1, 4, 5, 5 };
	private RAMDirectory directory = null;
	private IndexReader indexReader = null;

	/**
	 * 初始化日期数据
	 */
	public void initDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		dates = new Date[6];
		int[] attachs = { 2, 3, 1, 4, 5, 5 };
		try {
			dates[0] = sdf.parse("2010-02-19");
			dates[1] = sdf.parse("2012-01-11");
			dates[2] = sdf.parse("2011-09-19");
			dates[3] = sdf.parse("2010-12-22");
			dates[4] = sdf.parse("2012-01-01");
			dates[5] = sdf.parse("2011-05-19");
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	public NumberAndBoostTest() {
		initDate();
	}

	public void index() {
		IndexWriter iw = null;
		try {
			//1、创建目录
			directory = new RAMDirectory();
			//2、创建IndexWriter对象
			iw = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
			Document doc = null;
			for (int i = 0; i < 6; i++) {
				//3、创建document对象
				doc = new Document();
				//4、添加Field对象
				doc.add(new NumericField("date", Store.YES, true).setLongValue(dates[i].getTime()));
				doc.add(new NumericField("number", Field.Store.YES, true).setIntValue(attachs[i]));
				//5、添加document对象
				iw.addDocument(doc);
			}
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (iw != null) {
				try {
					//6、关闭indexwriter
					//iw.commit();
					iw.close();
				} catch (CorruptIndexException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 由于打开indexReader需要较大的资源
	 * @return
	 */
	public IndexSearcher getSercher() {
		try {
			if (indexReader == null) {
				indexReader = IndexReader.open(directory);
			} else {
				//如果IndexReader对象有变化,则返回IndexReader对象,否则返回null
				IndexReader newIndexReader = IndexReader.openIfChanged(indexReader);
				if (newIndexReader != null) {
					indexReader = newIndexReader;
				}
			}
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new IndexSearcher(indexReader);
	}

	public void queryByDate(String field, Date begin, Date end, int num) {
		long start = begin.getTime();
		long last = end.getTime();
		try {
			IndexSearcher indexSearcher = this.getSercher();
			// 获取范围
			Query query = NumericRangeQuery.newLongRange(field, start, last, true, true);
			TopDocs tds = indexSearcher.search(query, num);
			for (ScoreDoc sd : tds.scoreDocs) {
				Document doc = indexSearcher.doc(sd.doc);
				System.out.println("doc.date : " + longToDateStr(Long.parseLong(doc.get("date"))) + " ----- doc.num : " + doc.get("number"));
			}
			//查询完毕之后需要关闭indexSearher对象
			indexSearcher.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void queryByNum(String field, int start, int end, int num) {
		try {
			IndexSearcher indexSearcher = this.getSercher();
			// 根据NumericRangeQuery数字对象查询
			Query query = NumericRangeQuery.newIntRange(field, start, end, true, true);
			TopDocs tds = indexSearcher.search(query, num);
			for (ScoreDoc sd : tds.scoreDocs) {
				Document doc = indexSearcher.doc(sd.doc);
				System.out.println("doc.date : " + longToDateStr(Long.parseLong(doc.get("date"))) + " ----- doc.num : " + doc.get("number"));
			}
			//查询完毕之后需要关闭indexSearher对象
			indexSearcher.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 将long类型的数据转为Date对象,并格式化
	 * @param time
	 * @return
	 */
	public String longToDateStr(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(new Date(time));
	}

	public static void main(String[] agrs) {
		NumberAndBoostTest test = new NumberAndBoostTest();
		test.index();
		//根据数字查询
		// test.queryByNum("number", 3, 10,10);

		//根据日期查询
		Date myDate1;
		Date myDate2;
		try {
			DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
			myDate1 = dateFormat1.parse("2012-01-01");
			myDate2 = dateFormat1.parse("2013-01-01");
			test.queryByDate("date", myDate1, myDate2, 10);
		} catch (ParseException e) {
			e.printStackTrace();
		}

	}

}

 

你可能感兴趣的:(Lucene)