lucene-solr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thorsten Scherler <thorsten.scherler....@juntadeandalucia.es>
Subject Re: filter input from multiple fields
Date Fri, 22 Dec 2006 13:37:02 GMT
On Fri, 2006-12-22 at 13:29 +0100, Thorsten Scherler wrote:
> Hi all, 
> 
> I am looking for some information about which is the best way to write a
> MultipleFieldRequestHandler.java.


I did a small hack and it works like a charm without the above mentioned
handler. I only activated variable substitution for the FQ for testing
if you think that is a nice feature I can activate it for the rest.

Index: src/java/org/apache/solr/util/SolrPluginUtils.java
===================================================================
--- src/java/org/apache/solr/util/SolrPluginUtils.java  (revision
489649)
+++ src/java/org/apache/solr/util/SolrPluginUtils.java  (working copy)
@@ -59,7 +59,10 @@
  */
 public class SolrPluginUtils {

-  /**
+  private static final String VARIABLE_DETERMINATOR_CLOSE = "}";
+  private static final String VARIABLE_DETERMINATOR_OPEN = "${";
+
+/**
    * Set defaults on a SolrQueryRequest.
    *
    * RequestHandlers can use this method to ensure their defaults are
@@ -819,13 +822,24 @@
     SolrQueryParser qp = new SolrQueryParser(s.getSchema(), null);
     for (String q : in) {
       if (null != q && 0 != q.trim().length()) {
-        out.add(qp.parse(q));
+        out.add(qp.parse(substitute(q, req)));
       }
     }
     return out;
   }

-  /**
+  private static String substitute(String q, SolrQueryRequest req) {
+    if (q.contains(VARIABLE_DETERMINATOR_OPEN)){
+        String beforeVariable
=q.substring(0,q.indexOf(VARIABLE_DETERMINATOR_OPEN)) ;
+        String variable =
q.substring(q.indexOf(VARIABLE_DETERMINATOR_OPEN)+VARIABLE_DETERMINATOR_OPEN.length(),q.indexOf(VARIABLE_DETERMINATOR_CLOSE))
;
+        String afterVariable
=q.substring(q.indexOf(VARIABLE_DETERMINATOR_CLOSE)+VARIABLE_DETERMINATOR_CLOSE.length())
;
+        String variableValue= req.getParams().get(variable);
+        q = substitute(beforeVariable+variableValue+afterVariable,
req);
+    }
+    return q;
+}
+
+/**

salu2

> 
> My use case is that I have a form (the next version of
> http://andaluciajunta.es/portal/aj-bojaBuscador/0,22815,,00.html) where
> you limit the query via a couple of different fields.
> 
> Imaging I have a form like  
> <form action="select/" method="get">
>       <input name="start" value="0" type="hidden"/>
>       <input name="rows" value="10" type="hidden"/> term:
>       <input type="text" name="q"/>
>       <br/>
>       <table>
>         <tr>
>           <td colspan="3">between dates:</td>
>         </tr>
>         <tr>
>           <td>
>             <input type="text" name="startDate"/>
>           </td>
>           <td>y</td>
>           <td>
>             <input type="text" name="endDate"/>
>           </td>
>         </tr>
>       </table>
>       <input type="submit" value="buscar"></input> </form>
> 
> Using the StandardRequestHandler without prior processing would result
> that "startDate" and "endDate" would be ignored since they are not
> within the query string and are not solr standard param that got
> processed.
> 
> Meaning I can either construct the query string (including this filter)
> on the client side or with a request Handler, right?
> 
> JavaScript on the client side is not a possibility for my client so here
> I am to write my first RequestHandler.
> 
> I could write a request handler special for my client, but I think a
> more generic solution is more beneficial for all of us.
> 
> So my idea is to define:
> <requestHandler name="dateRange"
>   class="solr.MultipleFieldRequestHandler">
>   <lst name="defaults">
>     <str name="echoParams">explicit</str>
>     <str name="filterFields">startDate endDate</str>
>     <str name="startDate">*</str>
>     <str name="endDate">*</str>
>     <str name="fq">date:[${startDate} TO ${endDate}]</str>
>   </lst>
> </requestHandler>
> 
> Where filterFields defines the array of fields that can be used to limit
> the super set of docs. If this fields are null for the given request
> then we use they defined standard e.g. <str name="startDate">*</str> in
> the fq. Within the fq ${...} has to be parsed with the actual value of
> the variable. Since we defined default values that should work fine,
> right?
> 
> The only thing that a simpler more basic solution could be to just tweak
> the standard handler to 
> a) get all params and store them in a hashmap (already done)
> b) change SolrPluginUtils.parseFilterQueries to parse the fq string and
> replace ${startDate} with the corresponding key="startDate"
> req.getParam(key). General speaking implement a variable substitution
> parser (which I would have to do as well for the MFRH).
> 
> What do you think is the better approach write a RequestHandler or
> extend the standard one or there even an easier way?
> 
> TIA for any infos.
> 
> salu2
> 


Mime
View raw message