meilisearch/demos

Only return array items that contain matches

kiastorm opened this issue · 0 comments

I have a situation where I have an array field, Content, that I'm searching for a search term.

example:

[
  {
    Content: [
      "hello there",
      "testing",
      "want to say hello"
    ]  
  },
  {
    Content: [
      "some other string"
    ]
  }
]

If my search term is hello, I want this to return

[
  {
    Content: [
      "<mark>hello</mark> there",
      "want to say <mark>hello</mark>"
    ]  
  },
]

Essentially, I want only the array items that actually match to be returned

Currently, I'm filtering through on the client side, but this invalidates the totalResults and totalPages

// Leave out any pages that do not contain highlighted matches
for (const hit of searchResults.hits) {
  const formattedHit = {
    ...hit,
    _formatted: {
      ...hit._formatted,
      Content: [],
    },
  };

  for (const page of hit._formatted.Content) {
    if (page.includes("<mark>")) {
      formattedHit._formatted.Content.push(page);
    }
  }

  if (formattedHit._formatted.Content.length) {
    formattedHits.push(formattedHit);
  }
}

Is this possible already? Any suggestions?