custom handler with solarium suggester - solr

I am trying to use Soalarium suggester with custom handler. This is my php code:
$client = SolrClient::create();
$query = $client->createSuggester();
$query->setHandler("suggest_custom");
$query->setQuery("store");
$rq = $client->createRequest($query);
$debugInfo["requestUri"] = urldecode($rq->getUri());
$resultset = $client->execute($query);
The code executes, but it always returns empty result set. I echoed the request URL and it looks like this:
suggest_custom?omitHeader=true&wt=json&json.nl=flat&suggest=true&suggest.q=store&suggest.build=false&suggest.reload=false
I think the problem is that query is set as suggest.q=store instead of q=store. If I manually open suggest_custom?q.op=OR&q=store&rows=200 link, than it work as expected.
Ideally I would only send suggest_custom?q.op=OR&q=store&rows=200 to Solr server.
Any idea how to use suggester with custom handlers? This is my handler for completion:
<requestHandler name="/suggest_custom" class="org.apache.solr.handler.component.SearchHandler">
<lst name="defaults">
<str name="wt">json</str>
<str name="defType">edismax</str>
<str name="rows">5</str>
<str name="fl">categoryAutocompleteExactMatch</str>
<str name="qf">categoryAutocompleteExactMatch^10 categoryAutocomplete</str>
</lst>
</requestHandler>

Related

Solr Error: QueryComponent.mergeIds(QueryComponent.java:895) in custom request handler

I'm using Solr 8.4.0 and i tried to make a search request handler that only return a specific sets of field in a collection, without anyone can change what fields to be displayed.
Here is how the request handler looks like
<requestHandler class="solr.SearchHandler" name="/search">
<arr name="components">
<str>query</str>
<str>facet</str>
</arr>
<lst name="defaults">
<int name="rows">10</int>
<str name="wt">json</str>
<str name="q.alt">*:*</str>
</lst>
<lst name="invariants">
<str name="facet">true</str>
<str name="facet.mincount">1</str>
<str name="fl">_uniqueid</str>
<str name="fl">document_title_t</str>
<str name="fl">document_title_string_s</str>
<str name="fl">document_shortsummary_t</str>
<str name="fl">page_url_s</str>
<str name="fl">topic_path</str>
<str name="fl">itemid_s</str>
<str name="echoParams">none</str>
<str name="omitHeader">true</str>
</lst></requestHandler>
After making the collection and trying the request handler, i received
this error
It seems this issue only happens when we are using multiple shards, changing the collection to a single shard removes the error, but we need to have multiple shards for this collection later on production. We are using 2 shards and 3 replicas
I have managed to solve this issue. By going through solr's code from repository in github, i found out that at queryComponent.java line 895 it's trying to access a certain header. After removing the omitHeader invariant the request handler seems to work perfectly

/browse handler (velocity UI) not initiating CustomQueryParser

I have defined my CustomQueryParser in my RequestHandler.
Below is the RequestHandler part of the code in solrconfig.xml
<requestHandler name="/browse" class="solr.SearchHandler">
<lst name="defaults">
<str name="defType">customparser</str>
<str name="echoParams">explicit</str>
<str name="qf">body_txt manu2</str>
<str name="q.op">AND</str>
<!-- <str name="mm">100%</str> -->
</lst>
</requestHandler>
And below is the queryParser part of the code in solrconfig.xml
<queryParser name="customparser" class="com.mycompany.lucene.search.QueryParserPlugin"/>
I have ensured that the jar file associated to the QueryParserPlugin is loading.
But when I make a query the correct documents are not reflecting.
Is there a way I can print from within my CustomQueryParser jar to the solr.log to know if it is being invoked?
Also how do i get this to work?
Have been struggling with this since quite a few days. Please Help !
Thanks

Plugin for Solr Suggester Component

I created a plugin for Solr (version 6.3) that adds a permission layer to filter the retrieved documents using a database query (e.g: the user with ID 2 hasn't permissions to see the document with the ID 1); As the logic that defines if an user has permissions needs fields that aren't indexed in the Solr, i need to check in the database.
To achieve that i created a Query Parser (called DocumentsByUserParser that extends the class QParserPlugin) defined in the solrconfig.xml with the following:
<queryParser name="filterDocument" class="mypackage.solr.plugin.DocumentsByUserParser" />
To call this plugin, i only have to set the fq parameter with the {!filterDocument userId='<user_id>'} along with the other query parameters. Note that the code above works well with the Search Component with an edismax type.
My question is: can i create a new similar plugin, as described above, that works with the Suggest Component? Because when I index a document, the user may have (or not) permissions to see that document, so the suggester shouldn't show suggestions that the user hasn't permissions to see.
I defined my Suggest Component along with the Request Handler with the following:
<requestHandler name="/suggest" class="solr.SearchHandler" startup="lazy">
<lst name="defaults">
<str name="suggest">true</str>
<str name="suggest.count">10</str>
</lst>
<arr name="components">
<str>suggest</str>
</arr>
</requestHandler>
<searchComponent name="suggest" class="solr.SuggestComponent">
<lst name="suggester">
<str name="name">mySuggester</str>
<str name="lookupImpl">AnalyzingInfixLookupFactory</str>
<str name="indexPath">suggester_infix_dir</str>
<str name="highlight">true</str>
<str name="dictionaryImpl">DocumentDictionaryFactory</str>
<str name="field">AUTO_COMPLETE_FIELD</str>
<str name="suggestAnalyzerFieldType">text_general</str>
<str name="buildOnStartup">false</str>
<str name="buildOnCommit">false</str>
</lst>
</searchComponent>
P.S - The context filter query described in https://lucene.apache.org/solr/guide/6_6/suggester.html only works with a single indexed field, so this will not work with my use case.

Setting default for Solr faceting

I was wondering how I can enable faceting in the solr config without being forced to put it all the times in the URL. Secondly how can set the number of fields?
thanks
You can pass all the default parameters you need to your request handler like this:
<requestHandler name="standard" class="solr.SearchHandler" default="true">
<lst name="defaults">
<str name="facet">true</str>
<str name="facet.field">field1</str>
<str name="facet.field">field2</str>
<str name="facet.field">field3</str>
</lst>
</requestHandler>
This way you have facet enabled by default for field1, field2 and field3.

Solr Request Handlers and Query Parsers

I am new to Solr. Please help me with following queries:
What is the difference between a request handler and a query parser?
I'm thinking it's that when a query is sent through URL in solr, at first the query is parsed using the Query parser. Request handler then takes the parsed query and searches and presents the response according to request handler parameters. Is this Correct?
What is the default query parser and default request handler in Solr?
Parameter deftype is used to specify parser and qt for request handlers right?
I wrote this query
select?q=features:power%20features:latency&deftype=dismax
which works, but select?q=features:power%20features:latency&qt=dismax does not.
Here is my requestHandler
<requestHandler name="dismax" class="solr.SearchHandler">
<lst name="defaults">
<str name="defType">dismax< /str>
<str name="echoParams">explicit< /str>
<float name="tie">0.01< /float>
<str name="qf">text^0.5 features^1.0 name^1.2 sku^1.5 id^10.0 manu^1.1 cat^1.4< /str>
<str name="pf">text^0.2 features^1.1 name^1.5 manu^1.4 manu_exact^1.9< /str>
<str name="bf">popularity^0.5 recip(price,1,1000,1000)^0.3< /str>
<str name="fl">id,name,price,score< /str>
<str name="mm">2<-1 5<-2 6<90%< /str>
<int name="ps">100< /int>
<str name="q.alt">*:*< /str>
<!-- example highlighter config, enable per-query with hl=true
-- >
< str name="hl.fl">text features name</str>
<!-- for this field, we want no fragmenting, just highlighting
-- >
< str name="f.name.hl.fragsize">0< /str>
<!-- instructs Solr to return the field itself if no query terms are
found
-- >
<str name="f.name.hl.alternateField">name< /str>
<str name="f.text.hl.fragmenter">regex< /str>
<!-- defined below
-->
< /lst>
</requestHandler>
Default request handler is the one with default="true" parameter in SolrConfig.xml (SearchHandler, if you haven't changed that).
Request handler handles requests, so it is a starting point for every request, which means that request handler uses/calls query parser (either the one specified by the url or default one) as its first step.
You want to get:
1. Documents with "power latency" as the phrase?
2. Or docs with both terms anywhere in a doc?
3. Or docs with either of those terms?
Try like this:
1. select?q=features:"power latency"&qt=dismax
2. select?q=features:power+features:latency&qt=dismax&mm=2
3. select?q=features:power+features:latency&qt=dismax&mm=1
More info on DisMaxQParserPlugin.

Resources