travis-r6s / gridsome-plugin-flexsearch

Add lightning fast search to Gridsome with FlexSearch
24 stars 7 forks source link

Wrong result index when searching in more than one collection? #34

Closed ghost closed 4 years ago

ghost commented 4 years ago

I want to search in more than one collection - for example:

use: 'gridsome-plugin-flexsearch',
options: {
collections: [
    {
      typeName: 'Faqs',
      indexName: 'Faqs',
      fields: ['question', 'answer']
    },
    {
      typeName: 'Topics',
      indexName: 'Topics',
      fields: ['title', 'content']
    }
],
searchFields: ['question', 'answer', 'title']
}}

The wrong results are returned

e.g. item with id=3, but the wrong collection

 { "index": "Faqs", "id": "3",

instead of

{ "index": "Topics", "id": "3",

Any idea?

travis-r6s commented 4 years ago

So just to ensure I understand you correctly - in your data, there could potentially be two items with the id 3, but have different types...?

You should be able to specify a stricter search if needed, using https://github.com/nextapps-de/flexsearch#where, so you could specify that index = Topics.

ghost commented 4 years ago

So just to ensure I understand you correctly - in your data, there could potentially be two items with the id 3, but have different types...?

yes, we use Directus CMS as data source The IDs of the nodes are assigned individually per collection. Therefore there are always nodes with the same ID of a certain collection.

You should be able to specify a stricter search if needed, using https://github.com/nextapps-de/flexsearch#where, so you could specify that index = Topics.

Is this possible, using you plugin?

travis-r6s commented 4 years ago

@Schoell It is, yes - you will just need to update your search function. I.e:

// https://github.com/nextapps-de/flexsearch#combine-fuzzy-search-with-a-where-clause
this.$search.search(searchTerm,  {
    where: {
        "index": "some-index"
    },
    limit: 10
})

To add any specific filters as required.

ghost commented 4 years ago

The problem is the same ID in the search index. That means there is a FAQ with ID 1 and a TOPIC with ID 1. To solve the problem, the IDs must be extended by the collection type.

I.e. FAQ with ID FAQ1 and TOPIC with ID TOPIC1.

In the gridsome.server.js in line 56 I have extended the IDs. With this it works.

  const doc = {
    index: index.indexName,
    id: node.id+index.indexName,
    path: node.path,
    ...docFields
  }
travis-r6s commented 4 years ago

Ah, I get your problem now. Could you use onCreateNode to edit your id's as above?

PeterSchoell commented 4 years ago

Ah, I get your problem now. Could you use onCreateNode to edit your id's as above?

No, because we create the nodes using the Directus Plugin. But with the modification above it works fine.

travis-r6s commented 4 years ago

Even when using a source plugin, you can use the onCreateNode api to edit nodes - but if your solution works, then that's fine.