Hi Ed,
Here's an example, based on the code from
<http://wiki.apache.org/lucene-java/TheBasics> (UNTESTED):
public class LuceneFreshnessTest {
public static void main(String[] args) throws IOException {
RAMDirectory directory = new RAMDirectory();
IndexWriter writer =
new IndexWriter(directory, new WhitespaceAnalyzer(), true);
Document doc = new Document();
doc.add(new Field("id", "1", Field.Store.YES,
Field.Index.NOT_ANALYZED));
// 1 day since epoch = "1"
Field daysField = new Field
("days", "1", Field.Store.NO, Field.Index.ANALYZED);
daysField.setOmitNorms(true);
doc.add(daysField);
writer.addDocument(doc);
doc = new Document();
doc.add(new Field("id", "2", Field.Store.YES,
Field.Index.NOT_ANALYZED));
// 3 days since epoch = "1 1 1"
daysField = new Field("days", "1 1 1", Field.Store.NO,
Field.Index.ANALYZED);
daysField.setOmitNorms(true);
doc.add(daysField);
writer.addDocument(doc);
doc = new Document();
doc.add(new Field("id", "3", Field.Store.YES,
Field.Index.NOT_ANALYZED));
// 5 days since epoch = "1 1 1 1 1"
daysField = new Field("days", "1 1 1 1 1", Field.Store.NO,
Field.Index.ANALYZED);
daysField.setOmitNorms(true);
doc.add(daysField);
writer.addDocument(doc);
writer.close();
IndexSearcher searcher = new IndexSearcher(directory);
Query query = new TermQuery(new Term("days", "1"));
TopDocs rs = searcher.search(query, null, 10);
System.out.println("Total hits: " + rs.totalHits);
Document firstHit = searcher.doc(rs.scoreDocs[0].doc);
System.out.println("First hit ID (newest=3): "
+ firstHit.getField("id").toString());
}
}
Steve
On 01/15/2009 at 10:41 PM, mitu2009 wrote:
> Hi,
>
> Thanks for your suggestions!
> Am new to Lucene...would appreciate if u could elaborate ur
> following point with an example:
>
> > Add a separate field, say "days", in which you will put as many "1" as
> > many days elapsed since the epoch (not neccessarily since 1 Jan 1970 -
> > pick a date that makes sense for you). Then, if you want to prioritize
> > newer documents, just add "+days:1" to your query. Voila - the final
> > results are a sum of other score factors plus a score factor that is
> > higher for more recent document, containing more 1-s.
>
> Thanks again!
> Ed
>
> Steven A Rowe wrote:
> >
> > On 01/14/2009 at 10:44 PM, mitu2009 wrote:
> > > Is it possible to bubble up newer records in lucene search results?
> > > ie.I want Lucene to give a higher score to records which are closer to
> > > today's date.
> >
> > In addition to the fine ideas given by previous posters, Andrzej
> > Bialecki has described a technique that uses term frequency alone to
> > affect the score: from
> > <http://www.gossamer-threads.com/lists/lucene/java-user/43457>:
> >
> > > Here's the trick that works for me, without the issues of boost
> > > resolution or FunctionQuery.
> > >
> > > Add a separate field, say "days", in which you will put as many "1" as
> > > many days elapsed since the epoch (not neccessarily since 1 Jan 1970 -
> > > pick a date that makes sense for you). Then, if you want to prioritize
> > > newer documents, just add "+days:1" to your query. Voila - the final
> > > results are a sum of other score factors plus a score factor that is
> > > higher for more recent document, containing more 1-s.
> > >
> > > If you are dealing with large time spans, you can split this into
> > > years and days-in-a-year, and apply query boosts, like "+years:1^10.0
> > > +days:1^0.02". Do some experiments and find what works
> > > best for you.
> >
> > As noted in a later thread discussing this issue
> > <http://www.gossamer-threads.com/lists/lucene/java-user/64482>, you
> > should turn norms off on the "days" field:
> >
> <http://lucene.apache.org/java/2_4_0/api/org/apache/lucene/document/Fieldable.html#setOmitNorms(boolean)>
> >
> > Steve
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]