z3by / vuepress-plugin-flexsearch

Next-Generation full text search library for Vuepress
MIT License
41 stars 16 forks source link

Add hook/callback on searches #36

Open KieranHunt opened 3 years ago

KieranHunt commented 3 years ago

Hey!

Thanks for the great library!

We have some analytics running on our VuePress site. Currently, we're just collecting page views but would like to extend that to collect search terms too. We'll use that to align our documentation to what people are actually searching for.

What are you feelings on adding a hook to this plugin that gets called on every search? That'll be a great extension point for us to emit our metrics.

I was thinking something like this:

[
  "flexsearch",
  {
    search_options: {
      encode: "balance",
      tokenize: "reverse",
      threshold: 0,
      resolution: 3,
      depth: 3,
      doc: {
        id: "key",
        field: ["title", "headers", "content"],
      },
    },
    callbacks: {
      onSearch: (someObjectRepresentingTheSearch) => {
        // emit metrics here
      }
    }
  },
]

I don't mind doing the work, I just wanted your input whether this is something you'd like in the library and what the API should look like.

Thanks!