3

We need to implement a search functionality that focuses on specific fields, such as searching for the key phrase "chemical" within the title only, rather than searching the entire content of the page.

We have two different search requirements:

  1. A comprehensive content search with additional filters and facets.
  2. A lighter version that only searches specific fields, such as the title, rather than the entire page content.

We want to know if there's an option to specify the field to be searched within the REST API request object. For example, can we include the field in the request to search for a phrase?

Our goal is to use the same source for both types of searches without creating a separate source. We are simply looking for the flexibility to search within specific fields.

Here are our key questions:

  • Is there a way to specify the field in the REST API request to limit the search to specific fields?
  • If so, how can we structure the request to search within a specific field, like the title, without affecting the broader search functionality?

I appreciate any guidance or examples on how to achieve this.

Updated Info:

I tried the approach suggested by @ckhanna, but it returns results based on the "name" attribute instead of the "teasure_title" attribute.

Request:

{
    "context": {
        "page": {
            "uri": "/search"
        },
        "locale": {
            "country": "tt",
            "language": "en"
        }
    },
    "widget": {
        "items": [
            {
                "rfk_id": "rfkid_7",
                "entity": "mycontententity",
                "sources": [
                    "1007450"
                ],
                "search": {
                    "content": {
                        "fields":[
                            "name",
                            "teaser_title"
                        ]
                    },
                    "query": {
                        "keyphrase": "listing",
                        "operator":"and",
                        "relevance": [
                          {
                           "name": "teaser_title",
                           "analyzer" : "Multi Locale Standard Analyzer",
                           "weight": 50
                          }
                        ]
 
                    }
                }
            }
        ]
    }
}

Response:

{
    "widgets": [
        {
            "rfk_id": "rfkid_7",
            "type": "content_grid",
            "used_in": "page",
            "entity": "mycontententity",
            "content": [
                {
                    "id": "e7837764f80840869a3a57b06ff834ab",
                    "name": "Listing pages  - no mandatory tags",
                    "source_id": "1007450",
                    "teaser_title": "Consumer Product & Retail"
                }
            ],
            "total_item": 1,
            "limit": 1,
            "offset": 0
        }
    ],
    "dt": 8,
    "ts": 1721752431005
}

enter image description here

2 Answers 2

1

You can use the following request in Rest API, to frame the search to look out for phrase in specific search fields as below.

{
    "widget": {
        "items": [
            {
                "rfk_id": "{{Widget_Id}}",
                "entity": "content",
                "search": {
                    "content": {},
                    "query": {
                        "keyphrase": "{{SearchPhrase}}",
                        "relevance": [
                          {
                           "name": "{{Attribute_Name}}",
                           "analyzer" : "{{Attribute_Analyzer}}",
                           "weight": "{{Attribute_Weight}}"
                          }
                        ]
                    }
                }
            }
        ]
    }
}

Inside the above request, attributes for Textual relevance is added under the relevance key as above. This will sure with this request, only these attributes are looked out for search.

Note: These attributes must be added under the Administration -> Domain Settings -> Feature Configuration -> Textual Relevance.

You can find more details for the same here.

https://doc.sitecore.com/search/en/developers/search-and-recommentation-api/index.html

Update:

You can also include/exclude fields for textual relvance under the widget variation setting as below.

enter image description here

Hope this helps!!!

5
  • Thanks, @ckhanna, for your response. I tried the approach you suggested, but it returns results based on the "name" attribute instead of the "teasure_title" attribute. I have provided more details in the updated information above.
    – Suresh R
    Commented Jul 23 at 16:56
  • 1
    @SureshR Can you enter weight between 1.0 to 5.0..
    – ckhanna
    Commented Jul 23 at 17:08
  • @SureshR Moreover you can also include include/exclude fields for textual relevance under the widget variation as well..Updated the answer above. Please check.
    – ckhanna
    Commented Jul 23 at 17:19
  • thank you for your support. It is now working.
    – Suresh R
    Commented Jul 24 at 6:52
  • Glad to hear that @SureshR..Thank you!!!
    – ckhanna
    Commented Jul 24 at 6:55
0

@ckhanna is correct. You have a few options and it all depends on:

  • whether you want things configured from the workbench
  • or whether you'd prefer to pass runtime parameters in your request
  • whether you want to use the same or different widgets

If you'd like to use one widget, create two separate analysers, and change which one you are using at runtime as the first responder suggests.

If you are using different widgets for different search experiences (which would be my recommendation) then go inside the widget rules and overwrite the default analyser that looks into more fields for you by going into Rules > Default Variation > Settings > Textual Relevance.

Note 1: I would recommend not to use huge increments when assigning weights for textual relevance. Ideally increments of 0.1 so that you don't dilute your results too much.

Note 2: If there are things you can configure both from the workbench and from the API (which are most settings in Search, like this one fro textual relevance), aim to configure in the workbench. Runtime requests always trump what has been configured in the workbench but the issue is that you then need a developer to update the behaviour. Whereas, from the workbench any trained business user can update the settings without having to update your code base.

Hope this helps, let me know if any questions.

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.