ChemPal Documentation - v1.6.0
    Preparing search index...
    • Translates a SearchAst into a generic boolean CONTAINS filter tree of the form { and|or|not | term: { field, op: "CONTAINS", values } }. Used by GraphQL suppliers that accept this structure server-side, so the boolean matching happens on the backend in a single request.

      Parameters

      • ast: SearchAst

        The parsed query tree.

      • wrapValue: (value: string) => string = ...

        Maps a term value to the stored value (e.g. add *…* wildcards for Wix). Defaults to identity.

      • field: string = 'name'

        The field name to match against. Defaults to "name".

      Returns ContainsFilterNode

      A CONTAINS filter node.

      translateAstToContainsFilter({ type: "or",
      left: { type: "term", value: "foo", phrase: false },
      right: { type: "term", value: "bar", phrase: false } });
      // { or: [
      // { term: { field: "name", op: "CONTAINS", values: ["foo"] } },
      // { term: { field: "name", op: "CONTAINS", values: ["bar"] } },
      // ] }
      export function translateAstToContainsFilter(
      ast: SearchAst,
      wrapValue: (value: string) => string = (value) => value,
      field: string = 'name',
      ): ContainsFilterNode {
      const translate = (node: SearchAst): ContainsFilterNode => {
      switch (node.type) {
      case 'term':
      return { term: { field, op: 'CONTAINS', values: [wrapValue(node.value)] } };
      case 'and':
      return { and: [translate(node.left), translate(node.right)] };
      case 'or':
      return { or: [translate(node.left), translate(node.right)] };
      case 'not':
      return { not: translate(node.operand) };
      }
      };
      return translate(ast);
      }