#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
Jun 12, 2021 (32 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
&gt; 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.

Typesense

Lightning-fast, open source search engine for everyone | Knowledge Base powered by Struct.AI

Indexed 3015 threads (79% resolved)

Join Our Community

Similar Threads

Merging Results of Multi-Search Queries: Workarounds and Future Features

Julian wanted to merge results from two similar collections. Kishore Nallan confirmed it's a feature that might be implemented but hasn't been prioritised. Both Julian and robert plan to use client-side solutions for now.

12
16mo

Trouble in Implementing Deeply Nested Search

Anirudh is struggling to implement a two-level nested search. Jason asked for some specific examples to study the issue. Anirudh provided some material, realizing that adding top fields helped but might over-index. Jason then suggested reporting this issue on GitHub.

2

21
2d

Issues with Displaying Paragraphs Using Typesense and React-instantsearch

Mark is struggling with displaying only relevant paragraphs of indexed body text in a UI with react-instantsearch and Typesense. Jason suggests modifications to the TypesenseInstantSearchAdapter instantiation, highlighting only relevant fields. The issue remains unresolved.

2

7
1mo

Resolving Typesense Result Issue in Document Collection Queries

Mike was encountering errors when searching for specific query in their Typesense document collection. Jason suggested it may be due to the `drop_tokens_threshold` setting. There was a misunderstanding but after further explanation from Jason, Mike understood and decided to continue the conversation via email.

1

19
22mo

Phrase Search Relevancy and Weights Fix

Jan reported an issue with phrase search relevancy using Typesense Instantsearch Adapter. The problem occurred when searching phrases with double quotes. The team identified the issue to be related to weights and implemented a fix, improving the search results.

6

111
8mo