lucaong / minisearch

Tiny and powerful JavaScript full-text search engine for browser and Node
https://lucaong.github.io/minisearch/
MIT License
4.67k stars 133 forks source link

storeFields not being returned in autoSuggest #207

Closed prenx4x closed 1 year ago

prenx4x commented 1 year ago

Hello, I am using minisearch for autocompletion. I add documents with this code -

miniSearch = new MiniSearch({
    fields: ["keyword"],
    storeFields: ["keyword", "description"],
  });
  miniSearch.addAll(autoCompleteDB);

My document looks like array of:

{
   id: number;
   keyword: string;
   description: string;
}

And then get suggetions using -

  const suggetions = miniSearch.autoSuggest(word.text);
  console.log(suggetions);

The output is -

[
    {
        "suggestion": "megabit",
        "terms": [
            "megabit"
        ],
        "score": 2.3306379416671166
    },
    {
        "suggestion": "megabyte",
        "terms": [
            "megabyte"
        ],
        "score": 2.310253353256033
    },
    {
        "suggestion": "megagram",
        "terms": [
            "megagram"
        ],
        "score": 2.310253353256033
    },
    {
        "suggestion": "megaliter",
        "terms": [
            "megaliter"
        ],
        "score": 2.294643533301601
    },
    {
        "suggestion": "megasecond",
        "terms": [
            "megasecond"
        ],
        "score": 2.2823067401118076
    },
    {
        "suggestion": "megameter",
        "terms": [
            "megameter"
        ],
        "score": 1.873617243756775
    },
    {
        "suggestion": "meter",
        "terms": [
            "meter"
        ],
        "score": 1.7485642690373482
    }
]

As you can see I don't see description here, which I added as a storeFields. Also I don't see the id as well. If I got ID, then I could fetch other info from my original document array as well.

Am I missing something ot is this not supported in autosuggest?

lucaong commented 1 year ago

Hi @prenx4x , The standard use case for autoSuggest is to auto complete a partial query string from the user input. Therefore, suggestion results are strings representing completed queries, and do not contain any document information like ID or stored fields. A suggestion can then be used as the query string passed to search to retrieve matching documents.

If you want document IDs and fields that match a partial search, you should use the search method with the prefix option, like miniSearch.search(text, { prefix: true }).

I hope this helps

prenx4x commented 1 year ago

Thanks for responding @lucaong Let me try search with prefix instead and see the results.

lucaong commented 1 year ago

@prenx4x I will close the issue, as I think the question was answered, but feel free to comment further if more information is needed.