search-ui icon indicating copy to clipboard operation
search-ui copied to clipboard

BooleanFacet does not work with Elasticsearch connector

Open JasonStoltz opened this issue 3 years ago • 3 comments

Describe the bug See discussion here. https://discuss.elastic.co/t/search-ui-issues-with-elastic-search-connector-and-boolean-aggregations/312183/2

The BooleanFacet component does not work correctly with the Elasticsearch connector, because it's use of 0/1 values instead of true/false.

JasonStoltz avatar Aug 16 '22 14:08 JasonStoltz

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Is this issue still important to you? If so, please leave a comment and let us know. As always, thank you for your contributions.

botelastic[bot] avatar Oct 15 '22 14:10 botelastic[bot]

Here is a workaround for the APIConnector example from Search UI Next.js integration docs.

  async onSearch(requestState, queryConfig) {
    const response = await fetch("/api/search", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        requestState,
        queryConfig,
      }),
    });
    const { facets, ...rest } = await response.json();

    /**
     * Alter incoming facet data as a workaround for @elastic/search-ui issue.
     * See https://github.com/elastic/search-ui/issues/851
     */
    const alteredFacets = {
      ...facets,
      ...Object.fromEntries(
        Object.entries(facets)
          // Only process facets starting with "is".
          // Change this to suit the boolean field naming in your index!
          .filter(([facetName]) => facetName.startsWith("is"))
          .map(([facetName, facetValues]) => [
            facetName,
            facetValues?.map((facetValue) => ({
              ...facetValue,
              data:
                facetValue.data?.map((item) => {
                  // Map 0, 1 values to "false" and "true" strings.
                  if (item.value === 0) return { ...item, value: "false" };
                  if (item.value === 1) return { ...item, value: "true" };
                  return item;
                }) ?? [],
            })) ?? [],
          ])
      ),
    };

    return {
      ...rest,
      facets: alteredFacets,
    };
  }

MarttiR avatar Dec 13 '22 19:12 MarttiR

@MarttiR Thanks for posting this workaround, spent a while trying to get to the bottom why my boolean facets weren't working!

Hoping the workaround can be removed in a future release.

gaving avatar Dec 14 '23 08:12 gaving