Skip to main content

Searching with context

Abstract

Advanced example that shows you how to use the Discover Search and Recommendation API to search with context.

Use the context object to give Sitecore Discover as much information as possible. Your search results are more relevant to the query when you apply specific context object types.

The following is an example search query with user context.

In this example, you search for three products using the keyphrase red and apply a user context for gender.

Here is the request:

{
  "query": {
    "keyphrase": [["red"]]
  },
  "n_item": 3,
  "content": {},
  "context": {
    "user": {
      "gender": "female"
    }
  }
}

Here is the response:

{
  "ts": 1480977544,
  "rid": "response_id_17",
  "url": "/searchpage?keyphrase=red&keyphrase=shorts&context=Q1fcsl087pn",
  "query2id": {
    "keyphrase": "keyphrase_id_red__shorts"
  },
  "content": {
    "product": {
      "value": [
        {"name": "stylish bags", "color": "red", "price": 50, "category_names": ["women clothing"]},
        {"name": "red dresses", "price": 80, "gender": "female"},
        {"name": "women's beach shorts", "color": "red", "price": 45}
      ]
    }
  },
  "n_item": 3,
  "total_item": 45,
  "page_number": 1,
  "total_page": 15
}

The response returns the top three products related to the female gender.