Term Highlighting Search - Alfresco Content Services - 23.4 - 23.4 - Ready - Alfresco - external

Alfresco Content Services

Platform
Alfresco
Product
Alfresco Content Services
Release
23.4
License

The last example we’re going to look at is term highlighting. The example body below shows a search request that will look for content nodes that have a name or title starting with test, if the match occurs in either the cm:name or cm:title property the location of the match will be returned in the results. By default, the matched term is highlighted by surrounded by an em tag, to surround the match with something else the prefix and postfix properties can be used as shown in the example below:

{
  "query": {
    "query": "(name:\"test*\" OR title:\"test*\") AND TYPE:\"cm:content\""
  },
  "highlight": {
    "fields": [
      {
        "field": "cm:name",
        "prefix": "(",
        "postfix": ")"
      },
      {
        "field": "{http://www.alfresco.org/model/content/1.0}title"
      }
    ]
  }
}

Here is how the call looks like, assuming that we have stored the query JSON data in a file called highlight-query.json:

$ curl -X POST -H 'Content-Type: application/json' -H 'Accept: application/json' --header 'Authorization: Basic VElDS0VUXzIxYzAzOWMxNjFjYzljMDNmNmNlMzAwYzAyMDY5YTQ2OTQwZmYzZmM=' --data-binary '@highlight-query.json' 'http://localhost:8080/alfresco/api/-default-/public/search/versions/1/search' | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  7101    0  6786  100   315  56082   2603 --:--:-- --:--:-- --:--:-- 58685
{
  "list": {
    "pagination": {
      "count": 10,
      "hasMoreItems": false,
      "totalItems": 10,
      "skipCount": 0,
      "maxItems": 100
    },
    "context": {
      "consistency": {
        "lastTxId": 193
      }
    },
    "entries": [
      {
        "entry": {
          "isFile": true,
          "createdByUser": {
            "id": "admin",
            "displayName": "Administrator"
          },
          "modifiedAt": "2019-11-07T10:43:43.279+0000",
          "nodeType": "cm:content",
          "content": {
            "mimeType": "text/plain",
            "mimeTypeName": "Plain Text",
            "sizeInBytes": 9,
            "encoding": "UTF-8"
          },
          "parentId": "6b661ba4-830b-457d-af04-46f174351536",
          "createdAt": "2019-11-07T10:43:43.279+0000",
          "isFolder": false,
          "search": {
            "score": 1,
            "highlight": [
              {
                "field": "cm:name",
                "snippets": [
                  "(test)-file.txt"
                ]
              }
            ]
          },
          "modifiedByUser": {
            "id": "admin",
            "displayName": "Administrator"
          },
          "name": "test-file.txt",
          "location": "nodes",
          "id": "9613e418-b1c1-4889-8866-4dccda66a258"
        }
      },
      {
        "entry": {
          "isFile": true,
          "createdByUser": {
            "id": "admin",
            "displayName": "Administrator"
          },
          "modifiedAt": "2019-10-03T08:37:17.832+0000",
          "nodeType": "cm:content",
          "content": {
            "mimeType": "application/x-javascript",
            "mimeTypeName": "JavaScript",
            "sizeInBytes": 118,
            "encoding": "UTF-8"
          },
          "parentId": "fa62d870-061c-400f-a3c5-12a37fe0738e",
          "createdAt": "2019-10-03T08:37:17.832+0000",
          "isFolder": false,
          "search": {
            "score": 0.95773923,
            "highlight": [
              {
                "field": "cm:name",
                "snippets": [
                  "(test) return value.js.sample"
                ]
              }
            ]
          },
          "modifiedByUser": {
            "id": "admin",
            "displayName": "Administrator"
          },
          "name": "test return value.js.sample",
          "location": "nodes",
          "id": "6b0b7a4c-1630-47ec-9c2f-7620c1cefeed"
        }
      },
      {
        "entry": {
          "isFile": true,
          "createdByUser": {
            "id": "admin",
            "displayName": "Administrator"
          },
          "modifiedAt": "2019-10-03T08:37:17.743+0000",
          "nodeType": "cm:content",
          "content": {
            "mimeType": "application/x-javascript",
            "mimeTypeName": "JavaScript",
            "sizeInBytes": 2271,
            "encoding": "UTF-8"
          },
          "parentId": "fa62d870-061c-400f-a3c5-12a37fe0738e",
          "createdAt": "2019-10-03T08:37:17.743+0000",
          "isFolder": false,
          "search": {
            "score": 0.3308143,
            "highlight": [
              {
                "field": "cm:name",
                "snippets": [
                  "example (test) script.js.sample"
                ]
              },
              {
                "field": "{http://www.alfresco.org/model/content/1.0}title",
                "snippets": [
                  "Example <em>Test</em> Script"
                ]
              }
            ]
          },
          "modifiedByUser": {
            "id": "admin",
            "displayName": "Administrator"
          },
          "name": "example test script.js.sample",
          "location": "nodes",
          "id": "5d837ad9-4e7e-4eb9-9c41-1133f02c4cef"
        }
      },
      {
        "entry": {
          "isFile": true,
          "createdByUser": {
            "id": "admin",
            "displayName": "Administrator"
          },
          "modifiedAt": "2019-11-08T13:39:34.040+0000",
          "nodeType": "cm:content",
          "content": {
            "mimeType": "application/msword",
            "mimeTypeName": "Microsoft Word",
            "sizeInBytes": 205312,
            "encoding": "UTF-8"
          },
          "parentId": "d87e0641-e44a-4e42-a88c-6d9765a74600",
          "createdAt": "2019-11-08T13:30:41.839+0000",
          "isFolder": false,
          "search": {
            "score": 0.32019404,
            "highlight": [
              {
                "field": "cm:name",
                "snippets": [
                  "SampleTemplate-(test.doc)"
                ]
              }
            ]
          },
          "modifiedByUser": {
            "id": "admin",
            "displayName": "Administrator"
          },
          "name": "SampleTemplate-test.doc",
          "location": "nodes",
          "id": "6aa9d4ac-df1f-4c6d-9159-6f62821dd814"
        }
      },
      ...
    ]
  }
}

As we specified in the request, the match in the cm:name property is surrounded by brackets and the em tag surrounds the match in the cm:title property.