Julius
Julius

Reputation: 2864

Multiple facet.field fields in Solrj SolrQuery.add(SolrParams params) method overwrite each other

When I use SolrQuery's add(SolrParams params) method to add two "facet.fields" the last one overwrites the first, because the SolrQuery.add() method internally delegates to a put() method of a LinkedHashMap and a LinkedHashMap has unique keys.

How would I add two facet.field values to a SolrQuery? Do I really have to use the more specific SolrQuery.addFacetField() method (and use other methods to set things like mincount and sort for my facetfield)?

Upvotes: 0

Views: 1003

Answers (1)

Julius
Julius

Reputation: 2864

It seems that SolrQuery.add(SolrParams params) overwrites identical keys:

ModifiableSolrParams params1 = getParamsForTagsFacet(); // this has a 'facet.field'
ModifiableSolrParams params2 = getParamsForRegionsFacet(); // this has a 'facet.field'
SolrQuery q = new SolrQuery() // extends ModifiableSolrParams
q.add(params1); // adds first 'facet.field'
q.add(params2); // overwrites first 'facet.field'

Use q.add(String key, String... value) instead to retain both 'facet.field' keys

ModifiableSolrParams solrParams = facet.createSolrParams();
Iterator<String> iterator = solrParams.getParameterNamesIterator();

while(iterator.hasNext()){
    String param = iterator.next();
    String value = solrParams.get(param);
    // adding multiple identical params like this works. 
    // It creates a single key with multiple values in the underlying LinkedHashMap
    q.add(param, value); 

}

Upvotes: 3

Related Questions