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; |
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 |
* |
* |
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); |
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 |
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 |
/** |
/** |
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 |
|
|