typesense-api-spec icon indicating copy to clipboard operation
typesense-api-spec copied to clipboard

Add type for highlight field

Open jackwilsdon opened this issue 2 years ago • 1 comments

Change Summary

This PR introduces a new SearchHighlightV2 type which is used for the SearchResultHit#highlight field.

I've opened this as a draft as this version of the spec doesn't quite play nicely with the generator used by typesense-go (it creates some weirdly named structs). The generated Go version can be a bit nicer by making these changes:

--- openapi.yml
+++ openapi.yml
@@ -1465,9 +1465,7 @@
           additionalProperties:
             oneOf:
               - $ref: "#/components/schemas/SearchHighlightV2"
-              - type: array
-                items:
-                  $ref: "#/components/schemas/SearchHighlightV2"
+              - $ref: "#/components/schemas/SearchHighlightV2Array"
         document:
           type: object
           description: Can be any key-value pair
@@ -1560,6 +1558,10 @@
             type: string
           example:
             - Stark
+    SearchHighlightV2Array:
+      type: array
+      items:
+        $ref: "#/components/schemas/SearchHighlightV2"
     SearchOverrideSchema:
       type: object
       required:

Annoyingly, the Go version still doesn't offer a way to detect if the oneOf is an array or not, instead throwing an error if you call the wrong AsSearchHighlightV2* function.

But introducing an additional schema seems less than ideal. I'm open to any suggestions on how to improve this further.

PR Checklist

jackwilsdon avatar Sep 19 '23 14:09 jackwilsdon

Looks like this doesn't work for object fields, as they have two levels of nested objects:

{
  "highlight": {
    "my_object_field": {
      "my_matching_key_1": {
        "matched_tokens": [ "SEARCH TERM" ],
        "snippet": [ "<mark>SEARCH TERM</mark>" ]
      },
      "my_matching_key_2": {
        "matched_tokens": [ "SEARCH TERM" ],
        "snippet": [ "<mark>SEARCH TERM</mark>" ]
      }
    }
  }
}

jackwilsdon avatar Sep 19 '23 14:09 jackwilsdon