nextapps-de / flexsearch

Next-Generation full text search library for Browser and Node.js
Apache License 2.0
12.54k stars 492 forks source link

How to Search from a Document Index #399

Closed bmccorm2 closed 10 months ago

bmccorm2 commented 1 year ago

I can't get any results coming from my document index. Do you know what i am doing wrong?

import Flexsearch from "flexsearch";

const documents = [
  {
    id: 1,
    name: 'person one',
    title: 'first place'
  },
  {
    id: 2,
    name: 'person two',
    title: 'second place'
  },
  {
    id: 3,
    name: 'person three',
    title: 'second place'
  },

];

const Document = Flexsearch.Document ?? Flexsearch;
const index = Document({
  tokenize: 'forward',
  document: {
    id: 'id',
    index: ['name', 'title'],
  },
});

documents.forEach((e) => {
  index.add({
    id: e.id,
    name: e.name,
    title: e.title
  });
});

export const search = (query: string) => {
  const results = index.search(query, ['name', 'title']);

  return results.map((e: number) => {
    return documents[e];
  });
};
ts-thomas commented 10 months ago

You aren't using a flat representation of the result, so this will break return documents[e];

Read here about the result set: https://github.com/nextapps-de/flexsearch?tab=readme-ov-file#the-result-set