export default function keywordSearchTemplate()

in packages/graph-explorer/src/connector/gremlin/keywordSearch/keywordSearchTemplate.ts [22:70]


export default function keywordSearchTemplate({
  searchTerm,
  vertexTypes = [],
  searchByAttributes = [],
  limit,
  offset = 0,
  exactMatch = false,
}: KeywordSearchRequest): string {
  let template = "g.V()";

  if (vertexTypes.length !== 0) {
    const hasLabelContent = vertexTypes
      .flatMap(type => type.split("::"))
      .map(type => `"${type}"`)
      .join(",");
    template += `.hasLabel(${hasLabelContent})`;
  }

  if (searchTerm) {
    const escapedSearchTerm = escapeString(searchTerm);

    const orContent = uniq(
      searchByAttributes.includes("__all")
        ? ["__id", ...searchByAttributes]
        : searchByAttributes
    )
      .filter(attr => attr !== "__all")
      .map(attr => {
        if (attr === "__id") {
          if (exactMatch === true) {
            return `has(id,"${escapedSearchTerm}")`;
          }
          return `has(id,containing("${escapedSearchTerm}"))`;
        }
        if (exactMatch === true) {
          return `has("${attr}","${escapedSearchTerm}")`;
        }
        return `has("${attr}",containing("${escapedSearchTerm}"))`;
      })
      .join(",");

    template += `.or(${orContent})`;
  }

  if (limit) {
    template += `.range(${offset},${offset + limit})`;
  }
  return template;
}