Hey everyone, what’s the reasoning behind the java...
# community-help
d
Hey everyone, what’s the reasoning behind the javascript client returning highlights in an array instead of an object:
Copy code
"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?
Copy code
"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:
Copy code
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:
Copy code
<p>{{ result.highlights.content ?? result.document.content }}</p>
k
Would the highlights ever include more than one highlight per field?
When you are querying multiple fields, each matching field is highlighted.
d
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?
k
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.
d
For instance this would be the preferred response to make things easier in Javascript:
Copy code
"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"
          }
        }
      ]
    }
k
You could write a helper function that converts the array of highlights to a hashmap of
field_name -> highlight
d
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
Especially if the results are refreshing on every keystroke, the frontend will have to run the helper function a lot
k
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.
Unfortunately it might be too late now to change the structure, but at one point, this structure was actually debated internally.
I do agree with you that the hashmap structure will be far more convenient in Javascript.
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.
d
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.
k
Glad to hear 🙂 Let me know if you run into any gotchas. And what we can do to improve Typesense further.