flexsearch icon indicating copy to clipboard operation
flexsearch copied to clipboard

Improve Typescript definition or enhance search result format

Open gempain opened this issue 2 years ago • 2 comments

Hi, first, let me say that this project is an outstanding piece of software. Thanks for the time you take to write and maintain it.

We've been using this project in a TypeScript project and I have to say there is room for improvement.

First, the definitions written in @types/flexsearch cannot be imported correctly as explained in #342.

Then, these definitions actually lack some formats that I discovered while playing with the settings.

Here is a basic example of what I tried:

const { Document } = require('flexsearch');

export const campaignsDocument = new Document({
  document: {
    id: 'id',
    tag: 'clientId',
    index: [
      {
        field: 'title',
        tokenize: 'forward',
      },
    ],
    store: ['id', 'title', 'clientId'],
  },
});

const values = [
  { id: '1', clientId: '2', title: 'hello' },
  { id: '2', clientId: '3', title: 'hello world' },
];

values.map(val => campaignsDocument.add(val));

const settings = [
  {},

  { pluck: 'title' },
  { pluck: 'title', tag: '2' },
  { pluck: 'title', enrich: true },
  { pluck: 'title', tag: '2', enrich: true },

  { tag: ['2'] },
  { tag: ['2'], enrich: true },

  { enrich: true },
];

const query = ['hello', ''];

const possibleOptions = settings.flatMap(setting => query.map(q => ({ setting, q })));

const matrix = possibleOptions.map(({ setting, q }) => ({
  q,
  setting,
  result: campaignsDocument.search(q, setting),
}));

console.log(JSON.stringify(matrix));

The different formats returned vary base on the combinations of params. The enrich option is clear for typing, but the correlation between tag and pluck seem strange when combined with an empty search like ''.

Maybe the use case for searching '' is a bit weird, but anyways, it does create format differences.

[
  {
    "q": "hello",
    "setting": {},
    "result": [
      {
        "field": "title",
        "result": [
          "1",
          "2"
        ]
      }
    ]
  },
  {
    "q": "",
    "setting": {},
    "result": []
  },
  {
    "q": "hello",
    "setting": {
      "pluck": "title"
    },
    "result": [
      "1",
      "2"
    ]
  },
  {
    "q": "",
    "setting": {
      "pluck": "title"
    },
    "result": []
  },
  {
    "q": "hello",
    "setting": {
      "pluck": "title",
      "tag": "2"
    },
    "result": [
      "1"
    ]
  },
  {
    "q": "",
    "setting": {
      "pluck": "title",
      "tag": "2"
    },
    "result": [
      {
        "tag": "2",
        "result": [
          "1"
        ]
      }
    ]
  },
  {
    "q": "hello",
    "setting": {
      "pluck": "title",
      "enrich": true
    },
    "result": [
      {
        "id": "1",
        "doc": {
          "id": "1",
          "title": "hello",
          "clientId": "2"
        }
      },
      {
        "id": "2",
        "doc": {
          "id": "2",
          "title": "hello world",
          "clientId": "3"
        }
      }
    ]
  },
  {
    "q": "",
    "setting": {
      "pluck": "title",
      "enrich": true
    },
    "result": []
  },
  {
    "q": "hello",
    "setting": {
      "pluck": "title",
      "tag": "2",
      "enrich": true
    },
    "result": [
      {
        "id": "1",
        "doc": {
          "id": "1",
          "title": "hello",
          "clientId": "2"
        }
      }
    ]
  },
  {
    "q": "",
    "setting": {
      "pluck": "title",
      "tag": "2",
      "enrich": true
    },
    "result": [
      {
        "tag": "2",
        "result": [
          {
            "id": "1",
            "doc": {
              "id": "1",
              "title": "hello",
              "clientId": "2"
            }
          }
        ]
      }
    ]
  },
  {
    "q": "hello",
    "setting": {
      "tag": [
        "2"
      ]
    },
    "result": [
      {
        "field": "title",
        "result": [
          "1"
        ]
      }
    ]
  },
  {
    "q": "",
    "setting": {
      "tag": [
        "2"
      ]
    },
    "result": [
      {
        "tag": "2",
        "result": [
          "1"
        ]
      }
    ]
  },
  {
    "q": "hello",
    "setting": {
      "tag": [
        "2"
      ],
      "enrich": true
    },
    "result": [
      {
        "field": "title",
        "result": [
          {
            "id": "1",
            "doc": {
              "id": "1",
              "title": "hello",
              "clientId": "2"
            }
          }
        ]
      }
    ]
  },
  {
    "q": "",
    "setting": {
      "tag": [
        "2"
      ],
      "enrich": true
    },
    "result": [
      {
        "tag": "2",
        "result": [
          {
            "id": "1",
            "doc": {
              "id": "1",
              "title": "hello",
              "clientId": "2"
            }
          }
        ]
      }
    ]
  },
  {
    "q": "hello",
    "setting": {
      "enrich": true
    },
    "result": [
      {
        "field": "title",
        "result": [
          {
            "id": "1",
            "doc": {
              "id": "1",
              "title": "hello",
              "clientId": "2"
            }
          },
          {
            "id": "2",
            "doc": {
              "id": "2",
              "title": "hello world",
              "clientId": "3"
            }
          }
        ]
      }
    ]
  },
  {
    "q": "",
    "setting": {
      "enrich": true
    },
    "result": []
  }
]

gempain avatar Jun 21 '23 14:06 gempain

Probably the latest version will fix that issue. When doing a tag-based search and the query is empty, then it will perform a tag-only-search like when leaving the query at all. Did you expect to get no results? It is not clear to me what the desired behavior should be in this situation. What really would help me is, when you can add a desired result to each example.

ts-thomas avatar Apr 01 '25 06:04 ts-thomas

@gempain The TypeScript support was greatly improved. Would be nice if you can try it again with the latest version and give me some short feedback if you see any room for improvements.

ts-thomas avatar May 12 '25 12:05 ts-thomas