Thumbnail

How to upgrade Sajari JS-SDK from version 1.x.x to 2.x.x

Thumbnail

Feb Dao

|

Sajari is a cloud-based, instant search engine for websites and ecommerce. Powered by machine learning and the customer's own data, it gets users to what they need, fast.

Update by npm

First of all, we need to update to newest version by running

npm install --save @sajari/sdk-js@next

Update search function

The search function is the most important function of sajari, it has been changed the way to use from version 1.x.x to version 2.x.x

This is an example how it looks with version 1.x.x

import { Client, DefaultSession, TrackingType, etc... } from "@sajari/sdk-js";

const websitePipeline = new Client("<project>", "<collection>").pipeline(
  "website"
);

websitePipeline.search(
  { q: "FAQ" },
  (error, response, values) => {
    // Check for error
    response.results.forEach((r) => {
      const title = document.createElement("a");
      title.textContent = r.values.title;
      title.href = r.values.url;
      title.onmousedown = () => {
        title.href = r.token.click;
      };

      document.body.appendChild(title);
    });
  }
);

As you can see that, the search function includes response handling right inside it.

But this is how it looks on version 2.x.x

import { Client } from "@sajari/sdk-js";

const websitePipeline = new Client("<project>", "<collection>").pipeline("website");

websitePipeline.search(
  { q: "FAQ" },
).then(([response, values]) => {
    response.results.forEach((r) => {
      const title = document.createElement("a");
      title.textContent = r.values.title;
      title.href = r.values.url;
      title.onmousedown = () => {
        title.href = r.token.click;
      };

      document.body.appendChild(title);
    });
})
 .catch((error) => {
  // Handle error...
});

On version 2.x.x, handling response has been moved out of search function.

Tracking event

Version 1.x.x

import { Client, DefaultSession, TrackingType, etc... } from "@sajari/sdk-js";
const websitePipeline = new Client("<project>", "<collection>").pipeline(
  "website"
);

const clickTrackedSession = new InteractiveSession(
  "q",
  new DefaultSession(TrackingType.Click, "url", {})
);

websitePipeline.search(
  { q: "FAQ" },
  clickTrackedSession,
  (error, response, values) => {
    // Check for error
    response.results.forEach((r) => {
      const title = document.createElement("a");
      title.textContent = r.values.title;
      title.href = r.values.url;
      title.onmousedown = () => {
        title.href = r.token.click;
      };

      document.body.appendChild(title);
    });
  }
);

Version 2.x.x

import { Client, SearchIOAnalytics } from "@sajari/sdk-js";

const pipeline = new Client("<account_id>", "<collection_id>").pipeline("app");
const searchio = new SearchIOAnalytics("<account_id>", "<collection_id>");

const values = { q: "puppies" };
pipeline
  .search(values, { type: "EVENT", field: "id" })
  .then(([response, values]) => {
    searchio.updateQueryId(response.queryId);
    response.results.forEach((r) => {
      const item = document.createElement("a");
      item.textContent = r.values.name;
      item.href = r.values.url;
      item.onclick = () => {
        searchio.track("click", r.id);
      };

      document.body.appendChild(item);
    });
  })
  .catch((error) => {
    // Handle error...
  });

 

Documentation

Version 1: https://github.com/sajari/sdk-js/tree/v1

Version 2: https://github.com/sajari/sdk-js

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Comments

  • Allowed HTML tags: <em> <strong> <cite> <blockquote cite> <ul type> <ol start type> <li> <dl> <dt> <dd> <p>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
  • Use [gist:#####] where ##### is your gist number to embed the gist
    You may also include a specific file within a multi-file gist with [gist:####:my_file].

Spread the word