Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight for hierarchical fields not working #1343

Closed
ThomasJejkal opened this issue Feb 17, 2024 · 6 comments · Fixed by #1344
Closed

Highlight for hierarchical fields not working #1343

ThomasJejkal opened this issue Feb 17, 2024 · 6 comments · Fixed by #1344

Comments

@ThomasJejkal
Copy link

The problem

I'm currently evaluating SearchKit and struggle with the highlighting of matches. According to the documentation, it should work intuitively, but if I follow the documentation, my highlight component never contains any value for hierarchical fields. The only chance is to modify the result by storing the highlight result in an attribute without hierarchy separator. For more information, please see the details section. I've already opened an issue at algolia/instantsearch but I they send me here.

Environment

  • Searchkit version (or git revision) that exhibits the issue: 4.11.0
  • Last Searchkit version that did not exhibit the issue (if applicable): No idea
  • Node.js version: 18.18.0
  • Typescript version:
  • Next.js version:
  • ElasticSearch/OpenSearch version: 7.17.18 / 8.5.1 (same result)

Details

According to the documentation, using the highlight component for hierarchical fields should work for the following example hit:

{
   "_index": "test_index",
   "_id": "4b4876e1-5749-4d5f-95b6-2251681c96e1",
   "_score": 1.0,
   "_source": {
      "metadata": {
         "publisher": "Albert Einstein",
         "publicationYear": "2023"
       }
   },
   "highlight": {
      "metadata.publisher": [
         "<em>Albert</em> Einstein"
      ]
    }
 }

as follows:

<Highlight attribute="metadata.publisher" hit={hit}/>

Instead, the highlight component is is rendered but empty. If I change the attribute key as follows:

hit._highlightResult['test'] = hit._highlightResult['metadata.publisher'];

the following works:

<Highlight attribute="test" hit={hit}/>
@joemcelroy
Copy link
Member

Hiya could you share the configuration aswell? Maybe there’s a bug here with object fields

@ThomasJejkal
Copy link
Author

I'm afraid it's too simple to contain any bugs, but let's have a look:

const sk = new Searchkit({
  connection: {
    host: "http://localhost:9200",
  },
  search_settings: {
    search_attributes: ['metadata.titles.value', 'metadata.publisher'],
    result_attributes: ['metadata.titles.value', 'metadata.publisher'],
    highlight_attributes: ['metadata.publisher']
  }
})

Maybe for completeness, the rest of the code:

const searchClient = Client(sk);

function HitComponent({ hit }) {
    return <div>
        <Highlight attribute="metadata.publisher" hit={hit} />
    </div>;
}

function App() {
  return (
      <InstantSearch indexName="test_index" searchClient={searchClient}>
          <Configure attributesToHighlight={['metadata']} /> {/* This can also be done in the Dashboard */}
          <SearchBox />
          <Hits hitComponent={HitComponent}/>
      </InstantSearch>
  );
}

Adding the Configure-Component was a hint I received from the Algolia guys, but it had no effect.

@joemcelroy
Copy link
Member

could you try this with another field thats not using object field? IE, make 'metadata.publisher just publisher and try? When I have some time, will create a test case for this if it works to figure out the object issue.

@ThomasJejkal
Copy link
Author

I've already checked this yesterday and I just realized that I did not submit my comment. -.-
As expected, with a simple key it is working as intended as the response is similar to my 'hack' where I modified the result via

hit._highlightResult['test'] = hit._highlightResult['metadata.publisher'];

@joemcelroy
Copy link
Member

sounds like a bug within Searchkit for highlighting on object fields. This is the code for highlighting and tests. Likely will take me next weekend to look into this but very happy for a PR to resolve the issue if you can.

Joe

@joemcelroy
Copy link
Member

found the issue that instantsearch requires object fields to be represented in a object hierarchy, not currently as a 1 depth dot notation keys. Fixed this by transforming ES dot notation keys into an object hierarchy.

Got a PR fixing this and should work. Let me know if you have issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants