lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Wojtek Piaseczny (JIRA)" <j...@apache.org>
Subject [jira] Commented: (SOLR-1666) SolrParams conversion to NamedList and back to SolrParams misses the Arrays with more than one value
Date Fri, 11 Jun 2010 00:10:19 GMT

    [ https://issues.apache.org/jira/browse/SOLR-1666?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12877650#action_12877650
] 

Wojtek Piaseczny commented on SOLR-1666:
----------------------------------------

I made the following change in my code base to work around this issue. Is there a better approach?

{code:borderStyle=solid}
    // in SolrParams.java
    public static SolrParams toSolrParams(NamedList params) {
    // if no keys are repeated use the faster MapSolrParams
    HashMap<String,String> map = new HashMap<String,String>();
    for (int i=0; i<params.size(); i++) {
      Object val = params.getVal(i);
      if(val instanceof String[])
      {
        return new MultiMapSolrParams(toMultiMap(params));
      }
      else
      {
        String prev = map.put(params.getName(i), val.toString());
        if (prev!=null) return new MultiMapSolrParams(toMultiMap(params));
      }
    }
    return new MapSolrParams(map);
  }
  
  // in SolrParams.java
  /** Create a Map<String,String[]> from a NamedList */
  public static Map<String,String[]> toMultiMap(NamedList params) {
    HashMap<String,String[]> map = new HashMap<String,String[]>();
    for (int i=0; i<params.size(); i++) {
      String name = params.getName(i);
      Object val = params.getVal(i);
      addParam(name,val,map);
    }
    return map;
  }
  
  // in MultiMapSolrParams.java
  public static void addParam(String name, Object val, Map<String,String[]> map) {
    String[] arr = map.get(name);
    if (arr == null) {
      // first time we're seeing this parameter name.
      if (val instanceof String[]) {
        arr = (String[]) val;
      } else {
        arr = new String[] { val.toString() };
      }
    } else {
      // add new value(s) to existing value(s).
      if (val instanceof String[]) {
        String[] saVal = (String[]) val;
        String[] newarr = new String[arr.length + saVal.length];
        // copy current values.
        System.arraycopy(arr, 0, newarr, 0, arr.length);
        // copy new values.
        System.arraycopy(saVal, 0, newarr, arr.length, newarr.length - arr.length);
        arr = newarr;
      } else {
        String[] newarr = new String[arr.length + 1];
        System.arraycopy(arr, 0, newarr, 0, arr.length);
        newarr[arr.length] = val.toString();
        arr = newarr;
      }
    }
    map.put(name, arr);
  }
{code} 

> SolrParams conversion to NamedList and back to SolrParams misses the Arrays with more
than one value
> ----------------------------------------------------------------------------------------------------
>
>                 Key: SOLR-1666
>                 URL: https://issues.apache.org/jira/browse/SOLR-1666
>             Project: Solr
>          Issue Type: Bug
>          Components: search
>    Affects Versions: 1.3, 1.4
>            Reporter: Nestor Oviedo
>            Priority: Minor
>
> When a parameter in a SolrParams instance is an Array that has more than one element,
the method SolrParams.toNamedList() generates a NamedList<Object> correctly, but when
the method SolrParams.toSolrParams() is invoked with that NamedList instance, the resultant
SolrParams instance has that parameter as a String, wich is the result of the String[].toString()
method.
> TestCase:
> {code}
> public class TestDismaxQParserPlugin extends DisMaxQParserPlugin {
> 	private Log log = LogFactory.getLog(this.getClass());
> 	public QParser createParser(String qstr, SolrParams localParams, SolrParams params,
SolrQueryRequest req) {
> 		// TestCase with the param facet.field
> 		if(params.getParams(FacetParams.FACET_FIELD) != null) {
> 			// Original Values
> 			log.debug("FACET.FIELD Param - Before");
> 			String[] facetFieldBefore = params.getParams(FacetParams.FACET_FIELD);
> 			log.debug("toString():"+facetFieldBefore.toString());
> 			log.debug("length:"+facetFieldBefore.length);
> 			log.debug("Elements:");
> 			for(String value : facetFieldBefore) 
> 				log.debug("[class "+value.getClass().getName()+"] "+value);
> 			
> 			// Transforming
> 			NamedList<Object> paramsList = params.toNamedList();
> 			params = SolrParams.toSolrParams(paramsList);
> 			// Result Values
> 			log.debug("FACET.FIELD Param - After");
> 			String[] facetFieldAfter = params.getParams(FacetParams.FACET_FIELD);
> 			log.debug("toString():"+facetFieldAfter.toString());
> 			log.debug("length:"+facetFieldAfter.length);
> 			log.debug("Elements:");
> 			for(String value : facetFieldAfter) 
> 				log.debug("[class "+value.getClass().getName()+"] "+value);
> 		} else {
> 			log.debug("FACET.FIELD NOT SPECIFIED");
> 		}
> 		return super.createParser(qstr, localParams, params, req);
> 	}
> }
> {code}
> Editing the solrconfig.xml file for this QueryParser to be used and using an URL like
"http://host:port/path/select?q=something&facet=true&facet.field=subject&facet.field=date"
the output is (only the interesting lines):
> FINA: FACET.FIELD Param - Before
> FINA: toString():[Ljava.lang.String;@c96ad7c
> FINA: length:2
> FINA: Elements:
> FINA: [class java.lang.String] subject
> FINA: [class java.lang.String] date
> FINA: FACET.FIELD Param - After
> FINA: toString():[Ljava.lang.String;@44775121
> FINA: length:1
> FINA: Elements:
> FINA: [class java.lang.String] [Ljava.lang.String;@c96ad7c

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


Mime
View raw message