#community-help

JavaScript Client's Return of Highlights Inquiry

TLDR Daniel questioned why the JavaScript client returned highlights in an array instead of an object. Kishore Nallan explained it was due to specific issues with statically typed languages needing defined JSON structures for parsing.

Powered by Struct AI
14
27mo
Solved
Join the chat
Jun 12, 2021 (27 months ago)
Daniel
Photo of md5-a95c873d292f586581d5716882ae24b5
Daniel
11:19 AM
Hey everyone, what’s the reasoning behind the javascript client returning highlights in an array instead of an object:

"hits": [
        {
          "highlights": [
            {
              "field": "company_name",
              "snippet": "<mark>Stark</mark> Industries"
            }
          ],
          "document": {
            "id": "124",
            "company_name": "Stark Industries",
            "num_employees": 5215,
            "country": "USA"
          }
        }
]

Would the highlights ever include more than one highlight per field?

"highlights": [
            {
              "field": "content",
              "snippet": "<mark>Hawaii</mark> is the big wave island."
            },
            {
              "field": "content",
              "snippet": "for the <mark>Hawaii</mark> surf competition."
            }
          ],

This makes the frontend code a lot more complex as you need to parse the array and see if the field matches:

PSEUDO CODE

temp = result.highlights.filter(el => { 
   if el.field === my-field-name 
})

if temp.length === 0
// print highlight

// if temp.length > 1
// print first instance or list all highlights for this field

// else print document.content

However if the highlights was returned as an object it would be a lot easier on the frontend:

<p>{{ result.highlights.content ?? result.document.content }}</p>
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
11:21 AM
> Would the highlights ever include more than one highlight per field?

When you are querying multiple fields, each matching field is highlighted.
Daniel
Photo of md5-a95c873d292f586581d5716882ae24b5
Daniel
11:22 AM
I mean specifically for the same field. I don’t mind if “title, synopsis, content” all have highlights… but would content ever have two highlights if its a long document?
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
11:23 AM
No only one highlight per field. I think we chose this format because a dictionary did not work well in statically typed languages in a nested structure.
Daniel
Photo of md5-a95c873d292f586581d5716882ae24b5
Daniel
11:24 AM
For instance this would be the preferred response to make things easier in Javascript:

      "hits": [
        {
          "highlights": {
              "company_name": {
                  "snippet": "<mark>Stark</mark> Industries",
                  ...
              },
              "content": {
                  "snippet": "<mark>Hawaii</mark> is the big wave island.",
                  ...
              },
          },
          "document": {
            "id": "124",
            "company_name": "Stark Industries",
            "num_employees": 5215,
            "country": "USA"
          }
        }
      ]
    }
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
11:24 AM
You could write a helper function that converts the array of highlights to a hashmap of field_name -&gt; highlight
Daniel
Photo of md5-a95c873d292f586581d5716882ae24b5
Daniel
11:25 AM
Yeah, just wondering what the reasoning was… I thought maybe it was a speed thing to make the server faster and offload work to the frontend
11:26
Daniel
11:26 AM
Especially if the results are refreshing on every keystroke, the frontend will have to run the helper function a lot
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
11:26 AM
I remember running into a specific issue with statically typed languages where we need to define the types of the JSON structures for the parsing.
11:26
Kishore Nallan
11:26 AM
Unfortunately it might be too late now to change the structure, but at one point, this structure was actually debated internally.
11:27
Kishore Nallan
11:27 AM
I do agree with you that the hashmap structure will be far more convenient in Javascript.
11:28
Kishore Nallan
11:28 AM
If it helps: on such a small array, a linear search will be as fast as a hashmap look up 🙂 So I think performance impact will be negligible.
Daniel
Photo of md5-a95c873d292f586581d5716882ae24b5
Daniel
11:29 AM
Okay no worries, I’m going to keep playing with it. I’ve been testing MeiliSearch too but we found they had an issue searching for the word *Ho-oponopono* in my documents so I’m quite impressed that Typesense finds it.
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
11:32 AM
Glad to hear 🙂 Let me know if you run into any gotchas. And what we can do to improve Typesense further.