Custom ranking using DocSearch

I’m trying to understand the use of custom ranking, but not getting the results as expected. This is likely incorrect configuration on my end, but wondering if I am also limited to the free version of DocSearch.

My crawler is configured with the following 2 actions:

actions: [
  {
    indexName: "layer0",
    pathsToMatch: ["https://docs.edg.io/", "https://docs.edg.io/guides/**"],
    recordExtractor: ({ url, $, helpers }) => {
      /**
       * @param {string | string[]} selectors
       */
      function article(selectors) {
        const prefix = "article.docs-article ";
        if (typeof selectors === "string") {
          selectors = [selectors];
        }

        return selectors.map((selector) =>
          selector
            .split(",")
            .map((s) => prefix + s.trim())
            .join(",")
        );
      }

      // guide version
      const version =
        $('meta[name="app:guide-version"]').attr("content") || "v7";

      // remove content from guides to not be indexed
      $(article(["div.callout", "div.code-block", "ol li"]).join()).remove();

      return helpers
        .docsearch({
          recordProps: {
            lvl0: {
              selectors: "",
              defaultValue: "Guides",
            },
            lvl1: article("h1"),
            lvl2: article("h2"),
            lvl3: article("h3"),
            lvl4: article("h4"),
            lvl5: article("h5"),
            lvl6: article("h6"),

            content: article([
              ".article-text strong",
              ".article-text",
              "ul li",
              "table td",
            ]),
          },
          aggregateContent: false,
          recordVersion: "v3",
        })
        .map((record) => {
          return {
            ...record,
            weight: { ...record.weight, guideRank: 1 }, // custom rank to show guides before API docs
            version,
          };
        });
    },
  },
  {
    indexName: "layer0",
    pathsToMatch: ["https://docs.edg.io/docs/**"],
    recordExtractor: ({ $, helpers }) => {
      const version =
        $('meta[name="description"]')
          .attr("content")
          .match(/(v\d+)/)[1] || "v7";

      const packagePath = $("header .title")
        .text()
        .replace(/[\s-]+v[\d.]+$/, "")
        .trim();

      $("div.col-menu").remove();

      return helpers
        .docsearch({
          recordProps: {
            lvl0: {
              selectors: "",
              defaultValue: `API | ${packagePath}`,
            },
            lvl1: ["div.tsd-page-title h1"],
            lvl2: ["h2", "section.tsd-index-section h3"],
            lvl3: [
              "h3 span",
              "section.tsd-index-section .tsd-index-list a span",
            ],
            content: ["div.tsd-comment p:not(:has(> strong))"],
          },
          aggregateContent: true,
          recordVersion: "v3",
        })
        .map((record) => ({
          ...record,
          weight: { ...record.weight, guideRank: 2 }, // custom rank to show guides before API docs
          version,
        }));
    },
  },
]

The intent with setting the weight.guideRank is so that when searching for a term such as “debug”, I want to display results under level 0 “Guides” before the “API” results.

In my initialIndexSettings, I have customRanking as:

customRanking: [
  "asc(weight.guideRank)",
  "desc(weight.pageRank)",
  "desc(weight.level)",
  "asc(weight.position)",
]

When I search for “debug”, they show with “API” before “Guides”.

Am I using custom ranking incorrectly? Under all cases except for when there are no results, Guides should display first.

@tlee Yes, it looks like you’re using it correctly. I suggest looking at two things:

  1. Check the ranking/sorting configuration in the index dashboard. It might be different from what you’re setting in the API. (NOTE: It’s unclear to me which settings take precedence, but I think the dashboard settings do. Ref: Ranking)
  2. Test your search results in the index dashboard and inspect the ranking criteria.
    1. In the index dashboard, enter a search term.
    2. Click View Details in a result.
    3. Mouse-over the “prize ribbon” icon to display the ranking criteria for that result. It will look like this:

algolia-ranking-criteria