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

Failed to index to Algolia #108

Closed calaca closed 3 years ago

calaca commented 3 years ago

I am trying to switch from the old gatsby-plugin-algolia-search plugin to the current version of gatsby-plugin-algolia but I always get the following error if there is any partial update to be done:

[Algolia] 2 queries to index
[Algolia] Running 1 query for index Posts...
[Algolia] Running 1 query for index Downloads...
[Algolia] Query resulted in a total of 4 results
[Algolia] Query resulted in a total of 12 results
success Generating image thumbnails - 0.038s - 5/5 131.32/s
[Algolia] Starting Partial updates...
error failed to index to Algolia

  ReferenceError: curObj is not defined

not finished onPostBuild - 0.513s

I am using WordPress to get my data. Here's my config:

gatsby-config.js

...
{
      resolve: `gatsby-plugin-algolia`,
      options: {
        appId: process.env.GATSBY_ALGOLIA_APP_ID,
        apiKey: process.env.ALGOLIA_ADMIN_KEY,
        indexName: process.env.GATSBY_ALGOLIA_POSTS_INDEX_NAME,
        queries: require('./src/utils/algoliaQueries'),
        chunkSize: 10000,
        enablePartialUpdates: true,
      },
    },
...
./src/utils/algoliaQueries.js

const postsQuery = `{
  allWpPost(
      filter: { status: { eq: "publish" } }
      sort: { fields: date, order: DESC }
    ) {
      nodes {
        objectID: id
        title
        slug
        excerpt
        date_timestamp: date
        categories {
          nodes {
            name
          }
        }
        tags {
          nodes {
            slug
            name
          }
        }
        featuredImage {
          node {
            altText
            localFile {
              childImageSharp {
                fluid(maxWidth: 390, quality:  90) {
                  base64
                  aspectRatio
                  src
                  srcSet
                  sizes
                }
              }
            }
          }
        }
        author {
          node {
            slug
            name
          }
        }
      }
    }
  }
`;

const downloadsQuery = `{
  allWpDownload(
      filter: { status: { eq: "publish" } }
      sort: { fields: date, order: DESC }
    ) {
      nodes {
        objectID: id
        title
        slug
        downloadFields {
          type
          sourceLink
        }
        date_timestamp: date
        categories {
          nodes {
            name
          }
        }
        featuredImage {
          node {
            altText
            localFile {
              childImageSharp {
                fluid(maxWidth: 390) {
                  base64
                  aspectRatio
                  src
                  srcSet
                  sizes
                }
              }
            }
          }
        }
      }
    }
  }
`;

const flattenPost = (arr) =>
  arr.map(({ excerpt, date_timestamp, ...rest }) => ({
    excerpt: excerpt.replace(/(<([^>]+)>)/gi, ''),
    date_timestamp: parseInt(
      (new Date(date_timestamp).getTime() / 1000).toFixed(0),
      10
    ),
    ...rest,
  }));

const flattenDownload = (arr) =>
  arr.map(({ date_timestamp, ...rest }) => ({
    date_timestamp: parseInt(
      (new Date(date_timestamp).getTime() / 1000).toFixed(0),
      10
    ),
    ...rest,
  }));

const queries = [
  {
    query: postsQuery,
    transformer: ({ data }) => flattenPost(data.allWpPost.nodes),
    indexName: process.env.GATSBY_ALGOLIA_POSTS_INDEX_NAME,
    settings: {
      attributesToSnippet: [`excerpt:20`],
    },
  },
  {
    query: downloadsQuery,
    transformer: ({ data }) => flattenDownload(data.allWpDownload.nodes),
    indexName: process.env.GATSBY_ALGOLIA_DOWNLOADS_INDEX_NAME,
  },
];

module.exports = queries;

If there is no data to be updated, everything goes well. I haven't found anything about the ReferenceError: curObj is not defined that could be helpful. Did I miss any config setting?

calaca commented 3 years ago

Nevermind, I've managed to make it work by adding the modified field into both queries and adding the matchFields setting to the plugin settings passing modified in the array.