DomDew / strapi-plugin-fuzzy-search

Register a weighted fuzzy search endpoint for Strapi Headless CMS you can add your content types to in no time

Home Page:https://www.npmjs.com/package/strapi-plugin-fuzzy-search

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Way to query an object, or specify the keys to query?

maximelebreton opened this issue · comments

Hello, and thank you for this plugin!
I liked the easy way to setup!

I'm using an alternative Wysiwyg editor (editorjs),
and here is an example of how my content si stored in the pages model:

"pages": {
    "data": [
      {
        "attributes": {
          "title": "Hello title",
          "header": {
            "excerpt": "Hey i'm an excerpt"
          },
          "body": "{\"time\":1673535413146,\"blocks\":[{\"id\":\"HRvCGL8hBa\",\"type\":\"paragraph\",\"data\":{\"text\":\"Hey. Meet the new Editor. On this page you can see it in action — try to edit this text.\"}},{\"id\":\"sMdH3YdPVa\",\"type\":\"paragraph\",\"data\":{\"text\":\"Workspace in classic editors is made of a single contenteditable element, used to create different HTML markups. Each of them is an independent contenteditable element (or more complex structure) provided by Plugin and united by Editor's Core.\"}}],\"version\":\"2.26.4\"}"
        }
      }
    ]
  }

(title is a string, excerpt is a string but a child of the header object, and body is a stringified object)

And here is my config:

"fuzzy-search": {
    enabled: true,
    config: {
      contentTypes: [
        {
          uid: "api::page.page",
          modelName: "page",
          transliterate: false,
          queryConstraints: {
            where: {
              $and: [
                {
                  publishedAt: { $notNull: true },
                },
              ],
            },
          },
          fuzzysortOptions: {
            characterLimit: 300,
            threshold: -600,
            limit: 10,
            keys: [
              {
                name: "title",
                weight: 100,
              },
              {
                name: "header",
                weight: 100,
              },
              {
                name: "body",
                weight: 50,
              },
            ],
          },
        },
      ],
    },
  },

✔️ So when my graphql query is "Hello" (a word in the title field), everything working as excepted:

query {
  search(query: "Hello") {
    pages {
      data {
        attributes {
          title
          header {
            excerpt
          }
          body
        }
      }
    }
  }
}

❌ But if the query is "Hey" (a word in the excerpt field), it should be returns some results, but I have empty results.

query {
  search(query: "Hey") {
...

❌ And if the query is "Editor" (a word in the body field), it should be returns some results, but I have empty results.

query {
  search(query: "Editor") {
...

So I don't understand why and wondering how can I query the excerpt and body fields?

Also, is there a way to create a custom function to define how to query a specific field?

Thanks for your help!

Hey hey @maximelebreton ,

a fuzzy search can yield some weird results, especially when you search in longer strings - depending on the underlying algorithm. This library uses fuzzysort under the hood, as for me it yielded the best and most consistent results for searching in longer strings that a CMS like Strapi oftentimes provides (like long description texts and the like).

I suggest you start by lowering the threshold in your config file to see if you get consistent results that you expect as well as dropping the characterLimit entirely and only introduce it as soon as performance is becoming an issue.

Let me know should you still have issues!

thanks @DomDew, I lowered the threshold & remove the characterLimit and it's working!

@maximelebreton Awesome! Glad I could help :)