Search code examples
solrj

How to retrieve all stored fields from Core with SolrJ


Is it possible to retrieve all stored fields from certain Core using SolrJ client ?

e.g. <field name="author" type="string" stored="true" indexed="true"/>

Thanks, Peter


Solution

  • You can get the information about the fields using SolrJ client for the specific core.

    Use the following code snippet to retrieve.

        import java.util.ArrayList;
    
        import org.apache.solr.client.solrj.SolrQuery;
        import org.apache.solr.client.solrj.SolrServer;
        import org.apache.solr.client.solrj.SolrServerException;
        import org.apache.solr.client.solrj.impl.HttpSolrServer;
        import org.apache.solr.client.solrj.response.QueryResponse;
        import org.apache.solr.common.params.CommonParams;
        import org.apache.solr.common.util.NamedList;
        import org.apache.solr.common.util.SimpleOrderedMap;
    
    
    
        SolrServer solrCore = new HttpSolrServer("http://{host:port}/solr/core-name");
        SolrQuery query = new SolrQuery();
        query.add(CommonParams.QT, "/schema/fields");
        QueryResponse response = solrCore.query(query);
        NamedList responseHeader = response.getResponseHeader();
        ArrayList<SimpleOrderedMap> fields = (ArrayList<SimpleOrderedMap>) response.getResponse().get("fields");
        for (SimpleOrderedMap field : fields) {
            Object fieldName = field.get("name");
            Object fieldType = field.get("type");
            Object isIndexed = field.get("indexed");
            Object isStored = field.get("stored");
            // you can use other attributes here.
    
            System.out.println(field.toString());
        }
    

    Please note i am using 4.10.2 version of SolrJ.

    Thanks,

    Shiva Kumar SS