Skip to content

ElasticsearchAPIConnector searchTerm with white space throws error #1190

@lluisemper

Description

@lluisemper

Describe the bug
If to the searchTerm I add a whitespace the package search-ui-elasticsearch-connector throws and error internally.

To Reproduce
Steps to reproduce the behavior:
By doing a post request using postman to "/api/search" it will throw an error if the searchTerm includes a white space between 2 characters. If I remove the white space it works as it should.

I created a small express server to reproduce this:

import express from "express";
import cors from "cors"; // Import the cors package
import ElasticsearchAPIConnector from "@elastic/search-ui-elasticsearch-connector";

const app = express();
const PORT = process.env.PORT || 3001;

app.use(
  cors({
    origin: "*",
  })
);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.post("/api/search", async (req, res) => {
  try {
    const queryConfig = {
      result_fields: { log: { raw: {} } },
      search_fields: { log: {} },
      facets: {},
    };
    const state = {
      current: 1,
      filters: [],
      resultsPerPage: 20,
      searchTerm: "T 0", // WIHTOUT WHITESPACES IT WORKS WELL
      sortDirection: "",
      sortField: "",
    }; // Use req.body to access the parsed JSON

    const connector = new ElasticsearchAPIConnector({
      host: "<host>",
      index: `<index>`,
      apiKey: "<apiKey>",
    });
    const response = await connector.onSearch(state, queryConfig);
    return res.json(response);
  } catch (error) {
    console.error("🚀 ~ error:", error);
    return res.status(500).json({ error: "Something went wrong" });
  }
});

// Start the server
app.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});

This is the error I get:

🚀 ~ error: TypeError: Cannot read properties of undefined (reading 'total')
    at de (file://test/node_modules/@elastic/search-ui-elasticsearch-connector/lib/index.mjs:1:6891)
    at J (file://test/node_modules/@elastic/search-ui-elasticsearch-connector/lib/index.mjs:1:6396)
    at file://test/node_modules/@elastic/search-ui-elasticsearch-connector/lib/index.mjs:1:12147
    at Generator.next (<anonymous>)
    at o (file://test/node_modules/@elastic/search-ui-elasticsearch-connector/lib/index.mjs:1:493)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Expected behavior
I would expect this to work searchTerm: "T 0",. replacing this by searchTerm: "T0", does work.

Screenshots
If applicable, add screenshots to help explain your problem.

Which backends and packages are you using:
Backend: I am using an express server with a selfhosted elasticsearch instance, version 8.17.3
Packages: [@elastic/[email protected]]

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions