Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Typescript definition or enhance search result format #396

Open
gempain opened this issue Jun 21, 2023 · 0 comments
Open

Improve Typescript definition or enhance search result format #396

gempain opened this issue Jun 21, 2023 · 0 comments
Assignees

Comments

@gempain
Copy link

gempain commented Jun 21, 2023

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": []
  }
]
@ts-thomas ts-thomas self-assigned this Jan 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants