Result collapsing: data model

There are several elements within the data model that carry information about result collapsing.

The values of these elements are used, in conjunction with some logic, to display result collapsing.

response.resultPacket.resultsSummary.collapsed

This element contains an integer indicating the number of collapsed search results in the result set. If there are no collapsed results, or collapsing is off then this value is set to zero.

{
"question": {},
"response": {
  "resultPacket": {
    "details": {},
    "query": "prize",
    "queryAsProcessed": "prize",
    "queryRaw": "prize",
    "querySystemRaw": null,
    "queryCleaned": "prize",
    "collection": "training~sp-nobel-prize",
    "resultsSummary": {
      "fullyMatching": 6,
      "collapsed": 833, (1)
      "partiallyMatching": 0,
      "totalMatching": 6,
      "estimatedCounts": false,
      "carriedOverFtd": null,
      "totalDistinctMatchingUrls": null,
      "numRanks": 10,
      "currStart": 1,
...
1 This indicates the total number of matching items that have been collapsed in the search results. The collapsed count does not count towards the total number of search results as these items have been removed from the result set and grouped under the remaining items.
question.inputParameters["s"]

If the system query contains a ?: query language operator then this indicates that the set of results is an expanded set of collapsed results (i.e. the set of items that were grouped in the initial search. The value of this operator is the collapsing signature for the grouping that has been applied.

{
  "question": {
    "executionContext": "Admin",
    "query": "prize",
    "originalQuery": "prize",
    "collection": {},
    "profile": "nobel-prize-winners-search_preview",
    "clive": [ ],
    "form": "simple",
    "cnClickedCluster": null,
    "cnPreviousClusters": [ ],
    "metaParameters": [ ],
    "systemMetaParameters": [ ],
    "additionalParameters": {},
    "environmentVariables": {},
    "dynamicQueryProcessorOptions": [],
    "userKeys": [ ],
    "selectedFacets": [ ],
    "selectedCategoryValues": { },
    "facetsQueryConstraints": [ ],
    "facetCollectionConstraints": null,
    "facetsGScopeConstraints": null,
    "impersonated": false,
    "requestId": "10.0.2.0",
    "logQuery": null,
    "inputParameters": {
      "HTTP_HOST": [
        "localhost:8443"
      ],
      "s": [
        "?:9BC56EE9E1630C9D" (1)
      ],
      "collapsing": [
        "off" (2)
      ],
      "profile": [
        "nobel-prize-winners-search_preview"
      ],
1 When returning the set of results that form a group beneath a single search result the signature is set as the value of the ?: operator within the system query s field.
2 collapsing should also be set to off when returning the set of collapsed results because the collapsing should only occur when running the main set of search results.
s.result.collapsed

The collapsed node of each search result item is null if there are no collapsed items for the search result. If there are collapsed items then this node contains a data structure that indicates the number of matching results, the signature and the individual result items that were collapsed.

{
"question": {},
"response": {
  "resultPacket": {
    "details": {},
    "query": "prize",
    "queryAsProcessed": "prize",
    "queryRaw": "prize",
    "querySystemRaw": null,
    "queryCleaned": "prize",
    "collection": "training~sp-nobel-prize",
    "resultsSummary": {},
    "spell": null,
    "results": [
    {
      "rank": 1,
      "score": 1000,
      "title": "https://docs.squiz.net/training-resources/nobel/nobel.csv?id=587",
      "collection": "training~ds-nobel-prize-winners",
      "component": 0,
      "collapsed": null, (1)
      "liveUrl": "https://docs.squiz.net/training-resources/nobel/nobel.csv?id=587",
      "summary": "",
      "allSummaryText": null,
      "cacheUrl": "/s/cache?collection=training~ds-nobel-prize-winners&url=https%3A%2F%2Fdocs.squiz.net%2Ftraining-resources%2Fnobel%2Fnobel.csv%3Fid%3D587&profile=nobel-prize-winners-search",
      "date": 1574985600000,
...
1 When s.result.collapsed = null, this indicates that no results have been collapsed for this search result.

Contrast this to are result set that contains collapsed items:

{
"question": {},
"response": {
  "resultPacket": {
    "details": {},
    "query": "prize",
    "queryAsProcessed": "prize",
    "queryRaw": "prize",
    "querySystemRaw": null,
    "queryCleaned": "prize",
    "collection": "training~sp-nobel-prize",
    "resultsSummary": {},
    "spell": null,
    "results": [
      {
        "rank": 1,
        "score": 1000,
        "title": "https://docs.squiz.net/training-resources/nobel/nobel.csv?id=587",
        "collection": "training~ds-nobel-prize-winners",
        "component": 0,
        "collapsed": { (1)
          "signature": "9BC56EE9E1630C9D", (2)
          "count": 119, (3)
          "column": "[prizeName]", (4)
          "results": [ (5)
            {
              "rank": 0,
              "score": 0,
              "title": "https://docs.squiz.net/training-resources/nobel/nobel.csv?id=339",
              "collection": null,
              "component": 0,
              "collapsed": null,
              "liveUrl": "https://docs.squiz.net/training-resources/nobel/nobel.csv?id=339",
              "summary": null,
              "allSummaryText": null,
              "cacheUrl": null,
              "date": null,
              "fileSize": 0,
              "fileType": null,
              "tier": 0,
              "docNum": 0,
              "exploreLink": null,
              "kmFromOrigin": null,
              "listMetadata": { (6)
                "prizeName": [
                  "The Nobel Peace Prize"
                ],
                "year": [
                  "1962"
                ],
                "name": [
                  "Linus Carl Pauling"
                ]
              },
              "tags": [ ],
              "quickLinks": null,
              "displayUrl": "https://docs.squiz.net/training-resources/nobel/nobel.csv?id=339",
              "clickTrackingUrl": "https://docs.squiz.net/training-resources/nobel/nobel.csv?id=339",
              "explain": null,
              "indexUrl": "https://docs.squiz.net/training-resources/nobel/nobel.csv?id=339",
              "gscopesSet": [ ],
              "customData": { },
              "relatedDocuments": { },
              "documentVisibleToUser": true,
              "promoted": false,
              "diversified": false
            },
            {
              "rank": 0,
              "score": 0,
              "title": "https://docs.squiz.net/training-resources/nobel/nobel.csv?id=458",
...
1 When s.result.collapsed is not null and contains a data structure providing information about the collapsed results and a list of the individual results that have been collapsed into the main result.
2 s.result.collapsed.signature contains the collapsing signature that controls which grouping is applied. This value must be set in the ?: query language operator if you wish to view the set of collapsed results for this item.
3 s.result.collapsed.count indicates the total number of items that have been collapsed or grouped into the parent result.
4 s.result.collapsed.column indicates the set of fields that have be used to group the results.
5 s.result.collapsed.results is a list containing the set of results that are grouped beneath the parent search result, that can be displayed as a preview beneath the parent search result. The number of items listed is controlled by the collapsed_num_ranks and is not the same as the s.result.collapsed.count which indicates the number of items that were collapsed.
6 The listMetadata element contains a set of metadata fields that can be presented in the search results summary. The fields returned are controlled by the collapsing_SF option.

Results collapsing HTML

Results collapsing is usually incorporated into the search results pages in the following places.

Search results summary

The search results summary, which indicates the number of matching results, displays some additional text when result collapsing is enabled. This covers two cases - on the initial search which collapses the results beneath a parent result, and when viewing the set of collapsed results.

The logic from the default template is presented below:

<div>
  <if response.resultPacket.resultsSummary.totalMatching == 0>
    0 search results for {question.query}
  </if>
  <if response.resultPacket.resultsSummary.totalMatching != 0>
    {response.resultPacket.resultsSummary.currStart} - {response.resultPacket.resultsSummary.currEnd} of {response.resultPacket.resultsSummary.totalMatching?string.number}
    <if question.inputParameters["s"][0].contains("?:")>collapsed >search results for {question.query}</if> (1)
  </if>

  <if response.resultPacket.resultsSummary.partiallyMatching != 0>
    where {response.resultPacket.resultsSummary.fullyMatching} match all words and {response.resultPacket.resultsSummary.partiallyMatching match some words.
  </if>
  <if response.resultPacket.resultsSummary.collapsed != 0> (2)
    {response.resultPacket.resultsSummary.collapsed} very similar results included.
  </if>
</div>
1 If the system query field includes a special ?: operator then this indicates the search results are the set of results that were grouped beneath a parent result. In this case modify the result summary message to indicate this is a set of collapsed search results.
2 For the initial query (that groups the results beneath a parent result), we print an additional sentence indicating the set of results includes the collapsed results: N very similar results included at the end of the result summary. This message is printed if the response.resultPacket.resultsSummary.collapsed element contains a non-zero value, and the value indicates the number of collapsed results, which we also include in the message as the value of N.

Search result items

If a search result contains any items that are grouped beneath it then we print a link or button that enables the user to retrieve the set of results that have been grouped (or collapsed) beneath this result.

<if s.result.collapsed != null>
<h2>Preview of very similar results</h2>
  <list result.collapsed.results as r> (1)
   <p><a href="${r.indexUrl}">{r.listMetadata["firstName"][]} {r.listMetadata["lastName"][0]}</a></p> (2)
  </list>
<div><a href="?COLLAPSING-QUERY-STRING">{s.result.collapsed.count} very similar results</a></div> (3)
</if>
1 We want to list out a preview of the collapsed results. To do this we iterate over the result.collapsed.results element which contains the first N collapsed results (where N is determined by your collapsing_num_ranks setting).
2 For each item we print out selected data model elements for each collapsed result. Here we’re printing firstName and lastName metadata values.
3 We then print out a link to access the full set of collapsed results for this result item. The COLLAPSING-QUERY-STRING is built as follows:

Take the original query string, remove the start_rank parameter and add ?:{result.collapsed.signature}, fmo=on and collapsing=off

This modifies the query to return results from the start, limit the set of results to items that match the result.collapsed.signature value from the data model, to discard any partially matching results and to turn off result collapsing for the set of results (because you don’t want to collapse results when you view the full, uncollapsed, set).

This will result in a URL that looks something like: http://search.example.com/s/search.html?collection=example~sp-search-package-id&profile=example-results-page-id&query=example-query&collapsing=on&collapsing_sig=[example-field]&s=%3F:1E367DBB554395D7&fmo=on&s=%3F:1E367DBB554395D7&fmo=on&collapsing=off