Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Create a Custom Solr QueryParser for Fun and Profit

DZone's Guide to

Create a Custom Solr QueryParser for Fun and Profit

In this post, I'll show you what you need to do to implement a custom Solr QueryParser.

· Big Data Zone
Free Resource

Learn best practices according to DataOps. Download the free O'Reilly eBook on building a modern Big Data platform.

In this post, I'll show you what you need to do to implement a custom Solr QueryParser.

Step 1

Extend the QParserPlugin:

public class TestQueryParserPlugin extends QParserPlugin {
  public void init(NamedList namedList) {
  }

  @Override public QParser createParser(String s, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
    return new TestQParser(s, localParams, params, req);
  }
}

This is the class you'll define in solrconfig.xml, informing Solr of your queryparser. Define it like so:

<queryParser name="myfunparser" 
class="org.supermind.solr.queryparser.TestQParserPlugin"/>

Step 2

Extend QParser:

  public class TestQParser extends QParser {
    public TestQParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
      super(qstr, localParams, params, req);
    }

    @Override public Query parse() throws SyntaxError {
      return null;
    }
  }

Step 3

Actually implement the parsing in the parse() method.

Suppose we want to make a really simple parser for term queries, which are space-delimited. Here's how I'd do it:

@Override public Query parse() throws SyntaxError {
      String defaultField = req.getSchema().getDefaultSearchFieldName();
      QueryParser.Operator defaultOperator = QueryParser.Operator.valueOf(req.getSchema().getQueryParserDefaultOperator());
      BooleanClause.Occur op = (defaultOperator == QueryParser.Operator.AND) ? BooleanClause.Occur.MUST : BooleanClause.Occur.SHOULD;
      String[] arr = qstr.split(" ");
      BooleanQuery bq = new BooleanQuery(true);
      for(String s: arr) {
        if(s.trim().length() == 0) continue;
        bq.add(new TermQuery(new Term(defaultField, s)), op);
      }
      return bq;
    }

Step 4

In your query, use the nested query syntax to call your QueryParser:

http://localhost:8983/solr/collection1/select?q={!myfunparser}foo+bar+car

Maybe in a follow-up post I'll include the full code with jars and all.

Find the perfect platform for a scalable self-service model to manage Big Data workloads in the Cloud. Download the free O'Reilly eBook to learn more.

Topics:
java ,solr ,big data

Published at DZone with permission of Kelvin Tan. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}