Lucene: Introduction to Lucene (Part III)

1. How do we add index for number type?

// new Field(String, String, Field.Store.YES, Field.Index.NOT_ANALYZED)
// is only applicable for building for string type
// we should use a sub-class of Field called NumericField
doc.add(new Field("score", 110 + "", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new NumericField("score", Field.Store.YES, true).setIntValue(110));

2. How do we add index for date type?

// as there is no sub-class for date.
// But we can store date info as long type by using date.getTime();
doc.add(new NumericField("date", Field.Store.YES, true).setLongValue((new Date()).getTime()));

3. How do we fecth these data when execute query?

	/**
	 * Search
	 * 
	 * @throws IOException
	 * @throws CorruptIndexException
	 * 
	 */
	public void search() throws CorruptIndexException, IOException
	{
		IndexReader reader = IndexReader.open(dir);
		IndexSearcher searcher = new IndexSearcher(reader);
		TermQuery query = new TermQuery(new Term("name", "Davy"));
		TopDocs topDocs = searcher.search(query, 10);
		for (ScoreDoc scoreDoc : topDocs.scoreDocs)
		{
			Document doc = searcher.doc(scoreDoc.doc);
			String score = doc.get("score");
			String date = doc.get("date");
			float boost = doc.getBoost();
			System.out.println("Score = " + score + ", Date = " + date
					+ ", Boost = " + boost);
		}
	}

   Comments:

        1) Whatever the data type we stored in index file. When we get the data, they are all as the type of String.

 4.

    1) What is boost? 

    2) How do we set boost for doc?

猜你喜欢

转载自davyjones2010.iteye.com/blog/1872484
今日推荐