Flask routes and Algolia search

I’m putting a search interface on my Flask site with Algolia. My app.js file, which I’ve validated works on my index.html page, looks like this:

const searchClient = algoliasearch(
  '7YSIPT9QKL',
  '3ba3bc92bb97e00b9010742421c90e5a'
);

const search = instantsearch({
  indexName: 'rjh',
  searchClient,
  routing: true
});

search.addWidgets([
  instantsearch.widgets.searchBox({
    container: '#searchbox',
  }),
  instantsearch.widgets.hits({
    container: '#hits',
    templates: {
      item: `
      <div>
        <div class="hit-title">
          <a href="{{Id}}">{{#helpers.highlight}}{ "attribute": "Title" }{{/helpers.highlight}}</a>
        </div>
        <div class="hit-company">
          {{#helpers.highlight}}{ "attribute": "Company" }{{/helpers.highlight}}
        </div>
        <div class="hit-date">
          {{#helpers.highlight}}{ "attribute": "Date" }{{/helpers.highlight}}
        </div>
      </div>
`,
    },
  }),
  instantsearch.widgets.pagination({
    container: '#pagination',
  }),
]);


// After the `searchBox` widget
search.addWidgets([
  instantsearch.widgets.clearRefinements({
    container: '#clear-refinements',
  }),

  instantsearch.widgets.refinementList({
    container: '#company-list',
    attribute: 'Company',
  }),

  instantsearch.widgets.refinementList({
    container: '#date-list',
    attribute: 'Date',
  }),

  instantsearch.widgets.configure({
    hitsPerPage: 20
  }),
]);


search.start();

Now I’ve got a list of search results, I need to build the pages for each individual result. I built a route:

@app.route('/job/<posting>', methods=['POST'])
def job_post(posting):
    posting = {{Id}}
    return render_template('post_page.html', posting=posting)

Where ID is an attribute in my Algolia index. This throws an error because it doesn’t recognize what ID is. So, I’m not sure how to “connect” my Algolia index to this routes file. As an example for what I’m trying to do:

  1. User comes to my site and searches for an accountant job
  2. My Algolia index gets searches for accountant job and returns results
  3. Each result has a link to a page with the corresponding job, like mysite.com/job/accountingjob
  4. The Algolia index has an attribute called “Id” that needs to be passed to the route/URL so a job description page can be rendered. What I can’t figure out is how to pass that attribute to the route.

To clarify this request: I’ve got 10k records in an Algolia index. I need to create a page on my website for each one of those records. One attribute in my my index is Id. The structure of the URL for each page should be mysite.com/<Id>.

What I’m asking is how to build a Flask route that that pulls the Id off of each record in the Algolia index to build a page for each record.

Hi Eric, you can get the objectID or any other attribute that was retrieved in the hits widget template.