algolia / gatsby-plugin-algolia

A plugin to push to Algolia based on graphQl queries
https://yarn.pm/gatsby-plugin-algolia
Apache License 2.0
177 stars 45 forks source link

Multiple indices #8

Closed sedubois closed 6 years ago

sedubois commented 6 years ago

Thanks for this plugin, it's quite helpful. For multi-language sites it's mentioned that one index might be needed per language, however the plugin seems to accept a single indexName. Could it be enhanced to support that scenario?

Haroenv commented 6 years ago

What you can do is instantiate the plugin multiple times, each time with the same configuration, but a separate index. Does that make sense?

sedubois commented 6 years ago

OK I see, thanks I will try that.

Haroenv commented 6 years ago

Please reopen if the suggested solution doesn't fit the use case you were thinking of!

Haroenv commented 6 years ago

I just forgot that you can also specify the indexName per query :)

sedubois commented 5 years ago

Hello @Haroenv, I managed to generate the queries for the different locales as below. According to the README there's no way to pass GraphQL variables so I had to use string interpolation in the GraphQL query which isn't very good practice for GraphQL (it works here because we're just passing a plain String to algolia). It would be nice if GraphQL variables were supported.

NB: I can't reopen the issue myself.

// src/utils/algolia.js
module.exports = locale => {
  const query = `
  {
    seos: allDatoCmsSeo(filter: { locale: { eq: "${locale}" } }) {
      edges {
        node {
          title
          fields {
            path
          }
          description
        }
      }
    }
  }`;

  const transformer = function(chunksTotal, { node }) {
    const {
      title,
      fields: { path },
      description
    } = node;

    const html = "<html></html>"; // TODO actually load the HTML from the different content types
    const noEmojiContent = html.replace(/<img class="emoji-icon".+\/>/g, "");

    const contentChunks = chunkString(noEmojiContent, 5000);
    const record = { title, path, description };
    const recordChunks = contentChunks.reduce((recordChunksTotal, contentChunksItem, idx) => {
      return [
        ...recordChunksTotal,
        { ...record, ...{ content: contentChunksItem }, objectID: `${path}${idx}` }
      ];
    }, []);

    return [...chunksTotal, ...recordChunks];
  };

  return [
    {
      query,
      transformer: ({ data }) => {
        return data.seos.edges.reduce(transformer, []);
      }
    }
  ];
};

// gatsby-config.js
const algoliaQueries = require("./src/utils/algolia");
const locales = ["en", "fr", ...];

  plugins: [
    ...
    ...locales.map(locale => ({
      resolve: `gatsby-plugin-algolia`,
      options: {
        appId: process.env.ALGOLIA_APP_ID || "",
        apiKey: process.env.ALGOLIA_ADMIN_API_KEY || "",
        indexName: process.env.ALGOLIA_INDEX_NAME
          ? `${process.env.ALGOLIA_INDEX_NAME}-${locale}`
          : "",
        queries: algoliaQueries(locale),
        chunkSize: 10000 // default: 1000
      }
    })),
    ...
  ]
Haroenv commented 5 years ago

Hmm, good call. I don’t know exactly how that would be possible, but I’ll look into it. Instead of reopening this issue, can you open a new one with your snippet about the query variables, and the code you’d expect to work with those variables too? Thanks!

brotzky commented 5 years ago

I've run into the same issue. I would love to pass in query variables, but currently the plugin doesn't allow it. https://github.com/algolia/gatsby-plugin-algolia/blob/master/gatsby-node.js#L42

Here's an example of how variables would work https://github.com/gatsbyjs/gatsby/issues/4050#issuecomment-365887066

I think it would be as simple as adding another key to the index object and passing it through. But there are more creative solutions if that's too basic.

Forgive the naming, but the basic goal would be

      {
        query,
        queryVariables,
        transformer,
        indexName,
      },
const result = await graphql(query, queryVariables);
Haroenv commented 5 years ago

Would you be willing to make a PR to have the possibility to add query variables @brotzky?

brotzky commented 5 years ago

@Haroenv Hopefully soon :)