lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chellasamy G <chellasam...@zohocorp.com>
Subject RE: How to use customized Collector class with IndexSearcher.searchAfter() method
Date Mon, 21 May 2018 15:54:13 GMT
Hi Uwe,



Thanks for replying. I now understood how to do it.



Actually my requirement was to search records which fall between certain hours in a day i.e
records between 9AM to 5PM(each record has a GMT timestamp field indexed). The problem here
is the timezone, I have to adjust the hour value based on the timezone.

If it is going to be a single timezone, I could index the adjusted hour value in the index
itself but in my case the timezone is dynamic and it could vary. So, I used a customized collector
as shown in the below code, in which I have written my code logic to filter the records.



Please check the code and let me know if it is a correct way to do it or if there is any better
way to accomplish the requirement.





MyCollector timeFilterCollector = new MyCollector("TIME_FIELD", 9, 17, "PST", sortBy, 25,
null); 

indexSearcher.search(query, timeFilterCollector);

TopDocs topDocs = timeFilterCollector.collector.topDocs();





class MyCollector implements Collector { 





      private String filterField;

      private int startHour;

      private int endHour;

      private String timeZone;

      public TopFieldCollector collector;



      public MyCollector(String filterField, int startHour, int endHour, String timeZone,
Sort sort, int numHits, FieldDoc after) {

            this.filterField = filterField;

            this.startHour = startHour;

            this.endHour = endHour;

            this.timeZone = timeZone;

            this.collector = TopFieldCollector.create(sort, numHits, after, true, false, false);

      }



      @Override

      public LeafCollector getLeafCollector(final LeafReaderContext context)

            throws IOException {



            SortedNumericDocValues values = DocValues.getSortedNumeric(context.reader(), filterField);

            final LeafCollector topLevelLeafCollector = collector.getLeafCollector(context);



            return new LeafCollector() {

            

                  @Override

                  public void collect(int doc) throws IOException {

                        if(values.advanceExact(doc)) {

                              long value = values.nextValue();

                              int evtHour = new DateTime(value * 1000L, DateTimeZone.forID(timeZone)).getHourOfDay();
 // convert timestamp to hrs

                              if(startHour &lt;= evtHour &amp;&amp; evtHour &lt;=
endHour) {

                                    topLevelLeafCollector.collect(doc);

                              }

                        }

                  }



                  @Override

                  public void setScorer(Scorer arg0) throws IOException {

                  }

            };

      }



      @Override

      public boolean needsScores() {

            return false;

      }

}





Thanks,

Satyan






---- On Fri, 18 May 2018 23:34:11 +0530 Uwe Schindler &lt;uwe@thetaphi.de&gt; wrote
----




Hi, 

 

search after is implemented inside the collector, so it cannot be exposed by APIs like that.
Lucene by default does not support anything like searching after or starting with, because
this task is done by the collector (soring, paging,...). The TopDocsCollector subclasses are
doing this. 

 

Uwe 

 

----- 

Uwe Schindler 

Achterdiek 19, D-28357 Bremen 

http://www.thetaphi.de 

eMail: uwe@thetaphi.de 

 

&gt; -----Original Message----- 

&gt; From: Chellasamy G &lt;chellasamy.g@zohocorp.com&gt; 

&gt; Sent: Friday, May 18, 2018 8:00 PM 

&gt; To: java-user &lt;java-user@lucene.apache.org&gt; 

&gt; Subject: How to use customized Collector class with 

&gt; IndexSearcher.searchAfter() method 

&gt; 

&gt; Hi, 

&gt; 

&gt; 

&gt; 

&gt; I have written a customized collector and usually search the index using this 

&gt; collector. 

&gt; 

&gt; 

&gt; 

&gt; i.e using the below method, 

&gt; 

&gt; 

&gt; 

&gt; IndexSearcher.search(Query query, Collector results) 

&gt; 

&gt; 

&gt; 

&gt; 

&gt; 

&gt; But, I cant find any searchAfter() methods in IndexSearcher which accepts a 

&gt; Collector input. 

&gt; 

&gt; Please let me know how to use a customized collector when using 

&gt; searchAfter() method. 

&gt; 

&gt; 

&gt; 

&gt; 

&gt; 

&gt; Thanks, 

&gt; 

&gt; Satyan 

&gt; 

&gt; 

&gt; 

 

 

 

--------------------------------------------------------------------- 

To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org 

For additional commands, e-mail: java-user-help@lucene.apache.org 

 







Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message