Sorry to all, there was a terrible bug in my code. I should have checked whether the query was changed by (q.toString().equals(newQuery.toString()) instead of (q != newQuery)!
Hana wrote: > > Hi > > I need a help with boolean queries in my custom RequestHandler. The > purpose of the handler is to translate human readable > date (like January 1990 or 15.2.1983 or 1995) into two date range fields > using internal date representation. > > E.g. simple search 'q=chronological:1942' translates to > > '+from:[1942-01-01T00:00:01Z TO 1942-12-31T23:59:59Z] > +to:[1942-01-01T00:00:01Z TO 1942-12-31T23:59:59Z]' > > Everything works fine in the previous search, but when I try more complex > boolean search it returns no result. > > E.g complex search 'q=London AND chronological:1942' > > my RequestHandler translates it to > > '+text:london +(+from:[1942-01-01T00:00:01Z TO 1942-12-31T23:59:59Z] > +to:[1942-01-01T00:00:01Z TO 1942-12-31T23:59:59Z])' > > So this query above doesn't work and I don't see the reason why because it > seems to produce correct query. > > > I have checked it with direct query bellow, it returns correct results: > > 'q=London AND (from:[1942-01-01T00:00:00Z TO 1942-12-31T23:59:59Z] AND > to:[1942-01-01T00:00:00Z TO 1942-12-31T23:59:59Z])' > > and the boolean query syntax is: > > '+text:london +(+from:[1942-01-01T00:00:00 TO 1942-12-31T23:59:59] > +to:[1942-01-01T00:00:00 TO 1942-12-31T23:59:59])' > > > So I do not not understand why the previous is not working when the > boolean query is totally the same except for the 'Z' char for dates > string. But as the simple query works it don't seems to be the reason of > not working of the complex query. > > > Cheers > > Hana > > > Here's the code of the RequestHandler: > > > public class CenturyShareRequestHandling extends StandardRequestHandler > { > > public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse > rsp) throws Exception > { > SolrParams p = req.getParams(); > String query = p.get(CommonParams.Q); > Query q = QueryParsing.parseQuery(query, req.getSchema()); > Query newQuery = searchChronological(q); > if (q != newQuery) > { > ModifiableSolrParams m = new > ModifiableSolrParams(SolrParams.toMultiMap(p.toNamedList())); > m.remove(CommonParams.Q); > m.add(CommonParams.Q, newQuery.toString()); > req.setParams(m); > } > super.handleRequestBody(req, rsp); > } > > > private Query searchChronological(Query q) > { > if (q instanceof BooleanQuery) > { > BooleanQuery bq = (BooleanQuery) q; > BooleanClause[] cl = bq.getClauses(); > for (int i = 0; i < cl.length; i++) > { > if (cl[i].getQuery() instanceof BooleanQuery) > { > searchChronological(cl[i].getQuery()); > } else if (cl[i].getQuery() instanceof TermQuery) > { > String result = getTemporalTerm((TermQuery) cl[i].getQuery()); > if (result != null) > { > Query dateQuery = replaceChronological(result); > if (dateQuery != null) > cl[i].setQuery(dateQuery); > } > } > } > } > else if (q instanceof TermQuery) > { > String result = getTemporalTerm((TermQuery) q); > if (result != null) > { > Query dateQuery = replaceChronological(result); > if (dateQuery != null) > q = dateQuery; > } > } > return q; > } > > private String getTemporalTerm(TermQuery tq) > { > if ("chronological".equals(tq.getTerm().field())) > return tq.getTerm().text(); > else > return null; > } > > private Query replaceChronological(String chronological) > { > DateRange r = getDateRange(chronological); > BooleanQuery query = null; > if (r.getStartDate() != null && r.getEndDate() != null) > { > String startDate = r.getFormatedStartDate(); > String endDate = r.getFormatedEndDate(); > Term start = new Term("from", startDate); > Term end = new Term("from", endDate); > > RangeQuery startQuery = new RangeQuery(start, end, true); > start = new Term("to", startDate); > end = new Term("to", endDate); > RangeQuery endQuery = new RangeQuery(start, end, true); > query = new BooleanQuery(); > query.add(new BooleanClause(startQuery, BooleanClause.Occur.MUST)); > query.add(new BooleanClause(endQuery, BooleanClause.Occur.MUST)); > } > return query; > } > > private DateRange getDateRange(String text) > { > if (text == null) > return null; > else > { > DateParser p = new DateParser(); > return p.parseDateRange(text); > } > } > > } > > -- View this message in context: http://www.nabble.com/Query-Parsing-in-Custom-Request-Handler-tp21501351p21504363.html Sent from the Solr - User mailing list archive at Nabble.com.