Search code examples
solrsolrj

Does SolrJ support string arrays?


I have a Solr dynamic field added by my CMS with this definition:

<dynamicField name="*_path" type="string" indexed="true" stored="true" multiValued="true"/>

When I do a regular get call in the browser, I get this:

"classification_parent_path":
    [ 
      "tag:geo.example.no,2014:Europa tag:geo.example.no,2014:Skandinavia",
      "tag:geo.example.no,2014:Europa tag:geo.example.no,2014:Skandinavia tag:geo.example.no,2014:Norge tag:geo.example.no,2014:S%C3%B8rlandet",
      "tag:geo.example.no,2014:Europa tag:geo.example.no,2014:Skandinavia tag:geo.example.no,2014:Norge tag:geo.example.no,2014:Nord-Norge"
    ]

And that is how I want it. However, when I do the same request with SolrJ, I get this:

[tag:geo.example.no,2014:Europa tag:geo.example.no,2014:Skandinavia, tag:geo.example.no,2014:Europa tag:geo.example.no,2014:Skandinavia tag:geo.example.no,2014:Norge tag:geo.example.no,2014:S%C3%B8rlandet, tag:geo.example.no,2014:Europa tag:geo.example.no,2014:Skandinavia tag:geo.example.no,2014:Norge tag:geo.example.no,2014:Nord-Norge]

Alas, you can see that the quotes are gone, and the result are given in one long string, so I do not know which tags belongs to which array item. Doesn't SolrJ support simple string arrays?

The SolrJ request is simple, and looks like this:

public SolrDocumentList getSearchResults(String queryParam) {
    SolrQuery query = new SolrQuery(queryParam);

    QueryResponse response = null;
    try {
        response = SolrServer.solr.query(query);
    } catch (SolrServerException e) {
        logger.log(Level.ERROR,
                "Error while running query " + query +
                "on Solr server instance on " + SolrServer.solr.getBaseURL(), e);
    }

    return response.getResults();
}

Solution

  • Use .getFieldValues() instead of .getFieldValue(). The first will give you a Collection that you can iterate over to get all the available values for the field. I'm guessing the representation you're seeing is the .toString() method called on the field as a list.