`
DavyJones2010
  • 浏览: 147724 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

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?

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics