1

I'm working on the solr suggest module and I want to know about the Query and ContextFilterQuery in SuggestParameters class.

Can anyone explain it?

public class SuggestParameters
{
    public SuggestParameters();

    public string Query { get; set; }
    public string ContextFilterQuery { get; set; }
}

2 Answers 2

1

SuggestParameters add parameters to solr query that are related to suggester only (these parameters start with suggest. prefix).

Simple example:

SolrSuggestQuery query = "acco";
var options = new SuggestHandlerQueryOptions
{
    Parameters = new SuggestParameters
    {
        Count = 5
    }
};

var result = context.Suggest(query, options);

This code snippet send to solr request:

/suggest?q=acco&fq=_indexname:(test_search_index)&suggest=true&suggest.count=5 with results:

{
  "responseHeader":{
    "status":0,
    "QTime":0},
  "suggest":{
    "mySuggesterContent":{
      "acco":{
        "numFound":5,
        "suggestions":[{
            "term":"accordance",
            "weight":6,
            "payload":""},
          {
            "term":"according",
            "weight":3,
            "payload":""},
          {
            "term":"account",
            "weight":71,
            "payload":""},
          {
            "term":"account's",
            "weight":1,
            "payload":""},
          {
            "term":"accounts",
            "weight":46,
            "payload":""}]}}
}

When we change our example to following:

SolrSuggestQuery query = "acco";
var options = new SuggestHandlerQueryOptions
{
    Parameters = new SuggestParameters
    {
        Count = 5,
        Query = "bank"
    }
};

var result = context.Suggest(query, options);

request to sorl will be:

/suggest?q=acco&fq=_indexname:(test_search_index)&suggest=true&suggest.count=5&suggest.q=bank with results:

{
  "responseHeader":{
    "status":0,
    "QTime":2},
  "suggest":{
    "mySuggesterContent":{
      "bank":{
        "numFound":5,
        "suggestions":[{
            "term":"bank",
            "weight":93,
            "payload":""},
          {
            "term":"bank's",
            "weight":3,
            "payload":""},
          {
            "term":"banker",
            "weight":1,
            "payload":""},
          {
            "term":"bankers",
            "weight":2,
            "payload":""},
          {
            "term":"banking",
            "weight":55,
            "payload":""}]}}
 }

As you can see suggest.q parameter has higher priority and best practice is you use it instead of q parameter (because all parameter without suggest. prefix are not interesting for suggester in general).

ContextFilterQuery.

Lets imagine that we have a lot of Product items and each product has category (like sport/food/etc). And we want to find product names in our suggester. But we also want to have an option to find suggested products only in specific category to make our suggestions more relevant.

To do it, we just need to add contextField parameter in our suggester:

<searchComponent name="suggest" class="solr.SuggestComponent">
  <lst name="suggester">
    <str name="name">mySuggesterContent</str>
    <str name="lookupImpl">AnalyzingInfixLookupFactory</str>
    <str name="field">name</str>
    <str name="weightField">price</str>
    <str name="contextField">category</str>
    <str name="suggestAnalyzerFieldType">string</str>
    <str name="buildOnStartup">false</str>
  </lst>
</searchComponent>

And when we change our example to following:

var options = new SuggestHandlerQueryOptions
{
    Parameters = new SuggestParameters
    {
        Count = 5,
        Query = "air",
        ContextFilterQuery = "shoes"
    }
};

request to solr will be:

/suggest?q=&fq=_indexname:(test_search_index)&suggest=true&suggest.count=5&suggest.q=air&suggest.cfq=shoes

with results like: airmax etc. In this case you force prevent suggestions like airpods etc, because they are not relevant for you case for some reasons (for example if your users have ability to check categories where they want to search only).

1

This makes use of the Solr Suggester functionality documented here. In the Solr configuration, a field is specified as a contextField. When you call a suggest query, you would set the Query value to be your regular suggestion, and then set your ContextFilterQuery value to only bring back suggestions with the before mentioned contextField equal to the ContextFilterQuery. You must also configure your Lookup Implementation and Dictionary Implementation.

For (very simple) example you have a contextField set to "DayOfWeek" and make Suggestion query where the Query is "food" and ContextFilterQuery equal to "Thursday". Solr will return the suggestions for "food" but only if DayOfWeek on the item is "Thursday."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.