yura
yura

Reputation: 14655

Is it possible to request fields by specific pattern in solr?

I can use fl=fld1,fld2,fld3 tor return specific fields from solr. But sometimes i generate dynamic field names like ".*_attribute_group1" and want solr to return all group. Is it posible to extend solr 'fl' field with regexp? Where to look in solr codebase?

Upvotes: 0

Views: 200

Answers (1)

mailboat
mailboat

Reputation: 1077

Solr doesn't support wildcard patterns in field names ( "fl" param ). But you could write your own component to process the request & identify the list of fileds present in the index that you want.

Pesudo Code of extending search component to implement custom fields..

// PSUEDO CODE
public class FLPatternCustomComponent extends SearchComponent {

    @Override
        //Gauranteed to be called before any other SearchComponent.process
    public void prepare(ResponseBuilder rb) throws IOException {
           SolrParams params = rb.req.getParams();
           //Input fl=field_*
           String[] inputFl = params.getParams(CommonParams.FL);
           Collection<String> existingFl = rb.req.getSearcher().getFieldNames();
           //process & find matching fields{
           SolrQuery newFields = new SolrQuery();
           newFields.set(CommonParams.FL, "field_1,field_2,field_3,field_4");
           AppendedSolrParams appendedParams = new AppendedSolrParams(params, q);
           rb.req.setParams(appendedParams);

           super.prepare(rb);
        }

    @Override
    public void process(ResponseBuilder rb) throws IOException {
        //Process request
        super.process(rb);
    }       
}

You could have this a component chained to your existing request handler or create your request handler & perhaps you could also add any additional invariants.

You may want to consider any additional performance overhead of custom component & its processing. I have created couple of custom components for custom ranking & custom request handlers & use it without much issues.

You might want to check Solr Plugin Development.

Upvotes: 1

Related Questions