You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by Peter Markey <su...@gmail.com> on 2012/01/26 19:33:08 UTC

solr custom component

Hello,

I am building a custom component in Solr and I am trying to construct a
query to get the latest (based on a date field) DocID using SolrIndexSearcher.
Below is a short snippet of my code:

SolrIndexSearcher searcher =
final SchemaField sf = searcher.getSchema().getField(dateField);
//dateField is one of the fields that contains timestamp of the record

final IndexSchema schema = searcher.getSchema();

Query rangeQ = ((DateField)(sf.getType())).getRangeQuery(null, sf,null,NOW,
false,true); //NOW is current Date

DocList dateDocs = searcher.getDocList(rangeQ, base, null, 0, 1); //base is
a set of doc filters to limit search



Though I get some docs that satisfy the query, my goal is to get the doc
whose's dateField is closest to the current time. Are there any other
queries I can employ for this?


Thanks a lot for any suggestions.

Re: solr custom component

Posted by Erick Erickson <er...@gmail.com>.
Look at the Sort class. You just specify the field
you want to sort on, direction, and pass the class
to your IndexSearcher.search method.

Best
Erick

On Tue, Jan 31, 2012 at 1:24 AM, Peter Markey <su...@gmail.com> wrote:
> Hi Eric,
>
> I tried looking for a sample code to sort on Date but was unable to find
> one? I am using 3.4 version.
> Any idea as to where I can find one?
>
> Thanks a ton
>
> On Fri, Jan 27, 2012 at 8:13 AM, Erick Erickson <er...@gmail.com>wrote:
>
>> Why not just sort on date and take the first doc returned in the list?
>>
>> Best
>> Erick
>>
>> On Thu, Jan 26, 2012 at 10:33 AM, Peter Markey <su...@gmail.com>
>> wrote:
>> > Hello,
>> >
>> > I am building a custom component in Solr and I am trying to construct a
>> > query to get the latest (based on a date field) DocID using
>> SolrIndexSearcher.
>> > Below is a short snippet of my code:
>> >
>> > SolrIndexSearcher searcher =
>> > final SchemaField sf = searcher.getSchema().getField(dateField);
>> > //dateField is one of the fields that contains timestamp of the record
>> >
>> > final IndexSchema schema = searcher.getSchema();
>> >
>> > Query rangeQ = ((DateField)(sf.getType())).getRangeQuery(null,
>> sf,null,NOW,
>> > false,true); //NOW is current Date
>> >
>> > DocList dateDocs = searcher.getDocList(rangeQ, base, null, 0, 1); //base
>> is
>> > a set of doc filters to limit search
>> >
>> >
>> >
>> > Though I get some docs that satisfy the query, my goal is to get the doc
>> > whose's dateField is closest to the current time. Are there any other
>> > queries I can employ for this?
>> >
>> >
>> > Thanks a lot for any suggestions.
>>

Re: solr custom component

Posted by Peter Markey <su...@gmail.com>.
Hi Eric,

I tried looking for a sample code to sort on Date but was unable to find
one? I am using 3.4 version.
Any idea as to where I can find one?

Thanks a ton

On Fri, Jan 27, 2012 at 8:13 AM, Erick Erickson <er...@gmail.com>wrote:

> Why not just sort on date and take the first doc returned in the list?
>
> Best
> Erick
>
> On Thu, Jan 26, 2012 at 10:33 AM, Peter Markey <su...@gmail.com>
> wrote:
> > Hello,
> >
> > I am building a custom component in Solr and I am trying to construct a
> > query to get the latest (based on a date field) DocID using
> SolrIndexSearcher.
> > Below is a short snippet of my code:
> >
> > SolrIndexSearcher searcher =
> > final SchemaField sf = searcher.getSchema().getField(dateField);
> > //dateField is one of the fields that contains timestamp of the record
> >
> > final IndexSchema schema = searcher.getSchema();
> >
> > Query rangeQ = ((DateField)(sf.getType())).getRangeQuery(null,
> sf,null,NOW,
> > false,true); //NOW is current Date
> >
> > DocList dateDocs = searcher.getDocList(rangeQ, base, null, 0, 1); //base
> is
> > a set of doc filters to limit search
> >
> >
> >
> > Though I get some docs that satisfy the query, my goal is to get the doc
> > whose's dateField is closest to the current time. Are there any other
> > queries I can employ for this?
> >
> >
> > Thanks a lot for any suggestions.
>

Re: solr custom component

Posted by Erick Erickson <er...@gmail.com>.
Why not just sort on date and take the first doc returned in the list?

Best
Erick

On Thu, Jan 26, 2012 at 10:33 AM, Peter Markey <su...@gmail.com> wrote:
> Hello,
>
> I am building a custom component in Solr and I am trying to construct a
> query to get the latest (based on a date field) DocID using SolrIndexSearcher.
> Below is a short snippet of my code:
>
> SolrIndexSearcher searcher =
> final SchemaField sf = searcher.getSchema().getField(dateField);
> //dateField is one of the fields that contains timestamp of the record
>
> final IndexSchema schema = searcher.getSchema();
>
> Query rangeQ = ((DateField)(sf.getType())).getRangeQuery(null, sf,null,NOW,
> false,true); //NOW is current Date
>
> DocList dateDocs = searcher.getDocList(rangeQ, base, null, 0, 1); //base is
> a set of doc filters to limit search
>
>
>
> Though I get some docs that satisfy the query, my goal is to get the doc
> whose's dateField is closest to the current time. Are there any other
> queries I can employ for this?
>
>
> Thanks a lot for any suggestions.