#community-help

Searching Specific Contributor Type in Record

TLDR Greg needs to search for contributors who are authors only. Jason suggested multiple search queries filtered by types and aggregation of results on client-side.

Powered by Struct AI

1

Oct 03, 2022 (15 months ago)
Greg
Photo of md5-11c0f771a29e2aa8d72ae9544cc39017
Greg
04:59 PM
I have this contributor record
{
    "contributor1": {
        "bio": "",
        "firstName": "Lewis",
        "lastName": "Carroll",
        "type": "Author"
    },
    "contributor2": {
        "bio": "",
        "firstName": "Greg",
        "lastName": "Mascherino",
        "type": "Illustrator"
    },
    "contributor3": {
        "bio": "",
        "firstName": "Greg",
        "lastName": "Jamie",
        "type": "Illustrator"
    },
    "contributor4": {
        "bio": "",
        "firstName": "Jamie",
        "lastName": "Greg",
        "type": "Illustrator"
    },
    "contributor5": {
        "bio": "",
        "firstName": "Jamie",
        "lastName": "Mascherino",
        "type": "Illustrator"
    }
}

I want to search for Carroll and I only want to return this result if Carroll is an author. So Searching for Greg would not be a hit.
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
05:44 PM
You would have to use filter_by for this. Something like contributor1.type:=Author
05:45
Jason
05:45 PM
Typesense doesn’t support doing OR’s across multiple fields at the moment (though we’re actively working on adding support for this), so you would have to break the query out into multiple multi_search queries, with one contributor field per query
Greg
Photo of md5-11c0f771a29e2aa8d72ae9544cc39017
Greg
06:13 PM
The problem with filter_by is that it would return the record if any of the contributors is an author. It should only return if the query matches and the type is author. I assume that is not possible?
06:15
Greg
06:15 PM
My query by field would look like this: contributors.contributor5.lastName,contributors.contributor5.firstName,contributors.contributor4.lastName,contributors.contributor1.firstName,contributors.contributor2.firstName,contributors.contributor4.firstName,contributors.contributor1.lastName,contributors.contributor2.lastName,contributors.contributor3.firstName,contributors.contributor3.lastName
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
06:33 PM
You could do contributor1.type:=Author && contributor2.type:!=Author && contributor3.type:!=Author && contributor4.type:!=Author and so on
Greg
Photo of md5-11c0f771a29e2aa8d72ae9544cc39017
Greg
07:51 PM
I would have to know which contributor is an author 😕. I need to match only if the query matches and the type matches, but group them all into one result. I don’t think I can do it via query. I would need to parse the results of the query I think.
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
07:54 PM
If you have say 3 contributor fields, you’d run 3 search queries (in a multi_search request) like this:

Query1: 
q: carrol
filter_by: contributor1.type:=Author && contributor2.type:!=Author && contributor3.type:!=Author

Query2: 
q: carrol
filter_by: contributor1.type:!=Author && contributor2.type:=Author && contributor3.type:!=Author

Query3: 
q: carrol
filter_by: contributor1.type:!=Author && contributor2.type:!=Author && contributor3.type:=Author

And then aggregate the results on the client-side
Greg
Photo of md5-11c0f771a29e2aa8d72ae9544cc39017
Greg
07:55 PM
Yea, that was what I was thinking. Thank you for the help.

1

Typesense

Lightning-fast, open source search engine for everyone | Knowledge Base powered by Struct.AI

Indexed 3015 threads (79% resolved)

Join Our Community