/[sdx]/sdx_v2/src/java/fr/gouv/culture/sdx/search/lucene/query/DateIntervalQuery.java
ViewVC logotype

Diff of /sdx_v2/src/java/fr/gouv/culture/sdx/search/lucene/query/DateIntervalQuery.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.33 by rpandey, Fri Mar 26 15:26:37 2004 UTC revision 1.34 by rpandey, Wed May 12 14:44:13 2004 UTC
# Line 40  import fr.gouv.culture.sdx.search.lucene Line 40  import fr.gouv.culture.sdx.search.lucene
40  import fr.gouv.culture.sdx.utils.constants.Node;  import fr.gouv.culture.sdx.utils.constants.Node;
41  import org.apache.lucene.index.Term;  import org.apache.lucene.index.Term;
42  import org.apache.lucene.search.TermQuery;  import org.apache.lucene.search.TermQuery;
43    import org.apache.lucene.search.RangeQuery;
44  import org.xml.sax.ContentHandler;  import org.xml.sax.ContentHandler;
45  import org.xml.sax.SAXException;  import org.xml.sax.SAXException;
46  import org.xml.sax.helpers.AttributesImpl;  import org.xml.sax.helpers.AttributesImpl;
# Line 57  import java.util.Date; Line 58  import java.util.Date;
58   * exclusive If only one of the two is not null, the query is unbounded.   * exclusive If only one of the two is not null, the query is unbounded.
59   * It is an error to give two null dates.   * It is an error to give two null dates.
60   */   */
 //TODO replace this with a Lucene RangeQuery  
   
61  public class DateIntervalQuery extends AbstractQuery {  public class DateIntervalQuery extends AbstractQuery {
62    
63      /** The searched field. */      /** The searched field. */
64      private Field field;      private Field field;
65    
66      /** The lower bound date. */      /** The lower bound date. */
67      private Date beginDate;      private Date _beginDate;
68    
69      /** The upper bound date. */      /** The upper bound date. */
70      private Date endDate;      private Date _endDate;
71    
72        protected boolean _inclusive = true;
73    
74      /**Creates a query      /**Creates a query
75       *       *
# Line 93  public class DateIntervalQuery extends A Line 94  public class DateIntervalQuery extends A
94       *  @param  endDate         The upper bound date (may be null).       *  @param  endDate         The upper bound date (may be null).
95       */       */
96      public void setUp(SearchLocations sLocs, String fieldName, Date beginDate, Date endDate) throws SDXException {      public void setUp(SearchLocations sLocs, String fieldName, Date beginDate, Date endDate) throws SDXException {
97            setUp(sLocs, fieldName, beginDate, endDate, _inclusive);
98        }
99        /**
100         *  Buils a date interval query.
101         *
102         * <p>
103         * One of beginDate or endDate must be non null.
104         *
105         *  @param  sLocs           The SearchLocations object (indices to be searched).
106         *  @param  fieldName       The field name to search (if null or non existent, default field will be searched).
107         *  @param  beginDate       The lower bound date (may be null).
108         *  @param  endDate         The upper bound date (may be null).
109         */
110        public void setUp(SearchLocations sLocs, String fieldName, Date beginDate, Date endDate, boolean inclusive) throws SDXException {
111    
112          // Calls the super constructor.          // Calls the super constructor.
113          super.setSearchLocations(sLocs);          super.setSearchLocations(sLocs);
# Line 114  public class DateIntervalQuery extends A Line 129  public class DateIntervalQuery extends A
129              throw new SDXException(super.getLog(), SDXExceptionCode.ERROR_DATE_INVALID, null, null);              throw new SDXException(super.getLog(), SDXExceptionCode.ERROR_DATE_INVALID, null, null);
130    
131          // Keep the dates          // Keep the dates
132          this.beginDate = beginDate;          this._beginDate = beginDate;
133          this.endDate = endDate;          this._endDate = endDate;
134    
135            String lowerBound = fr.gouv.culture.sdx.search.lucene.DateField.dateToString(_beginDate);
136            String upperBound = fr.gouv.culture.sdx.search.lucene.DateField.dateToString(_endDate);
137            setLuceneRangeQuery(fieldName, lowerBound, upperBound, inclusive);
138    
139            /* old code using a date filter
140          // Lucene handles date interval queries using a filter, which means that we need a          // Lucene handles date interval queries using a filter, which means that we need a
141          // query that will return all the results, and then apply the date filter.          // query that will return all the results, and then apply the date filter.
142          luceneQuery = new TermQuery(new Term(LuceneDocumentBase.INTERNAL_FIELD_NAME_SDXALL, LuceneDocumentBase.INTERNAL_SDXALL_FIELD_VALUE));          luceneQuery = new TermQuery(new Term(LuceneDocumentBase.INTERNAL_FIELD_NAME_SDXALL, LuceneDocumentBase.INTERNAL_SDXALL_FIELD_VALUE));
143    
144          // Build the filter                  // Build the filter
145          DateFilter f;          DateFilter f;
146          if (beginDate == null)          if (beginDate == null)
147              f = DateFilter.Before(field.getCode(), endDate); // Upper bounded              f = DateFilter.Before(field.getCode(), endDate); // Upper bounded
# Line 137  public class DateIntervalQuery extends A Line 157  public class DateIntervalQuery extends A
157          filt.setUp(Filter.BOOLEAN_OPERATOR_AND);          filt.setUp(Filter.BOOLEAN_OPERATOR_AND);
158          filt.add(c);          filt.add(c);
159          addFilter(filt);          addFilter(filt);
160            */
161        }
162    
163        protected void setLuceneRangeQuery(String fieldName, String lowerBound, String upperBound, boolean inclusive){
164            super.luceneQuery = new RangeQuery(new Term(fieldName, lowerBound), new Term(fieldName, upperBound),inclusive);
165      }      }
166    
167      /**      /**
# Line 210  public class DateIntervalQuery extends A Line 233  public class DateIntervalQuery extends A
233          }          }
234          atts.addAttribute("", Node.Name.FIELD, Node.Name.FIELD, Node.Type.CDATA, field.getCode());          atts.addAttribute("", Node.Name.FIELD, Node.Name.FIELD, Node.Type.CDATA, field.getCode());
235    
236          if (beginDate != null) atts.addAttribute("", Node.Name.BEGIN_DATE, Node.Name.BEGIN_DATE, Node.Type.CDATA, fr.gouv.culture.sdx.utils.Date.formatDate(beginDate));          if (_beginDate != null) atts.addAttribute("", Node.Name.BEGIN_DATE, Node.Name.BEGIN_DATE, Node.Type.CDATA, fr.gouv.culture.sdx.utils.Date.formatDate(_beginDate));
237          if (endDate != null) atts.addAttribute("", Node.Name.END_DATE, Node.Name.END_DATE, Node.Type.CDATA, fr.gouv.culture.sdx.utils.Date.formatDate(endDate));          if (_endDate != null) atts.addAttribute("", Node.Name.END_DATE, Node.Name.END_DATE, Node.Type.CDATA, fr.gouv.culture.sdx.utils.Date.formatDate(_endDate));
238          //Let's build the query representation          //Let's build the query representation
239          hdl.startElement(sdxNsUri, localName, qName, atts);          hdl.startElement(sdxNsUri, localName, qName, atts);
240    

Legend:
Removed from v.1.33  
changed lines
  Added in v.1.34

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26