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

Hits Per Page Not Working as Expected? #46

Closed arhoy closed 4 years ago

arhoy commented 4 years ago

I am trying to limit the amount of hits or search results that algolia returns. I thought I could do the following inside the gatsby config.js file to limit the results to 8.

{
  resolve: 'gatsby-plugin-algolia',
  options: {
    appId: process.env.GATSBY_ALGOLIA_APP_ID,
    apiKey: process.env.GATSBY_ALGOLIA_ADMIN_KEY,
    indexName: process.env.GATSBY_ALGOLIA_INDEX_NAME,
    queries,
    hitsPerPage: 8,
    chunkSize: 10000,
  },

However, reducing that number does not seem to be doing anything. Am I missing something?

Haroenv commented 4 years ago

the hits per page is supposed to go in the settings key (in each query), does that fix it for you? The hitsPerPage where you put it isn't an option :)

arhoy commented 4 years ago

I am not sure what you mean by settings key in each query Do you have a code example or screenshot I can follow?

Thank you. Currently I am limiting like so, but I not sure if it is best practice

const ProductSearchPreview = ({ hit }) => {
  if (hit && hit.__position <= 9) {
    return (
      <Container>
        <CustomLink to={`/product/${hit.handle}`}>
          <h4>{hit.title}</h4>
        </CustomLink>
      </Container>
    );
  } else {
    return null;
  }
};
arhoy commented 4 years ago

I would prefer to limit it in some settings to limit my algolia index usage.

Haroenv commented 4 years ago
const queries = [
  {
    query: myQuery,
    transformer: ({ data }) => data.allSitePage.edges.map(({ node }) => node), // optional
    indexName: 'index name to target', // overrides main index name, optional
    settings: {
      // optional, any index settings
      hitsPerPage: 9
    },
  },
];

module.exports = {
  plugins: [
    {
      resolve: `gatsby-plugin-algolia`,
      options: {
        appId: process.env.ALGOLIA_APP_ID,
        apiKey: process.env.ALGOLIA_API_KEY,
        indexName: process.env.ALGOLIA_INDEX_NAME, // for all queries
        queries,
        chunkSize: 10000, // default: 1000
      },
    },
  ],
};

OR in the frontend:

<Configure hitsPerPage={9} />
arhoy commented 4 years ago

Thank you that is very helpful!