#community-help

Creating Word-Suggestions for Large Dataset

TLDR Michael is struggling to create word-suggestions for a large dataset. Jason suggested to list priority fields in query_by for single search and to filter unnecessary fields out of the returned hit.document.

Powered by Struct AI

1

5
4mo
Solved
Join the chat
Aug 07, 2023 (4 months ago)
Michael
Photo of md5-ab9a5d60cb7ebd0211d1a368b45a0473
Michael
01:24 PM
Hey everyone 👋

I'm currently struggling a little with the following situation:

So I have a big dataset with a lot of fields, no issue searching for these with the query_by parameters on multiple fields.
Now I want to create word-suggestions (typeahead) for that matter, no issue into creating a new suggestions index, but wondering what would be the best approach for this?

[
                            'name' => 'address.city',
                            'type' => 'string',
                        ],
                        [
                            'name' => 'address.county',
                            'type' => 'string',
                        ],
                        [
                            'name' => 'address.state',
                            'type' => 'string',
                        ],
                        [
                            'name' => 'address.street',
                            'type' => 'string',
                        ],
                        [
                            'name' => 'address.zip',
                            'type' => 'string',
                        ],
                        [
                            'name' => 'primaryServiceType',
                            'type' => 'string',
                        ],
                        [
                            'name' => 'servicingType',
                            'type' => 'string',
                        ],
                        [
                            'name' => 'serviceType',
                            'type' => 'string[]',
                        ],
                        [
                            'name' => 'title',
                            'type' => 'string',
                        ],
                        [
                            'name' => 'post_date_timestamp',
                            'type' => 'int32',
                            'sort' => true,
                        ],

The above would be all the fields I want to receive a suggestion for. So if you'd type Maryland for example. I might have a resource where Maryland is part of the title, just as much Maryland could be part of the street or the county...

So If I start typing Mary I'd love to get the suggestion --> title / county / street for that matter, but weighted.
I do get results, I have weighted results, but how would I display that best as a single result?
Merge all the data together in array from the results I get or .. ?
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
03:36 PM
The order of fields you mention in query_by, along with query_by_weights determines the ranking of the best-matched record
03:37
Jason
03:37 PM
> but how would I display that best as a single result?
> Merge all the data together in array from the results I get or
It sounds like you’re sending multiple searches in a multi_search request, with one search per field? This is not necessary to do unless you have some other constraint. Instead if you just list the fields in priority order in query_by in a single search, Typesense will return the best matched result, across different fields in a single ranked list
Aug 08, 2023 (4 months ago)
Michael
Photo of md5-ab9a5d60cb7ebd0211d1a368b45a0473
Michael
07:11 AM
I'm currently doing it as follows Jason:

const searchParameters = {
        q: searchParams?.searchQuery,
        query_by: 'title,address.city,address.county,address.state,address.street,address.zip,primaryServiceType,serviceType,servicingType',
        query_by_weights: '3,2,1,1,2,2,3,3,3',
        filter_by: searchParams?.filterQuery,
        sort_by: sortParams.type + ':' + sortParams.order,
    }

But then my results are an object that are returned following the structure I've shown above.

What I'd like to do is get a single array of best search matches, without the need to loop through all the fields in the returned object ( if that is possible )
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
04:14 PM
You would have to filter the fields you don’t need out of the returned hit.document. You could do the filtering based on the presence of a highlight for that field in hit.highlight.

So this needs to be done client-side with the values in the API response

1