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

Flatten results #272

Closed robokozo closed 2 years ago

robokozo commented 3 years ago

I'm use flexsearch to do some web client side filtering of some objects. While I appreciate getting the search results broken down by the object field that matches, it is often unnecessary in my use cases and I find myself having to flatten and filter my results down to a unique set. I feel like performance can be improved when searching in this proposed mode because flexsearch can ignore already matched items.

Thanks!

pie6k commented 3 years ago

I have the same issue: https://codesandbox.io/s/flexsearch-search-tests-forked-40gk9?file=/src/index.ts

It is not even about performance, but how do I sort those 2 independent arrays of results, if there is no score included in results?

peterbe commented 3 years ago

I have the same issue: https://codesandbox.io/s/flexsearch-search-tests-forked-40gk9?file=/src/index.ts

It is not even about performance, but how do I sort those 2 independent arrays of results, if there is no score included in results?

What I would do is to make 2 indexes. E.g.

const indexContent = new Document({
  id: "id",
  index: ["content"]
});
const indexLol = new Document({
  id: "id",
  index: ["lol"]
});
...

for (const message of messages) {
  indexContent.add(message);
  indexLol.add(message);
}

...

const result = [
  ...indexContent.search({
    query: input,
    index: ["content",]
  }), 
  ...indexLol.search({
    query: input,
    index: ["lol"]
  })
]

Now, that result will look something like this: [ID10, ID123, ID10, ID55] so you as turn this back from identifiers to documents, you use a Set to keep track of which ones you've seen.

ts-thomas commented 2 years ago

You can also use just one document index. After getting back the result you can merge the fields like the example above.