Class SwitchQParserPlugin
- java.lang.Object
- 
- org.apache.solr.search.QParserPlugin
- 
- org.apache.solr.search.SwitchQParserPlugin
 
 
- 
- All Implemented Interfaces:
- AutoCloseable,- SolrInfoBean,- SolrMetricProducer,- NamedListInitializedPlugin
 
 public class SwitchQParserPlugin extends QParserPlugin A QParserPlugin that acts like a "switch/case" statement.QParser's produced by this plugin will take their primary input string, trimmed and prefixed with " case.", to use as a key to lookup a "switch case" in the parser's local params. If a matching local param is found the resulting param value will then be parsed as a subquery, and returned as the parse result.The " case" local param can be optionally be specified as a switch case to match missing (or blank) input strings. The "default" local param can optionally be specified as a default case to use if the input string does not match any other switch case local params. Ifdefaultis not specified, then any input which does not match a switch case local param will result in a syntax error.In the examples below, the result of each query would be XXX....q={!switch case.foo=XXX case.bar=zzz case.yak=qqq}foo q={!switch case.foo=qqq case.bar=XXX case.yak=zzz} bar // extra whitespace q={!switch case.foo=qqq case.bar=zzz default=XXX}asdf // fallback on default q={!switch case=XXX case.bar=zzz case.yak=qqq} // blank inputA practical usage of this QParsePlugin, is in specifying "appends" fqparams in the configuration of aSearchHandler, to provide a fixed set of filter options for clients using custom parameter names. Using the example configuration below, clients can optionally specify the custom parametersin_stockandshippingto override the default filtering behavior, but are limited to the specific set of legal values (shipping=any|free,in_stock=yes|no|all).<requestHandler name="/select" class="solr.SearchHandler"> <lst name="defaults"> <str name="in_stock">yes</str> <str name="shipping">any</str> </lst> <lst name="appends"> <str name="fq">{!switch case.all='*:*' case.yes='inStock:true' case.no='inStock:false' v=$in_stock}</str> <str name="fq">{!switch case.any='*:*' case.free='shipping_cost:0.0' v=$shipping}</str> </lst> </requestHandler>A slightly more interesting variant of the shippingexample above, would be to combine the switch parser with the frange parser, to allow the client to specify an arbitrary "max shipping" amount that will be used to build a filter if and only if a value is specified. Example:<requestHandler name="/select" class="solr.SearchHandler"> <lst name="invariants"> <str name="shipping_fq">{!frange u=$shipping}shipping_cost</str> </lst> <lst name="defaults"> <str name="shipping">any</str> </lst> <lst name="appends"> <str name="fq">{!switch case='*:*' case.any='*:*' default=$shipping_fq v=$shipping}</str> </lst> </requestHandler>With the above configuration a client that specifies shipping=any, or does not specify ashippingparam at all, will not have the results filtered. But if a client specifies a numeric value (ie:shipping=10,shipping=5, etc..) then the results will be limited to documents whoseshipping_costfield has a value less then that number.A similar use case would be to combine the switch parser with the bbox parser to support an optional geographic filter that is applied if and only if the client specifies a locationparam containing a lat,lon pair to be used as the center of the bounding box:<requestHandler name="/select" class="solr.SearchHandler"> <lst name="invariants"> <str name="bbox_fq">{!bbox pt=$location sfield=geo d=$dist}</str> </lst> <lst name="defaults"> <str name="dist">100</str> </lst> <lst name="appends"> <str name="fq">{!switch case='*:*' default=$bbox_fq v=$location}</str> </lst> </requestHandler>
- 
- 
Nested Class Summary- 
Nested classes/interfaces inherited from interface org.apache.solr.core.SolrInfoBeanSolrInfoBean.Category, SolrInfoBean.Group
 
- 
 - 
Field SummaryFields Modifier and Type Field Description static StringNAMEstatic StringSWITCH_CASEUsed as both a local params key to find the "default" if no blank input is provided to the parser, as well as a prefix (followed by '.' for looking up the switch input.static StringSWITCH_DEFAULTA local param whose value, if specified, is used if no switch case matches the parser input.- 
Fields inherited from class org.apache.solr.search.QParserPluginDEFAULT_QTYPE, standardPlugins
 
- 
 - 
Constructor SummaryConstructors Constructor Description SwitchQParserPlugin()
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description QParsercreateParser(String qstr, org.apache.solr.common.params.SolrParams localParams, org.apache.solr.common.params.SolrParams params, SolrQueryRequest req)return aQParser- 
Methods inherited from class org.apache.solr.search.QParserPlugingetCategory, getDescription, getName, getSolrMetricsContext, initializeMetrics
 - 
Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 - 
Methods inherited from interface org.apache.solr.util.plugin.NamedListInitializedPlugininit
 - 
Methods inherited from interface org.apache.solr.metrics.SolrMetricProducerclose
 
- 
 
- 
- 
- 
Field Detail- 
NAMEpublic static final String NAME - See Also:
- Constant Field Values
 
 - 
SWITCH_CASEpublic static String SWITCH_CASE Used as both a local params key to find the "default" if no blank input is provided to the parser, as well as a prefix (followed by '.' for looking up the switch input.
 - 
SWITCH_DEFAULTpublic static String SWITCH_DEFAULT A local param whose value, if specified, is used if no switch case matches the parser input. If this param is not specified, and no switch case matches the parser input, an error is returned.
 
- 
 - 
Method Detail- 
createParserpublic QParser createParser(String qstr, org.apache.solr.common.params.SolrParams localParams, org.apache.solr.common.params.SolrParams params, SolrQueryRequest req) Description copied from class:QParserPluginreturn aQParser- Specified by:
- createParserin class- QParserPlugin
 
 
- 
 
-